## Program to Find Prime & Sum Of Prime is prime or not.

**Here is my problem statement:**

**A prime number (or a prime) is a natural number greater than 1 that can only be divided by 1 and itself.**

Ex-First few Prime number are-

2, 3, 5, 7, 11, 13, 17, 19,...

Ex-First few Prime number are-

2, 3, 5, 7, 11, 13, 17, 19,...

A Digit Prime is a prime number whose sum of digits is also prime.

For example the prime number 23 is a Digit prime because 2+3=5 and 5 is a prime number. 17 is not a Digit prime because 1+7 = 8, and 8 is not a prime number.

A Digit Prime is a prime number whose sum of digits is also prime.

For example the prime number 23 is a Digit prime because 2+3=5 and 5 is a prime number. 17 is not a Digit prime because 1+7 = 8, and 8 is not a prime number.

**Write a program to find out the Digit Prime Number within a certain range at specified position.and print the Digit Prime Number of specified position within the range.**

**Input:**

Input three positive integers : lower limit , upper limit , and pos is the position of the required digit prime number

Input three positive integers : lower limit , upper limit , and pos is the position of the required digit prime number

**Out Put is as following :**

.

Source code:

import java.io.*;

class PrimesAndSum

{

public static void main(String args[]) throws IOException

{

int low,up,pos;

int sum,temp,num;

int sumPrimeNo=0;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the Lower Limit : ");

low=Integer.parseInt(br.readLine());

System.out.println("Enter the Uper Limit : ");

up=Integer.parseInt(br.readLine());

System.out.println("Enter the Position : ");

pos=Integer.parseInt(br.readLine());

boolean found = false;

boolean isPrime = true;

boolean isSumPrime = true;

for(int i=low;i<=up;i++)

{

isPrime = true;

for(int j=2; j <= i/2; j++)

{

if((i % j) == 0)

{

isPrime = false;

break;

}

}

if(isPrime)

{

num=i;

sum=0;

while(num>0)

{

temp=num%10;

num=num/10;

sum=sum+temp;

}

isSumPrime = true;

for(int k=2; k <= sum/2; k++)

{

if((sum % k) == 0)

{

isSumPrime = false;

break;

}

}

if(isSumPrime)

{

sumPrimeNo++;

if(sumPrimeNo==pos)

{

found=true;

System.out.println("Number "+ i +" is Prime.Sum of this prime is also prime: "+sum+" Number is at position: "+ sumPrimeNo);

}

//System.out.println("Number "+ i +" is Prime.Sum of this prime is also prime: "+sum+" But it is at position: "+ sumPrimeNo);

}

else

{

//System.out.println("Numer " +i+" is prime.But Sum of prime is not prime "+sum);

}

}

}

if(!found)

{

System.out.println("No match");

}

}

}

class PrimesAndSum

{

public static void main(String args[]) throws IOException

{

int low,up,pos;

int sum,temp,num;

int sumPrimeNo=0;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the Lower Limit : ");

low=Integer.parseInt(br.readLine());

System.out.println("Enter the Uper Limit : ");

up=Integer.parseInt(br.readLine());

System.out.println("Enter the Position : ");

pos=Integer.parseInt(br.readLine());

boolean found = false;

boolean isPrime = true;

boolean isSumPrime = true;

for(int i=low;i<=up;i++)

{

isPrime = true;

for(int j=2; j <= i/2; j++)

{

if((i % j) == 0)

{

isPrime = false;

break;

}

}

if(isPrime)

{

num=i;

sum=0;

while(num>0)

{

temp=num%10;

num=num/10;

sum=sum+temp;

}

isSumPrime = true;

for(int k=2; k <= sum/2; k++)

{

if((sum % k) == 0)

{

isSumPrime = false;

break;

}

}

if(isSumPrime)

{

sumPrimeNo++;

if(sumPrimeNo==pos)

{

found=true;

System.out.println("Number "+ i +" is Prime.Sum of this prime is also prime: "+sum+" Number is at position: "+ sumPrimeNo);

}

//System.out.println("Number "+ i +" is Prime.Sum of this prime is also prime: "+sum+" But it is at position: "+ sumPrimeNo);

}

else

{

//System.out.println("Numer " +i+" is prime.But Sum of prime is not prime "+sum);

}

}

}

if(!found)

{

System.out.println("No match");

}

}

}

- Save the file with name PrimesAndSum.java
- Complie with: javac PrimesAndSum.java
- Run with: java PrimesAndSum

## No comments:

## Post a Comment