Question

JAVA (programing) 1. For a given positive integer n, output the first n primes. E.g. n=3,...

JAVA (programing)

1. For a given positive integer n, output the first n primes. E.g. n=3, output: 2,3,5; n=7, output: 2,3,5,7,11,13,17.

2. For a given integer n>1, list all primes not exceeding n. E.g. n=10, output: 2,3,5,7; n=16, output: 2,3,5,7,11,13.

3.For a given integer n>1, output its prime factorization. E.g. n=8, output: 2^3; n=72, output: 2^3*3^2.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)
import java.util.*;
public class first_nprimes {
static List<Integer>alist=new ArrayList<Integer>();
static Scanner sc=new Scanner(System.in);
   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       System.out.println("Enter limit for prime numbers:");
       int limit=sc.nextInt();
       int count=0;
       for(int i=1;i<=limit;i++){
       for(int j=1;j<=i;j++){
           if(i%j==0){
               count++;
           }
       }
       if(count==2)
       alist.add(i);
       count=0;
       }
       printnprimes();
   }
   private static void printnprimes() {
       System.out.println("enter n value:");
       int n=sc.nextInt();
       for(int i=0;i<n;i++)
           System.out.print(alist.get(i)+" ");
      
      
   }
}
output:
Enter limit for prime numbers:
50
enter n value:
7
2 3 5 7 11 13 17




2)
import java.util.*;
public class notexceding {
static List<Integer>alist=new ArrayList<Integer>();
static Scanner sc=new Scanner(System.in);
   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       System.out.println("Enter limit for prime numbers:");
       int limit=sc.nextInt();
       int count=0;
       for(int i=1;i<=limit;i++){
       for(int j=1;j<=i;j++){
           if(i%j==0){
               count++;
           }
       }
       if(count==2)
       alist.add(i);
       count=0;
       }
       printnotexceeding();
   }
   private static void printnotexceeding() {
       System.out.println("enter n value:");
       int n=sc.nextInt();
       for(int i=0;i<alist.size();i++){
           if(alist.get(i)<n)
           System.out.print(alist.get(i)+" ");
          
       }
      
      
   }
}

output
Enter limit for prime numbers:
50
enter n value:
16
2 3 5 7 11 13

3)
import java.util.*;
public class Prime_Factorization {
   static Set<Integer>alist=new HashSet<Integer>();
static Scanner sc=new Scanner(System.in);
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       System.out.println("enter n:");
       int n=sc.nextInt();
       primefactorizationmethod( n);
   }
   private static void primefactorizationmethod(int n) {
       // TODO Auto-generated method stub
       int da=0;
       for(int i=2;i<n;i++){
           if(n%i==0){
               if(i%2==0){
                   da=2;
               }else if(i%3==0){
                   da=3;
               }else
                   da=i;
               alist.add(da);
           }
       }
       Iterator i=alist.iterator();
       while(i.hasNext()){
           System.out.print(i.next()+" ");
       }
   }
}

output:

enter n:
72
2 3

Add a comment
Know the answer?
Add Answer to:
JAVA (programing) 1. For a given positive integer n, output the first n primes. E.g. n=3,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT