Write a program to find Number is Prime number or not using for loop
Input:
Enter the Number 12
output:
Number is Not prime
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
No comments:
Post a Comment
If you have any doubts, please let me know