Circular Prime Number

Here given code implementation process.

//C Program
//Check if a given number is Circular Prime or not
#include <stdio.h>
#include <math.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;
}
//Count the number of digits in number 
int digit_length(int number) 
{ 
  int size = 0; 

  while (number!=0) 
  { 
    number/=10; 

    size++; 
  } 
  return size; 
}


int is_circular_prime(int number) 
{ 
  //Assign the value of how many number of digits are exist in number
  int  size = digit_length(number);

  int auxiliary = number;

  //Variable which control  execution process
  int remainder = 0;

  int divisor = 0;

  while (is_prime(auxiliary)==1) { 

    //Calculate divisor of auxiliary number
    divisor = auxiliary / 10;

    //Calculate remainder of auxiliary number
    remainder = auxiliary % 10; 
    
    //permutation calculation
    auxiliary = (pow(10, size - 1)) * remainder + divisor; 
    
    //When permutation of number is equal to actual number
    //T
    if (auxiliary == number) 
    {
      return 1; 
    }
  } 

  return 0; 
} 
//Function which are handling the request of number is circular prime or not
void circular_prime(int number)
{
  if(is_circular_prime(number)==1)
  {
    //When Yes
    printf("%d Is an Circular Prime Number\n",number);
  } 
  else
  {
    //When No
    printf("%d Is not a Circular Prime Number\n",number);
  }
}

int main() {
  //Test Cases
  circular_prime(71);
  circular_prime(9311);
  circular_prime(23);
  circular_prime(43);
  circular_prime(13);

  return 0;
}

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
/*
 C++ Program
 Check if a given number is Circular Prime or not
*/
#include<iostream>
#include <math.h>
using namespace std;

class MyNumber {
	public:

		//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;
		}
	//Count the number of digits in number 
	int digit_length(int number) {
		int size = 0;
		while (number != 0) {
			number /= 10;
			size++;
		}
		return size;
	}
	int is_circular_prime(int number) {
		//Assign the value of how many number of digits are exist in number
		int size = this->digit_length(number);
		int auxiliary = number;
		//Variable which control  execution process
		int remainder = 0;
		int divisor = 0;
		while (this->is_prime(auxiliary) == 1) {
			//Calculate divisor of auxiliary number
			divisor = auxiliary / 10;
			//Calculate remainder of auxiliary number
			remainder = auxiliary % 10;
			//permutation calculation
			auxiliary = ((int)pow(10, size - 1)) *remainder + divisor;
			//When permutation of number is equal to actual number

			if (auxiliary == number) {
				return 1;
			}
		}
		return 0;
	}
	//Function which are handling the request of number is circular prime or not
	void circular_prime(int number) {
		if (this->is_circular_prime(number) == 1) {
			//When Yes

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

			cout << number << " Is not a Circular Prime Number\n";
		}
	}
};
int main() {
	MyNumber obj;
	//Test Cases
	obj.circular_prime(71);
	obj.circular_prime(9311);
	obj.circular_prime(23);
	obj.circular_prime(43);
	obj.circular_prime(13);
	return 0;
}

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
/*
  Java Program
  Check if a given number is Circular Prime or not
*/

public class MyNumber {

  //Check number is prime or not
  public 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;
  }
  //Count the number of digits in number 
  public int digit_length(int number) 
  { 
    int size = 0; 

    while (number!=0) 
    { 
      number/=10; 

      size++; 
    } 
    return size; 
  }


  public int is_circular_prime(int number) 
  { 
    //Assign the value of how many number of digits are exist in number
    int  size = digit_length(number);

    int auxiliary = number;

    //Variable which control  execution process
    int remainder = 0;

    int divisor = 0;

    while (is_prime(auxiliary)==1) { 

      //Calculate divisor of auxiliary number
      divisor = auxiliary / 10;

      //Calculate remainder of auxiliary number
      remainder = auxiliary % 10; 
      
      //permutation calculation
      auxiliary = ((int)Math.pow(10, size - 1)) * remainder + divisor; 
      
      //When permutation of number is equal to actual number
      if (auxiliary == number) 
      {
        return 1; 
      }
    } 

    return 0; 
  } 
  //Function which are handling the request of number is circular prime or not
  public void circular_prime(int number)
  {
    if(is_circular_prime(number)==1)
    {
      //When Yes
      System.out.print(number+" Is an Circular Prime Number\n");
    } 
    else
    {
      //When No
      System.out.print(number+" Is not a Circular Prime Number\n");
    }
  }

  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
    //Test Cases
    obj.circular_prime(71);
    obj.circular_prime(9311);
    obj.circular_prime(23);
    obj.circular_prime(43);
    obj.circular_prime(13);

  }
}

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
/*
  C# Program
  Check if a given number is Circular Prime or not
*/
using System;
public class MyNumber {

	//Check number is prime or not
	public 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;
	}
	//Count the number of digits in number 
	public int digit_ength(int number) {
		int size = 0;

		while (number != 0) {
			number /= 10;

			size++;
		}
		return size;
	}


	public int is_circular_prime(int number) {
		//Assign the value of how many number of digits are exist in number
		int size = digit_ength(number);

		int auxiliary = number;

		//Variable which control  execution process
		int reMainder = 0;

		int divisor = 0;

		while (is_prime(auxiliary) == 1) {

			//Calculate divisor of auxiliary number
			divisor = auxiliary / 10;

			//Calculate reMainder of auxiliary number
			reMainder = auxiliary % 10;

			//permutation calculation
			auxiliary = ((int) Math.Pow(10, size - 1)) * reMainder + divisor;

			//When permutation of number is equal to actual number
			if (auxiliary == number) {
				return 1;
			}
		}

		return 0;
	}
	//Function which are handling the request of number is circular prime or not
	public void circular_prime(int number) {
		if (is_circular_prime(number) == 1) {
			//When Yes
			Console.Write(number + " Is an Circular Prime Number\n");
		} else {
			//When No
			Console.Write(number + " Is not a Circular Prime Number\n");
		}
	}

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		//Test Cases
		obj.circular_prime(71);
		obj.circular_prime(9311);
		obj.circular_prime(23);
		obj.circular_prime(43);
		obj.circular_prime(13);

	}
}

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
# Python 3 Program
# Check if a given number is Circular Prime or not

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

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

			if (n % i == 0) :
				return 0
			
			i -= 1
		
		return 1
	
	#Count the number of digits in number 
	def digit_length(self, number) :
		size = 0
		while (number != 0) :
			number = int(number / 10)
			size += 1
		
		return size
	
	def is_circular_prime(self, number) :
		size = self.digit_length(number)
		auxiliary = number
		remainder = 0
		divisor = 0
		while (self.is_prime(auxiliary) == 1) :
			#Calculate divisor of auxiliary number
			divisor = int(auxiliary / 10)
			#Calculate remainder of auxiliary number
			remainder = auxiliary % 10
			#permutation calculation
			auxiliary = (10**(size - 1)) * remainder + divisor
			#When permutation of number is equal to actual number

			if (auxiliary == number) :
				return 1
			
		
		return 0
	
	#Function which are handling the request of number is circular prime or not
	def circular_prime(self, number) :
		if (self.is_circular_prime(number) == 1) :
			print(number ," Is an Circular Prime Number\n")
		else :
			print(number ," Is not a Circular Prime Number\n")
		
	

def main() :
	obj = MyNumber()
	obj.circular_prime(71)
	obj.circular_prime(9311)
	obj.circular_prime(23)
	obj.circular_prime(43)
	obj.circular_prime(13)


if __name__ == "__main__":
	main()

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
# Ruby Program 
# Check if a given number is Circular Prime or not

