Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class hw4 {
  4.  
  5.     public static void main(String[] args) {
  6.         for(;;)
  7.         {
  8.             Scanner kb = new Scanner(System.in);
  9.             int N,option;
  10.             System.out.println("Enter N");
  11.             N = kb.nextInt();
  12.             System.out.println("Choose option");
  13.             System.out.println("1- Sum of numbers from 1 to N\n2- Perfect squares from 1 to N\n3- Prime numbers from 2 to N\n4- sum, average, min, and max.\n5- Exit");
  14.             option = kb.nextInt();
  15.             int sum=0,max=0,min=0,avg=0;
  16.             if (N >= 1){
  17.                 max = N;
  18.                 min = 1;    }
  19.             else {
  20.                 max = 1;
  21.                 min = N;   
  22.             }
  23.             for (int i = 1;i <= N; i++)
  24.             {
  25.                 sum += i;
  26.                 avg = avg + 1;
  27.             }
  28.             switch(option)
  29.             {
  30.             case(1):
  31.                 System.out.println("The Sum is : " + sum);break;
  32.             case(2):
  33.                 System.out.println("The Perfect numbers are :");
  34.             double x;
  35.             for (int i = 1;i <= N; i++)
  36.             {
  37.                 x = Math.sqrt(i);
  38.                 if (x - (int)x == 0)
  39.                 System.out.println(i );    
  40.             }
  41.             break;
  42.             case(3):
  43.                 System.out.println("The Prime numbers are :");
  44.             for (int i = 1;i <= N; i++)
  45.             {
  46.             if (isPrime(i) == true)
  47.                 System.out.println(i);
  48.             }
  49.             break;
  50.             case(4):
  51.                 System.out.println("The Sum is : " + sum);
  52.             System.out.println("The Avg is : " + ((float)sum/avg));
  53.             System.out.println("The Max is : " + max);
  54.             System.out.println("The Min is : " + min);
  55.             break;
  56.             case(5):
  57.                 System.exit(1);
  58.             break;
  59.             default: System.out.println("Choose a right option");
  60.                
  61.             }
  62.             System.out.println("=======================================================");
  63.         }
  64.     }
  65.    
  66.        public static boolean isPrime(int n) {  
  67.            if (n <= 1) {  
  68.                return false;  
  69.            }  
  70.            for (int i = 2; i <= Math.sqrt(n); i++) {  
  71.                if (n % i == 0) {  
  72.                    return false;  
  73.                }  
  74.            }  
  75.            return true;  
  76.        }  
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement