Print prime numbers from 1 to 100 in Java.

Home / Uncategorized / Print prime numbers from 1 to 100 in Java.

 

Print prime numbers from 1 to 100 in Java.
import java.util.Scanner;
public class PrimeNumbers
{
public static void main(String[] args)
{
int count=0;
System.out.print(“Enter any number: “);
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
for(int i=2;i<num;i++)
{
if(num%i==0)
{
count++;
}
}
if(count==0)
{
System.out.println(“This number is Prime.”);
}
else
{
System.out.println(“This number is not Prime.”);
}
}
}

Output:-

Enter any number: 5
This number is Prime.

Leave a Reply

Your email address will not be published. Required fields are marked *