class MyNumber 
	#Check number is prime or not
	def is_prime(n) 
		if (n <= 1) 
			return 0
		end
		#Base case

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

			if (n % i == 0) 
				return 0
			end
			i -= 1
		end
		return 1
	end
	#Count the number of digits in number 
	def digit_length(number) 
		size = 0
		while (number != 0) 
			number /= 10
			size += 1
		end
		return size
	end
	def is_circular_prime(number) 
		size = self.digit_length(number)
		auxiliary = number
		remainder = 0
		divisor = 0
		while (self.is_prime(auxiliary) == 1) 
			#Calculate divisor of auxiliary number
			divisor = auxiliary / 10
			#Calculate remainder of auxiliary number
			remainder = auxiliary % 10
			#permutation calculation
			auxiliary = (10**(size - 1)) * remainder + divisor
			#When permutation of number is equal to actual number

			if (auxiliary == number) 
				return 1
			end
		end
		return 0
	end
	#Function which are handling the request of number is circular prime or not
	def circular_prime(number) 
		if (self.is_circular_prime(number) == 1) 
			print(number ," Is an Circular Prime Number\n")
		else 
			print(number ," Is not a Circular Prime Number\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	obj.circular_prime(71)
	obj.circular_prime(9311)
	obj.circular_prime(23)
	obj.circular_prime(43)
	obj.circular_prime(13)
end
main()

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
/*
 Scala Program
 Check if a given number is Circular Prime or not
*/
class MyNumber {
	//Check number is prime or not
	def is_prime(n: Int): Int = {
		if (n <= 1) {
			return 0;
		}
		//Base case

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

			if (n % i == 0) {
				return 0;
			}
			i -= 1;
		}
		return 1;
	}
	//Count the number of digits in number 
	def digit_length(value: Int): Int = {
		var size: Int = 0;
      	var number: Int = value;
		while (number != 0) {
			number /= 10;
			size += 1;
		}
		return size;
	}
	def is_circular_prime(number: Int): Int = {
		var size: Int = this.digit_length(number);
		var auxiliary: Int = number;
		var remainder: Int = 0;
		var divisor: Int = 0;
		while (this.is_prime(auxiliary) == 1) {
			//Calculate divisor of auxiliary number
			divisor = auxiliary / 10;
			//Calculate remainder of auxiliary number
			remainder = auxiliary % 10;
			//permutation calculation
			auxiliary = (scala.math.pow(10, size-1)).toInt * remainder + divisor;
			//When permutation of number is equal to actual number

			if (auxiliary == number) {
				return 1;
			}
		}
		return 0;
	}
	//Function which are handling the request of number is circular prime or not
	def circular_prime(number: Int): Unit = {
		if (this.is_circular_prime(number) == 1) {
			print(s"$number Is an Circular Prime Number\n");
		} else {
			print(s"$number Is not a Circular Prime Number\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
        obj.circular_prime(71);
        obj.circular_prime(9311);
        obj.circular_prime(23);
        obj.circular_prime(43);
        obj.circular_prime(13);
	}
}

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
/*
  Swift 4 Program
  Check if a given number is Circular Prime or not
*/
import Foundation
class MyNumber {
	//Check number is prime or not
	func is_prime(_ n: Int) -> Int {
		if (n <= 1) {
			return 0;
		}
		//Base case

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

			if (n % i == 0) {
				return 0;
			}
			i -= 1;
		}
		return 1;
	}
	//Count the number of digits in number 
	func digit_length(_ value: Int) -> Int {
		var size: Int = 0;
      	var number: Int = value;
		while (number != 0) {
			number /= 10;
			size += 1;
		}
		return size;
	}
	func is_circular_prime(_ number: Int) -> Int {
		let size: Int = self.digit_length(number);
		var auxiliary: Int = number;
		var remainder: Int = 0;
		var divisor: Int = 0;
		while (self.is_prime(auxiliary) == 1) {
			//Calculate divisor of auxiliary number
			divisor = auxiliary / 10;
			//Calculate remainder of auxiliary number
			remainder = auxiliary % 10;
			//permutation calculation
			auxiliary =  Int(pow(Double(10), Double(size-1))) * remainder + divisor;
			//When permutation of number is equal to actual number

			if (auxiliary == number) {
				return 1;
			}
		}
		return 0;
	}
	//Function which are handling the request of number is circular prime or not
	func circular_prime(_ number: Int) {
		if (self.is_circular_prime(number) == 1) {
			print(number ," Is an Circular Prime Number");
		} else {
			print(number ," Is not a Circular Prime Number");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	obj.circular_prime(71);
	obj.circular_prime(9311);
	obj.circular_prime(23);
	obj.circular_prime(43);
	obj.circular_prime(13);
}
main();

Output

71  Is an Circular Prime Number
9311  Is an Circular Prime Number
23  Is not a Circular Prime Number
43  Is not a Circular Prime Number
13  Is an Circular Prime Number
<?php
/*
  Php Program
  Check if a given number is Circular Prime or not
*/
class MyNumber {
	//Check number is prime or not

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

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

			if ($n % $i == 0) {
				return 0;
			}
		}
		return 1;
	}
	//Count the number of digits in number 

	public 	function digit_length($number) {
		$size = 0;
		while ($number != 0) {
			$number=intval($number / 10);
			$size++;
		}
		return $size;
	}
	public 	function is_circular_prime($number) {
		//Assign the value of how many number of digits are exist in number
		$size = $this->digit_length($number);
		$auxiliary = $number;
		//Variable which control  execution process
		$remainder = 0;
		$divisor = 0;
		while ($this->is_prime($auxiliary) == 1) {
			//Calculate divisor of auxiliary number
			$divisor = intval($auxiliary / 10);
			//Calculate remainder of auxiliary number
			$remainder = $auxiliary % 10;
			//permutation calculation
			$auxiliary = (pow(10, $size - 1)) *$remainder + $divisor;
			//When permutation of number is equal to actual number

			if ($auxiliary == $number) {
				return 1;
			}
		}
		return 0;
	}
	//Function which are handling the request of number is circular prime or not

	public 	function circular_prime($number) {
		if ($this->is_circular_prime($number) == 1) {
			//When Yes

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

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

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

	$obj->circular_prime(71);
	$obj->circular_prime(9311);
	$obj->circular_prime(23);
	$obj->circular_prime(43);
	$obj->circular_prime(13);
}
main();

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
/*
 Node Js Program
 Check if a given number is Circular Prime or not
*/
class MyNumber {
	//Check number is prime or not
	is_prime(n) {
		if (n <= 1) {
			return 0;
		}
		//Base case

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

			if (n % i == 0) {
				return 0;
			}
		}
		return 1;
	}
	//Count the number of digits in number 
	digit_length(number) {
		var size = 0;
		while (number != 0) {
			number = parseInt(number / 10);
			size++;
		}
		return size;
	}
	is_circular_prime(number) {
		//Assign the value of how many number of digits are exist in number
		var size = this.digit_length(number);
		var auxiliary = number;
		//Variable which control  execution process
		var remainder = 0;
		var divisor = 0;
		while (this.is_prime(auxiliary) == 1) {
			//Calculate divisor of auxiliary number
			divisor = parseInt(auxiliary / 10);
			//Calculate remainder of auxiliary number
			remainder = auxiliary % 10;
			//permutation calculation
			auxiliary = (parseInt(Math.pow(10, size - 1))) *remainder + divisor;
			//When permutation of number is equal to actual number

			if (auxiliary == number) {
				return 1;
			}
		}
		return 0;
	}
	//Function which are handling the request of number is circular prime or not
	circular_prime(number) {
		if (this.is_circular_prime(number) == 1) {
			//When Yes

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

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

function main(args) {
	var obj = new MyNumber();
	//Test Cases
	obj.circular_prime(71);
	obj.circular_prime(9311);
	obj.circular_prime(23);
	obj.circular_prime(43);
	obj.circular_prime(13)
}
main();

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular 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