switch case random prime
Write a program to write a menu driven program for the following :-
a. To input a number and find the sum of the largest and smallest digit. Check the sum of the digits is prime or not.
b. to input a number and check the number is divisible by 9 or not. If not,then what should be added to the number to make it divisible by 9.
c. Write a program to input the number of digits by the user. And then generate a number with the same number of digits.
import java.util.*; class Pratyaksh_17AugQ6 { public static void main() { Scanner in=new Scanner(System.in); System.out.println("press 1 to find the sum of the largest and smallest digit and check prime or not"); System.out.println("press 2 to check the divisibility of 9 and if not make it divisible"); System.out.println("press 3 to generate the random number of given digit"); int choice=in.nextInt(); switch(choice) { case 1: System.out.println("Enter a number"); int s=9,l=0,num; num= in.nextInt(); for(;num>0;num/=10) { if(num%10>l) l=num%10; if(num%10<s) s=num%10; } for(int i=2;i<19;i++) if((l+s) %i==0) num=1; System.out.println("Sum of largest and smallest digit is "+(s+l)); if(num==0) System.out.println("Sum of largest and smallest digit is PRIME"); else System.out.println("Sum of largest and smallest digit is not PRIME"); break; case 2: System.out.println("Enter a number"); int num2= in.nextInt(); if(num2 % 9==0) System.out.println(num2 +" is divisible by 9"); else System.out.println(9-(num2 % 9)+ " should be added to make it divisible by 9"); break; case 3: System.out.println("Enter the number of digits of random number"); int num3= in.nextInt(); System.out.println("Random number is "+(int)(Math.random()*Math.pow(10,num3))); break; default : System.out.println("Invalid choice"); } } }
Comments
Post a Comment