Monday 31 August 2020

prime numbers from 1 to N

Write a Java program to find Number is Prime number or not from 1 To N.

Input: 
Enter the N: 20
Output:
Prime nos from Given Range :
2
3
5
7
11
13
17
19
no of prime number in list: 8

Code In java:
import java.util.Scanner;
public class Prime{
public static void main(String[] args) {
Scanner scan = new java.util.Scanner(System.in);
System.out.println("Enter the N: ");
int N=scan.nextInt();
int count,count2=0,temp;
System.out.println("Prime nos from Given Range :");
//condition used to identify number is odd or even
for(temp=1;temp<=N;temp++)
                {
count=0;
for(int j=1; j<=temp; j++)
                        {
if(temp%j==0)
                                {
count++;
}
}
if(count==2)
                        
System.out.println(temp);
count2++;
}
}
                 System.out.println("no of prime number in list: " + count2);
}
}
Output:
Enter the N: 
20
Prime nos from Given Range :
2
3
5
7
11
13
17
19
no of prime number in list: 8



No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32