Twisted Prime Number

Here given code implementation process.

//C Program
//Check if a given number is Twisted Prime or not
#include <stdio.h>

//Check number is prime or not
int is_prime(int n)
{
  if(n<=1)
  {
    return 0;
  }
  //Base case
  if(n==2 || n ==3 || n==5)
  {
    return 1;
  }
  
  for (int i = n/2; i > 1 ; --i)
  {
    //Check divisibility of a number
    if(n%i == 0)
    {
      return 0;
    }
  }
  
  return 1;
}
//Reverse the given number
int reverse(int number)
{

  int result = 0;

  while(number>0)
  {
    result=(result*10)+(number%10);
    //remove last digit
    number/=10;
  }
  return result;
}
//Determine whether number is twisted prime or not
void is_twisted_prime(int number) 
{ 
  //Check if a given number is prime and reversal of number is also prime?
  if (is_prime(number) == 1 && is_prime(reverse(number)) == 1) 
  {
    //When Yes
    printf("%d Is Twisted Prime Number\n", number);
  }
  else
  { 
    //When No
    printf("%d Is not a Twisted Prime Number\n", number);
  } 
} 


int main() {
  //Test Cases
  is_twisted_prime(113);
  is_twisted_prime(23);
  is_twisted_prime(12);
  is_twisted_prime(13);
  is_twisted_prime(149);
  return 0;
}

Output

113 Is Twisted Prime Number
23 Is not a Twisted Prime Number
12 Is not a Twisted Prime Number
13 Is Twisted Prime Number
149 Is Twisted Prime Number
/*
 C++ Program
 Check if a given number is Twisted Prime or not
*/
#include<iostream>

using namespace std;

class MyNumber {
	public:

		//Check number is prime or not
		bool is_prime(int n) {
			if (n <= 1) {
				return false;
			}
			//Base case
			if (n == 2 || n == 3 || n == 5) {
				return true;
			}
			for (int i = n / 2; i > 1; --i) {
				//Check divisibility of a number
				if (n % i == 0) {
					return false;
				}
			}
			return true;
		}
	//Reverse the given number
	int reverse(int number) {
		int result = 0;
		while (number > 0) {
			result = (result *10) + (number % 10);
			//remove last digit
			number /= 10;
		}
		return result;
	}
	//Determine whether number is twisted prime or not
	void is_twisted_prime(int number) {
		//Check if a given number is prime and reversal of number is also prime?

		if (this->is_prime(number) == true && this->is_prime(this->reverse(number)) == true) {
			//When Yes

			cout << number << " Is Twisted Prime Number\n";
		} else {
			//When No

			cout << number << " Is not a Twisted Prime Number\n";
		}
	}
};
int main() {
	MyNumber obj ;
	//Test Cases
	obj.is_twisted_prime(113);
	obj.is_twisted_prime(23);
	obj.is_twisted_prime(12);
	obj.is_twisted_prime(13);
	obj.is_twisted_prime(149);
	return 0;
}

Output

113 Is Twisted Prime Number
23 Is not a Twisted Prime Number
12 Is not a Twisted Prime Number
13 Is Twisted Prime Number
149 Is Twisted Prime Number
/*
  Java Program
  Check if a given number is Twisted Prime or not
*/

public class MyNumber {


  //Check number is prime or not
  public boolean is_prime(int n)
  {
    if(n<=1)
    {
      return false;
    }
    //Base case
    if(n==2 || n ==3 || n==5)
    {
      return true;
    }
    
    for (int i = n/2; i > 1 ; --i)
    {
      //Check divisibility of a number
      if(n%i == 0)
      {
        return false;
      }
    }
    
    return true;
  }
  //Reverse the given number
  public int reverse(int number)
  {

    int result = 0;

    while(number>0)
    {
      result=(result*10)+(number%10);
      //remove last digit
      number/=10;
    }
    return result;
  }
  //Determine whether number is twisted prime or not
  public void is_twisted_prime(int number) 
  { 
    //Check if a given number is prime and reversal of number is also prime?
    if (is_prime(number) == true && is_prime(reverse(number)) == true) 
    {
      //When Yes
      System.out.print(number+" Is Twisted Prime Number\n");
    }
    else
    { 
      //When No
      System.out.print(number+" Is not a Twisted Prime Number\n");
    } 
  } 

  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
    //Test Cases
    obj.is_twisted_prime(113);
    obj.is_twisted_prime(23);
    obj.is_twisted_prime(12);
    obj.is_twisted_prime(13);
    obj.is_twisted_prime(149);

  }
}

Output

113 Is Twisted Prime Number
23 Is not a Twisted Prime Number
12 Is not a Twisted Prime Number
13 Is Twisted Prime Number
149 Is Twisted Prime Number
/*
  C# Program
  Check if a given number is Twisted Prime or not
*/
using System;
public class MyNumber {


	//Check number is prime or not
	public Boolean is_prime(int n) {
		if (n <= 1) {
			return false;
		}
		//Base case
		if (n == 2 || n == 3 || n == 5) {
			return true;
		}

		for (int i = n / 2; i > 1; --i) {
			//Check divisibility of a number
			if (n % i == 0) {
				return false;
			}
		}

		return true;
	}
	//Reverse the given number
	public int reverse(int number) {

		int result = 0;

		while (number > 0) {
			result = (result * 10) + (number % 10);
			//remove last digit
			number /= 10;
		}
		return result;
	}
	//Determine whether number is twisted prime or not
	public void is_twisted_prime(int number) {
		//Check if a given number is prime and reversal of number is also prime?
		if (is_prime(number) == true && is_prime(reverse(number)) == true) {
			//When Yes
			Console.Write(number + " Is Twisted Prime Number\n");
		} else {
			//When No
			Console.Write(number + " Is not a Twisted Prime Number\n");
		}
	}

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		//Test Cases
		obj.is_twisted_prime(113);
		obj.is_twisted_prime(23);
		obj.is_twisted_prime(12);
		obj.is_twisted_prime(13);
		obj.is_twisted_prime(149);

	}
}

Output

113 Is Twisted Prime Number
23 Is not a Twisted Prime Number
12 Is not a Twisted Prime Number
13 Is Twisted Prime Number
149 Is Twisted Prime Number
# Python 3 Program
# Check if a given number is Twisted Prime or not

class MyNumber :
	#Check number is prime or not
	def is_prime(self, n) :
		if (n <= 1) :
			return False
		
		#Base case

		if (n == 2 or n == 3 or n == 5) :
			return True
		
		i = int(n / 2)
		while (i > 1) :
			#Check divisibility of a number

			if (n % i == 0) :
				return False
			
			i -= 1
		
		return True
	
	#Reverse the given number
	def reverse(self, number) :
		result = 0
		while (number > 0) :
			result = (result * 10) + (number % 10)
			#remove last digit
			number = int(number / 10)
		
		return result
	
	#Determine whether number is twisted prime or not
	def is_twisted_prime(self, number) :
		#Check if a given number is prime and reversal of number is also prime?

		if (self.is_prime(number) == True and self.is_prime(self.reverse(number)) == True) :
			print(number ," Is Twisted Prime Number")
		else :
			print(number ," Is not a Twisted Prime Number")
		
	

def main() :
	obj = MyNumber()
	#Test Case
	obj.is_twisted_prime(113)
	obj.is_twisted_prime(23)
	obj.is_twisted_prime(12)
	obj.is_twisted_prime(13)
	obj.is_twisted_prime(149)


if __name__ == "__main__":
	main()

Output

113 Is Twisted Prime Number
23 Is not a Twisted Prime Number
12 Is not a Twisted Prime Number
13 Is Twisted Prime Number
149 Is Twisted Prime Number
# Ruby Program 
# Check if a given number is Twisted Prime or not

