Write a Java program that prompts the user for an integer and then prints out all prime numbers up to that integer.

//Program to print prime numbers upto given integer
import java.util.*;
class Prime
{
            public static void main(String args[])
            {
                        int n,f;
                        Scanner scr=new Scanner(System.in);
                        System.out.println(“nEnter n value:  “);
                        n=scr.nextInt();
                        System.out.println(“nPrimenumbers are : “);
                        for(int i=2;i<=n;i++)
                        {
                                    f=0;
                                    for(int j=2;j<=i/2;j++)
                                    if((i%j)==0)
                                    {
                                                f=1;
                                                break;
                                    }
                        if(f==0)
                        System.out.print(i+”   “);
                        }
            }
}
                       
Output:
Enter n value:
10
Prime numbers are :

2   3   5   7

Leave a Reply

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

Enable Notifications OK No thanks