Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- GTU Practical EXERCISES
- 29th March,2023
- Karishma and Nency
- Write a program in Java to generate first n prime numbers
- input n=7
- output output 2,3,5,7,11,13,17
- */
- import java.util.Scanner;
- class q5
- {
- //this method check given arg is prime or not
- static int prime(int n)
- {
- for(int i=2;i<n;i++)
- {
- if(n%i==0)
- return 1;
- }
- return 0;
- }
- public static void main(String args[])
- {
- Scanner sc=new Scanner(System.in);
- int n,cnt=2,i=0;
- System.out.print("Enter number=>");
- n=sc.nextInt();
- while(true)
- {
- if(prime(cnt)==0)
- {
- System.out.println(cnt);
- i++;
- }
- if(i==n)
- {
- break;
- }
- cnt++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement