CL X CH 12 Program
CL X CH 12 Program
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
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
Programming code
import java.util.Scanner;
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:
Programming code
import java.util.Scanner;
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
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
Programming code
import java.util.Scanner;
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;
}
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 ;
}