CHAPTER-12
PG-214,215
1. Using the switch statement, write a menu driven program to:
(i) To find and display all the factors of a number input by the user
(including 1 and excluding number itself).
Example:
Sample Input: n=15
Sample Output: 1, 3, 5.
(ii) To find and display the factorial of a number input by the user (the
factorial of a non-negative integer n, denoted by n\ is the product of all
integers less than or equal to n.
Example:
Sample Input: n=5
Sample Output: 5! = 1×2×3×4×5 = 120.
For an incorrect choice, an appropriate error message should be displayed.
Programming code
import java.util.*;
public class menu1
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println(" 1. Factor of number ");
System.out.println(" 2. Factorial of number ");
System.out.println(" Enter your choice ");
int ch = sc.nextInt();
System.out.println(" Enter number ");
int n = sc.nextInt();
switch(ch)
{
case 1:
for (int i=1;i<n-1;i++)
{
if (n%i==0)
System.out.print(i);
}
break;
case 2:
int F=1;
for (int i=1;i<=n;i++)
{
F=F*i;
}
System.out.print(" Factorail is " +F);
break;
default :
System.out.print(" Incorrect Choice " );
}
}
}
Output
1. Factor of number
2. Factorial of number
Enter your choice
2
Enter number
5
Factorial is 120
2. Using the switch statement, write a menu driven program to calculate the
maturity amount of a Bank Deposit.
The user is given the following options:
1. Term Deposit
2. Recurring Deposit
For option 1, accept principal (P), rate of interest(r) and time period in years(n).
Calculate and output the maturity amount(A) receivable using the formula:
A = P[1 + r / 100]n
For option 2, accept Monthly Installment (P), rate of interest (r) and time period
in months (n). Calculate and output the maturity amount (A) receivable using the
formula:
A = P x n + P x (n(n+1) / 2) x r / 100 x 1 / 12
For an incorrect option, an appropriate error message should be displayed.
Programming code
import java.util.Scanner;
public class BankDeposit
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Term Deposit");
System.out.println("Type 2 for Recurring Deposit");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
double p = 0.0, r = 0.0, a = 0.0;
int n = 0;
switch (ch)
{
case 1:
System.out.print("Enter Principal: ");
p = in.nextDouble();
System.out.print("Enter Interest Rate: ");
r = in.nextDouble();
System.out.print("Enter time in years: ");
n = in.nextInt();
a = p * Math.pow(1 + r / 100, n);
System.out.println("Maturity amount = " + a);
break;
case 2:
System.out.print("Enter Monthly Installment: ");
p = in.nextDouble();
System.out.print("Enter Interest Rate: ");
r = in.nextDouble();
System.out.print("Enter time in months: ");
n = in.nextInt();
a = p * n + p * ((n * (n + 1)) / 2) * (r / 100) * (1 / 12.0);
System.out.println("Maturity amount = " + a);
break;
default:
System.out.println("Invalid choice");
}
}
}
Output
Type 1 for Term Deposit
Type 2 for Recurring Deposit
Enter your choice: 1
Enter Principal: 10000
Enter Interest Rate: 8
Enter time in years: 2
Maturity amount = 11664.000000000002
3. Using the switch statement, write a menu driven program:
1. To check and display whether a number input by the user is a composite
number or not.
A number is said to be composite, if it has one or more than one factors
excluding 1 and the number itself.
Example: 4, 6, 8, 9…
2. To find the smallest digit of an integer that is input:
Sample input: 6524
Sample output: Smallest digit is 2
For an incorrect choice, an appropriate error message should be displayed.
Programming code
import java.util.Scanner;
public class Number
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Composite Number");
System.out.println("Type 2 for Smallest Digit");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch)
{
case 1:
System.out.print("Enter Number: ");
int n = in.nextInt();
int c = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
c++;
}
if (c > 2)
System.out.println("Composite Number");
else
System.out.println("Not a Composite Number");
break;
case 2:
System.out.print("Enter Number: ");
int num = in.nextInt();
int s = 10;
while (num != 0)
{
int d = num % 10;
if (d < s)
s = d;
num /= 10;
}
System.out.println("Smallest digit is " + s);
break;
default:
System.out.println("Wrong choice");
}
}
}
Output
Type 1 for Composite Number
Type 2 for Smallest Digit
Enter your choice: 2
Enter Number: 98564
Smallest digit is 4
4. Write a menu driven program to perform the following tasks by using Switch
case statement:
(a) To print the series:
0, 3, 8, 15, 24, ………… to n terms. (value of 'n' is to be an input by the user)
(b) To find the sum of the series:
S = (1/2) + (3/4) + (5/6) + (7/8) + ……….. + (19/20)
Programming code
import java.util.Scanner;
public class SeriesMenu
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Type 1 to print series");
System.out.println("0, 3, 8, 15, 24,....to n terms");
System.out.println();
System.out.println("Type 2 to find sum of series");
System.out.println("(1/2) + (3/4) + (5/6) + (7/8) +....+ (19/20)");
System.out.println();
System.out.print("Enter your choice: ");
int choice = in.nextInt();
switch (choice)
{
case 1:
System.out.print("Enter n: ");
int n = in.nextInt();
for (int i = 1; i <= n; i++)
System.out.print(((i * i) - 1) + " ");
System.out.println();
break;
case 2:
double sum = 0;
for (int i = 1; i <= 19; i = i + 2)
sum += i / (double)(i + 1);
System.out.println("Sum = " + sum);
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
Output
Type 1 to print series
0, 3, 8, 15, 24,....to n terms
Type 2 to find sum of series
(1/2) + (3/4) + (5/6) + (7/8) +....+ (19/20)
Enter your choice: 2
Sum = 8.535515873015873
5. Write a menu driven program to accept a number from the user and check
whether it is a Prime number or an Automorphic number.
(a) Prime number: (A number is said to be prime, if it is only divisible by 1 and
itself)
Example: 3,5,7,11
(b) Automorphic number: (Automorphic number is the number which is
contained in the last digit(s) of its square.)
Example: 25 is an Automorphic number as its square is 625 and 25 is present as
the last two digits.
Programming code
import java.util.Scanner;
public class PrimeAutomorphic
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("1. Prime number");
System.out.println("2. Automorphic number");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter number: ");
int num = in.nextInt();
switch (choice)
{
case 1:
int c = 0;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
{
c++;
}
}
if (c == 2)
System.out.println(num + " is Prime");
else
System.out.println(num + " is not Prime");
break;
case 2:
int numCopy = num;
int sq = num * num;
int d = 0;
while(num > 0)
{
d++;
num /= 10;
}
int ld = (int)(sq % Math.pow(10, d));
if (ld == numCopy)
System.out.println(numCopy + " is automorphic");
else
System.out.println(numCopy + " is not automorphic");
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
Output
1. Prime number
2. Automorphic number
Enter your choice: 1
Enter number: 97
97 is Prime
6. Write a menu driven program to accept a number from the user and check
whether it is a BUZZ number or to accept any two number and print the GCD of
them.
a. A BUZZ number is the number which either ends with 7 or is divisible by 7.
b. GCD (Greatest Common Divisor) of two integers is calculated by continued
division method. Divide the largest number by the smaller ; the remainder
then divides the precious divisor. The process is repeated till the remainder
is zero.The divisor then results the GCD.
Programming code
import java.util.*;
public class Number1
{
public static int gcd (int a, int b)
{
int num, den, GCD = 0, r ;
if (a > b) {
num = a ;
den = b ;
}
else {
num = b ;
den = a ;
}
while(den > 1)
{ r = num % den ;
if(r == 0)
{
GCD = den ;
break ;
}
else {
num = den ;
den = r ;
}
}
if (den == 1)
GCD = 1 ;
return GCD ;
}
public static boolean BUZZ ( int a )
{
if ( a % 7 ==0 || a % 10 == 7) return true ;
else return false ;
}
public static void main( )
{
Scanner kb = new Scanner( System.in );
System.out.println("Menu");
System.out.println("1. BUZZ number");
System.out.println("2. GCD of two numbers");
System.out.println("Enter your choice ");
int ch = kb.nextInt();
if ( ch ==1 ){
System.out.println("Enter a number");
int num = kb.nextInt();
if (BUZZ(num) == true)
System.out.println(num+" is a BUZZ number");
else
System.out.println(num+" is not a BUZZ number");
}
else if ( ch ==2 ){
System.out.println("Enter two numbers ");
int num1 = kb.nextInt();
int num2 = kb.nextInt();
int res = gcd(num1, num2);
System.out.println("GCD of "+num1+" and "+num2+" : "+res);
}
else
System.out.println("Wrong Choice!!");}
}
Output
Menu
1. BUZZ number
2. GCD of two numbers
Enter your choice
1
Enter a number
2401
2401 is a BUZZ number