TheDeveloperBlog.com

Home | Contact Us

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 number

Program to determine whether a given number is a twisted prime number on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

Program to determine whether a given number is a twisted prime number

A 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

  • STEP 1: START
  • STEP 2: DEFINE n, reverse, flag
  • STEP 3: SET sum = 0
  • STEP 4: ENTER n
  • STEP 5: REPEAT STEP 6 to 8 UNTIL (n!=0)
  • STEP 6: reverse = n%10
  • STEP 7: sum = sum*10 + reverse
  • STEP 8: n = n/10
  • STEP 9: SET flag =0
  • STEP 10: REPEAT STEP 11 UNTIL j<=sum/2
  • STEP 11: if(sum%j)==0
    then
    set flag = 1
    break
  • STEP 12: if(flag==0)
    then PRINT "yes"
    else
    PRINT "no"
  • STEP 13: END

Java Program

import 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 Program

n=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# program

using 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#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf