Prime Number Program Using Java With I/O Stream

Introduction

In this blog, I will explain about prime number program, using Java. It is very simple in Java programming. The output will be displayed in the Run module.

Software Requirement

JDK1.3.

Simple Program

  1. import java.io.*;  
  2.   
  3. class prime  
  4. {  
  5.   public static void main(String arg[]) throws IOException  
  6.   {  
  7.     int i,n;  
  8.     String s;   
  9.   
  10.     DataInputStream dr = new DataInputStream(System.in);  
  11.   
  12.     System.out.println("Enter the Number : ");  
  13.     s = dr.readLine();  
  14.     n = Integer.parseInt(s);         
  15.       
  16.      i=2;  
  17.   
  18.      while(i<=n-1)  
  19.       {  
  20.        if(n%i==0)  
  21.         {   
  22.           System.out.println("The given number is not a Prime");  
  23.         }  
  24.        i++;  
  25.       }  
  26.   
  27.        if(n==i)  
  28.         {   
  29.           System.out.println("The given number is Prime");  
  30.         }  
  31.          
  32.       }  
  33.     } 
Explanation

In this blog, I explained about prime number program, using Java. The output will be displayed in the Run module.

Output

Ebook Download
View all
Learn
View all