C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to determine whether a given number is a twisted prime numberA number is called a twisted prime number if it is a prime number and reverse of this number is also a prime number. Examples: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79 The algorithm to check whether a given number is a twisted prime number is given below. Algorithm
Java Programimport java.util.*; class Twisted_Prime { public static void main(String[] args) { int n, reverse,sum=0 , flag; Scanner sc = new Scanner(System.in); System.out.println("Enter the prime number?"); n = sc.nextInt(); while(n!=0) { reverse = n%10; sum = sum*10 + reverse; n= n/10; } flag = 0; for (int j = 2; j <= sum / 2; j++) { if ((sum % j) == 0) { flag = 1; break; } } if (flag == 0) System.out.println("Twisted Prime"); else System.out.println("Not Twisted Prime"); } } Output: Enter the prime number? 43 Not Twisted Prime Python Programn=int(input("Enter the prime number?")) rev=0 flag=0 while(n>0): dig=n%10 rev=rev*10+dig n=n//10 for j in range(2, rev//2): if ((rev % j) == 0): flag = 1 break if (flag == 0): print("Twisted Prime") else: print("Not Twisted Prime") Output: Enter the prime number? 13 Twisted Prime C Program#include <stdio.h> int main() { int n, reverse,sum=0 , flag; printf("Enter the prime number?"); scanf("%d",&n); while(n!=0) { reverse = n%10; sum = sum*10 + reverse; n= n/10; } printf("\n"); flag = 0; for (int j = 2; j <= sum / 2; j++) { if ((sum % j) == 0) { flag = 1; break; } } if (flag == 0) printf("Twisted Prime"); else printf("Not Twisted Prime"); } Output: Enter the prime number? 11 Twisted Prime C# programusing System; public class PrimeNumber { public static void Main() { int n, reverse,sum=0 , flag; Console.WriteLine("Enter the prime number?"); n = Convert.ToInt32(Console.ReadLine()); while(n!=0) { reverse = n%10; sum = sum*10 + reverse; n= n/10; } flag = 0; for (int j = 2; j <= sum / 2; j++) { if ((sum % j) == 0) { flag = 1; break; } } if (flag == 0) Console.WriteLine("Twisted Prime"); else Console.WriteLine("Not Twisted Prime"); }} Output: Enter the prime number? 13 Twisted Prime PHP Program<?php echo "Enter the prime number?"; $n=readline(); $sum=0; while($n!=0) { $reverse = $n%10; $sum = $sum*10 + $reverse; $n= $n/10; } $flag = 0; for ($j = 2; $j <= $sum / 2; $j++) { if (($sum % $j) == 0) { $flag = 1; break; } } if ($flag == 0) echo ("Twisted Prime"); else echo("Not Twisted Prime"); ?> Output: Enter the prime number? 17 Twisted Prime
Next Topic#
|