Switch case and special number
Write a program by using switch case statement.
a. A bank offer a bonus of 5% for the female account holder if the balance in the account is more than 100000 other wise a bonus of 2% is given. For a male account holder the bank offers a bonus of 4% if the account balance is more than 20000 other wise a bonus of 2% is given. Write a program which accepts the gender of account holder( m – male,F – female) and initial balance amount,calculate the bonus and display the net account balance.
b. wap t o generate a number and check the number generated is a special number or not. (a number is said to be special if the sum of its digits is equal to its digits. For ex – 1142. Sum of its digit = 8 and product of its digits is 8. Plz display a proper message.
import java.util.*; class question5 { void main() { Scanner mas=new Scanner(System.in); System.out.println("press 1 to calculate the bonus and display the net account balance of a account holder"); System.out.println("press 2 to generate a number and check it is special or not"); int ch=mas.nextInt(); switch(ch) { case 1: System.out.println("enter the gender of the account holder(M,m or F,f)"); char a=mas.next().charAt(0); System.out.println("enter the initial balance amount"); int b=mas.nextInt(); if(a=='F'||a=='f') { if(b>100000) { double c=b*0.05; System.out.println("bonus="+c); System.out.println("net balance="+(c+b)); } else { double d=b*0.02; System.out.println("bonus="+d); System.out.println("net balance="+(d+b)); } } else if(a=='m'||a=='M') { if(b>20000) { double e=b*0.04; System.out.println("bonus="+e); System.out.println("net balance="+(e+b)); } else { double f=b*0.02; System.out.println("bonus="+f); System.out.println("net balance="+(f+b)); } } else { System.out.println("Invalid gender"); } break; case 2: int num=(int)((Math.random()*9000)+1000); System.out.println("number generated="+num); int dgt,pro=1,sum=0,g=num; for(;num!=0;num/=10) { dgt=num%10; sum+=dgt; pro*=dgt; } if(sum==pro) System.out.println("special number="+g); else System.out.println("not a special number="+g); break; default : System.out.println("Invalid choice"); } } }
Comments
Post a Comment