class MyNumber 
	#Check number is prime or not
	def is_prime(n) 
		if (n <= 1) 
			return false
		end
		#Base case
		if (n == 2 or n == 3 or n == 5) 
			return true
		end
		i = n / 2
		while (i > 1) 
			#Check divisibility of a number
			if (n % i == 0) 
				return false
			end
			i -= 1
		end
		return true
	end
	#Reverse the given number
	def reverse(number) 
		result = 0
		while (number > 0) 
			result = (result * 10) + (number % 10)
			#remove last digit
			number /= 10
		end
		return result
	end
	#Determine whether number is twisted prime or not
	def is_twisted_prime(number) 
		#Check if a given number is prime and reversal of number is also prime?

		if (self.is_prime(number) == true and self.is_prime(self.reverse(number)) == true) 
			print(number ," Is Twisted Prime Number\n")
		else 
			print(number ," Is not a Twisted Prime Number\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	#Test Case
	obj.is_twisted_prime(113)
	obj.is_twisted_prime(23)
	obj.is_twisted_prime(12)
	obj.is_twisted_prime(13)
	obj.is_twisted_prime(149)
end
main()

Output

113 Is Twisted Prime Number
23 Is not a Twisted Prime Number
12 Is not a Twisted Prime Number
13 Is Twisted Prime Number
149 Is Twisted Prime Number
/*
 Scala Program
 Check if a given number is Twisted Prime or not
*/
class MyNumber {
	//Check number is prime or not
	def is_prime(n: Int): Boolean = {
		if (n <= 1) {
			return false;
		}
		//Base case

		if (n == 2 || n == 3 || n == 5) {
			return true;
		}
		var i: Int = n / 2;
		while (i > 1) {
			//Check divisibility of a number

			if (n % i == 0) {
				return false;
			}
			i -= 1;
		}
		return true;
	}
	//Reverse the given number
	def reverse(value: Int): Int = {
		var result: Int = 0;
      	var number: Int = value;
		while (number > 0) {
			result = (result * 10) + (number % 10);
			//remove last digit
			number /= 10;
		}
		return result;
	}
	//Determine whether number is twisted prime or not
	def is_twisted_prime(number: Int): Unit = {
		//Check if a given number is prime and reversal of number is also prime?

		if (this.is_prime(number) == true && this.is_prime(this.reverse(number)) == true) {
			print(s"$number Is Twisted Prime Number\n");
		} else {
			print(s"$number Is not a Twisted Prime Number\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case
		obj.is_twisted_prime(113);
        obj.is_twisted_prime(23);
        obj.is_twisted_prime(12);
        obj.is_twisted_prime(13);
        obj.is_twisted_prime(149);
	}
}

Output

113 Is Twisted Prime Number
23 Is not a Twisted Prime Number
12 Is not a Twisted Prime Number
13 Is Twisted Prime Number
149 Is Twisted Prime Number
/*
  Swift 4 Program
  Check if a given number is Twisted Prime or not
*/
class MyNumber {
	//Check number is prime or not
	func is_prime(_ n: Int) -> Bool {
		if (n <= 1) {
			return false;
		}
		//Base case

		if (n == 2 || n == 3 || n == 5) {
			return true;
		}
		var i: Int = n / 2;
		while (i > 1) {
			//Check divisibility of a number

			if (n % i == 0) {
				return false;
			}
			i -= 1;
		}
		return true;
	}
	//Reverse the given number
	func reverse(_ value: Int) -> Int {
		var result: Int = 0;
      	var number: Int = value;
		while (number > 0) {
			result = (result * 10) + (number % 10);
			//remove last digit
			number /= 10;
		}
		return result;
	}
	//Determine whether number is twisted prime or not
	func is_twisted_prime(_ number: Int) {
		//Check if a given number is prime and reversal of number is also prime?

		if (self.is_prime(number) == true && self.is_prime(self.reverse(number)) == true) {
			print(number ," Is Twisted Prime Number");
		} else {
			print(number ," Is not a Twisted Prime Number");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	//Test Case
	obj.is_twisted_prime(113);
	obj.is_twisted_prime(23);
	obj.is_twisted_prime(12);
	obj.is_twisted_prime(13);
	obj.is_twisted_prime(149);
}
main();

Output

113  Is Twisted Prime Number
23  Is not a Twisted Prime Number
12  Is not a Twisted Prime Number
13  Is Twisted Prime Number
149  Is Twisted Prime Number
<?php
/*
  Php Program
  Check if a given number is Twisted Prime or not
*/
class MyNumber {
	//Check number is prime or not

	public 	function is_prime($n) {
		if ($n <= 1) {
			return false;
		}
		//Base case

		if ($n == 2 || $n == 3 || $n == 5) {
			return true;
		}
		for ($i = intval($n / 2); $i > 1; --$i) {
			//Check divisibility of a number

			if ($n % $i == 0) {
				return false;
			}
		}
		return true;
	}
	//Reverse the given number

	public 	function reverse($number) {
		$result = 0;
		while ($number > 0) {
			$result = ($result *10) + ($number % 10);
			//remove last digit
			$number=intval($number / 10);
		}
		return $result;
	}
	//Determine whether number is twisted prime or not

	public 	function is_twisted_prime($number) {
		//Check if a given number is prime and reversal of number is also prime?

		if ($this->is_prime($number) == true && $this->is_prime($this->reverse($number)) == true) {
			//When Yes

			echo($number ." Is Twisted Prime Number\n");
		} else {
			//When No

			echo($number ." Is not a Twisted Prime Number\n");
		}
	}
};

function main() {
	$obj = new MyNumber();
	//Test Cases

	$obj->is_twisted_prime(113);
	$obj->is_twisted_prime(23);
	$obj->is_twisted_prime(12);
	$obj->is_twisted_prime(13);
	$obj->is_twisted_prime(149);
}
main();

Output

113 Is Twisted Prime Number
23 Is not a Twisted Prime Number
12 Is not a Twisted Prime Number
13 Is Twisted Prime Number
149 Is Twisted Prime Number
/*
 Node Js Program
 Check if a given number is Twisted Prime or not
*/
class MyNumber {
	//Check number is prime or not
	is_prime(n) {
		if (n <= 1) {
			return false;
		}
		//Base case

		if (n == 2 || n == 3 || n == 5) {
			return true;
		}
		for (var i = parseInt(n / 2); i > 1; --i) {
			//Check divisibility of a number

			if (n % i == 0) {
				return false;
			}
		}
		return true;
	}
	//Reverse the given number
	reverse(number) {
		var result = 0;
		while (number > 0) {
			result = (result *10) + (number % 10);
			//remove last digit
			number = parseInt(number / 10);
		}
		return result;
	}
	//Determine whether number is twisted prime or not
	is_twisted_prime(number) {
		//Check if a given number is prime and reversal of number is also prime?

		if (this.is_prime(number) == true && this.is_prime(this.reverse(number)) == true) {
			//When Yes

			process.stdout.write(number + " Is Twisted Prime Number\n");
		} else {
			//When No

			process.stdout.write(number + " Is not a Twisted Prime Number\n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test Cases
	obj.is_twisted_prime(113);
	obj.is_twisted_prime(23);
	obj.is_twisted_prime(12);
	obj.is_twisted_prime(13);
	obj.is_twisted_prime(149)
}
main();

Output

113 Is Twisted Prime Number
23 Is not a Twisted Prime Number
12 Is not a Twisted Prime Number
13 Is Twisted Prime Number
149 Is Twisted Prime Number


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved