Prime Number

Java program to check given number is prime or not using for, while, do-while loop

There are different ways to identify prime number in Java.

  1. Write a java program to check a number is prime number or not using while loop.
  2. Write a java program to check a number is prime number or not using for loop.
  3. Write a java program to check a number is prime number or not using do-while .
  4. Java program to display prime numbers from 1 to N
  5. Java program to display prime numbers from Given Range


Input:
Enter the Number  17
output:
Number is prime

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 number: ");
int number=scan.nextInt();
int count=0;int i=1;
//condition used to identify number is prime or not
while(i<=number)
{
if(number%i==0)
{
count++;
} i++;
}
if(count==2)
{
System.out.println(number + " is Prime number");
}
else
{
System.out.println(number + " is not Prime number");
}
}
}
Output:
Enter the number: 
96
96 is not Prime number




Input:
Enter the Number  12
output:
Number is  Not prime

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 number: ");
int number=scan.nextInt();
int count=0;
//condition used to identify number is prime or not
for(int i=1; i<=number; i++)
{
if(number%i==0)
{
count++;
}
}
if(count==2)
{
System.out.println(number + " is Prime number");
}
else
{
System.out.println(number + " is not Prime number");
}
}
}
Output:
Enter the number: 
51
51 is not Prime number




Input:
Enter the Number  17
output:
Number is prime

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 number: ");
int number=scan.nextInt();
int count=0;int i=1;
//condition used to identify number is prime or not
do
{
if(number%i==0)
{
count++;
} i++;
}while(i<=number);
if(count==2)
{
System.out.println(number + " is Prime number");
}
else
{
System.out.println(number + " is not Prime number");
}
}
}
Output:
Enter the number: 
67
67 is Prime number





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





Input: Enter the First number: 
20
Enter the Last number: 
50
Output: Prime nos from Given Range :
23
29
31
37
41
43
47
no of prime number in list: 7

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 First number: ");
int first=scan.nextInt();
System.out.println("Enter the Last number: ");
int last=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=first;temp<=last;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();
System.out.println("no of prime number in list: " + count2);
}
}
Output:
Enter the First number: 
20
Enter the Last number: 
50
Prime nos from Given Range :
23
29
31
37
41
43
47
no of prime number in list: 7










No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32