Saturday, 29 August 2020

prime number or not (using do-while loop).

Write a program to find Number is Prime number or not using do-while loop

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



No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32