switch case binary and till 9 check composite
Write a menu driven program for the following:-
a. to input a number and change it into binary. Ex -65, output – 1000001
b. Write a program to take input numbers as long the user wants to take the input. The input will terminate only if the user enters a multiple of 9. Find the sum of all the numbers taken as input. Check the sum of all the numbers is composite or not.
import java.util.*; class Pratyaksh_17AugQ7 { public static void main() { Scanner in=new Scanner(System.in); System.out.println("press 1 to convert decimal to binary"); System.out.println("press 2 input number till the one of input number is multiple of 9"); int choice=in.nextInt(); switch(choice) { case 1: int last; long binary=0; System.out.println("Enter a number in base 10"); int a = in.nextInt(); for(int i=0;a>0;i++) { last= a%2; a=a/2; binary=(long)Math.pow(10,i)*last + binary; } System.out.println("Given number in base 2 "+binary); break; case 2: System.out.print("\nEnter a number "); int num,sum; sum=num=in.nextInt(); for(;num%9!=0;sum+=num) { System.out.print("\nEnter a number again "); num=in.nextInt(); } System.out.println("Sum of numbers are "+sum); for(int i=2;i<sum;i++) if(sum%i==0) num=0; if(num==0) System.out.println("Sum of numbers are composite"); else System.out.println("Sum of numbers are not composite"); break; default : System.out.println("Invalid choice"); } } }
Comments
Post a Comment