Write a Java program to print all the prime numbers upto a number entered by the user.

                       
Image is Unavailable

Algorithm

  1. Start
  2. x:=0
  3. If x<=1, go to step 4 or else go to step 9
  4. Print "Enter a number"
  5. Read x
  6. If x<=1, go to step 7 or else go to step 8
  7. Print "Invalid Input. Try Again."
  8. Go to step 3
  9. Print "The Prime Numbers Are:"
  10. j=2
  11. If j<=x, go to step 12 or else go to step 22
  12. a=j
  13. count=0
  14. i=2
  15. If  i<a, go to step 16 or else go to step 19
  16. If a%i==0, go to step 17 or else go to step 18
  17. count++
  18. i++. Go to step 15
  19. If count==0, go to step 20 or else go to step 21
  20. Print j
  21. j++. Go to step 11
  22. Stop

Program

Buffered Reader

import java.io.*;
class PrimeList
{
    int x;
    
    void accept() throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(x<=1)
        {
            System.out.println("Enter a number");
            x=Integer.parseInt(br.readLine());
            if(x<=1)
            {
                System.out.println("Invalid Input. Try Again");
            }
        }
    }
    
    boolean isPrime(int a)
    {
        int count=0;
        for(int i=2;i<a;i++)
        {
            if(a%i==0)
            {
                count++;
            }
        }
        if(count==0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    void printList()
    {
        System.out.println("The Prime Numbers Are:");
        for(int j=2;j<=x;j++)
        {
            if(isPrime(j)==true)
            {
                System.out.println(j);
            }
        }
    }
    
    void main() throws IOException
    {
        accept();
        printList();
    }
}

Scanner

import java.util.*;
class PrimeList
{
    int x;
    
    void accept() 
    {
        Scanner sc=new Scanner(System.in);
        while(x<=1)
        {
            System.out.println("Enter a number");
            x=sc.nextInt();
            if(x<=1)
            {
                System.out.println("Invalid Input. Try Again");
            }
        }
    }
    
    boolean isPrime(int a)
    {
        int count=0;
        for(int i=2;i<a;i++)
        {
            if(a%i==0)
            {
                count++;
            }
        }
        if(count==0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    void printList()
    {
        System.out.println("The Prime Numbers Are:");
        for(int j=2;j<=x;j++)
        {
            if(isPrime(j)==true)
            {
                System.out.println(j);
            }
        }
    }
    
    void main() 
    {
        accept();
        printList();
    }
}

Documentation

Variable Description

 Data Type Variable Name Description
 intTo accept a number from the user. 
 intcount To count the number of factors except 1 and itself 
 int To act as a counter in loop 
 intTo act as a counter in loop 
 intTo parse value to method. 

Method Description

 Return TypeMethod Name Description 
 voidaccept() To accept data from the user 
 booleanisPrime(int) To check if the argument is Prime or not 
 void printList() To print the list of  prime numbers 
 voidmain() To act as an entry gateway for the compiler 

Output

Image is Unavailable
Image is Unavailable
Share with all ISC Computer Science Students you know.

Comment below with all your program requests and blog suggestions.

Follow us on social media by clicking on these links. 
Instagram : @IscJavaCode
Twitter: @IscCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode


Stay Healthy
Stay Happy
Stay Helpful



Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Emirp Number | Write a program in Java to print an Emirp Number. | ISC Computer Science | ICSE Computer Applications

Conversion of Complex Sentences to Compound Sentences