Advertisement
netx_iit

First N prime number

Mar 28th, 2023
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | Source Code | 0 0
  1. /**
  2.     GTU Practical EXERCISES
  3.     29th March,2023
  4.     Karishma and Nency
  5.     Write a program in Java to generate first n prime numbers
  6.     input n=7
  7.     output output 2,3,5,7,11,13,17
  8. */
  9. import java.util.Scanner;
  10. class q5
  11. {
  12.     //this method check given arg is prime or not
  13.     static int prime(int n)
  14.     {
  15.         for(int i=2;i<n;i++)
  16.         {
  17.             if(n%i==0)
  18.                 return 1;
  19.         }
  20.         return 0;
  21.     }
  22.     public static void main(String args[])
  23.     {
  24.         Scanner sc=new Scanner(System.in);
  25.         int n,cnt=2,i=0;
  26.         System.out.print("Enter number=>");
  27.         n=sc.nextInt();
  28.         while(true)
  29.         {
  30.             if(prime(cnt)==0)
  31.             {
  32.                 System.out.println(cnt);
  33.                 i++;   
  34.             }
  35.             if(i==n)
  36.             {
  37.                 break;
  38.             }
  39.             cnt++;
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement