Categories

# ICSE Computer Application Function, Decision Making, Loops Question

Write a menu-driven program in Java to perform the following tasks as per user’s choice by designing individual methods for each of the choices (choice 1 to 8) and finally calling them from main() as per user’s choice:

calculator(): A method that performs the operations just the way a hand-held calculator does.
unique(): check whether an integer input is a unique number or not and returns proper signal. A unique number is the number in which the digits are not repeated. Example: 32480.
twinPrime(): checks whether two integer inputs are twin primes or not. 3 and 5, 11 and 13, 29 and 31 are twin primes.
mercenne(): checks whether an integer input is a Mercenne number or not. It is a number that can be expressed as 2n – 1.
series1(): prints the given series up to nth term: 1/+2, 2/-3, 6/-5, 24/-7, 120/+11
series2(): prints the given series 1111111, 111111, 11111, 1111, 111, 11, 1.
series3(): prints the given series and its result: + (2*3*4)/(1+2+3) + (3*4*5)/(2+3+4) + (4*5*6)/(3+4+5) … nth term.

``````import java.util.Scanner;
class MyProject{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("1. CALCULATOR");
System.out.println("2. UNIQUE");
System.out.println("3. DISARIUM");
System.out.println("4. TWIN-PRIME");
System.out.println("5. MERCENNE");
System.out.println("6. SERIES 1");
System.out.println("7. SERIES 2");
System.out.println("8. SERIES 3");
int choice = Integer.parseInt(in.nextLine());
switch(choice){
case 1:
calculator();
break;
case 2:
System.out.print("Enter the integer: ");
int num = Integer.parseInt(in.nextLine());
if(unique(num))
System.out.println(num + " is unique.");
else
System.out.println(num + " is NOT unique.");
break;
case 3:
System.out.print("Enter the integer: ");
num = Integer.parseInt(in.nextLine());
disarium(num);
break;
case 4:
System.out.print("Enter the first number: ");
int m = Integer.parseInt(in.nextLine());
System.out.print("Enter the second number: ");
int n = Integer.parseInt(in.nextLine());
twinPrime(m, n);
break;
case 5:
System.out.print("Enter the number: ");
num = Integer.parseInt(in.nextLine());
mercenne(num);
break;
case 6:
System.out.print("N = ");
n = Integer.parseInt(in.nextLine());
series1(n);
break;
case 7:
series2();
break;
case 8:
System.out.print("N = ");
n = Integer.parseInt(in.nextLine());
series3(n);
break;
default:
System.out.println("Invalid Choice!");
}
}
public static void calculator(){
Scanner in = new Scanner(System.in);
boolean start = true;
double y = 0.0;
double result = 0.0;
loop:
while(true){
if(start){
System.out.print("Operand: ");
result = Double.parseDouble(in.nextLine());
}
System.out.print("Operator: ");
char op = in.nextLine().charAt(0);
if(op != '='){
System.out.print("Operand: ");
y = Double.parseDouble(in.nextLine());
}
switch(op){
case '+':
if(start){
result = result + y;
start = false;
}
else
result += y;
break;
case '-':
if(start){
result = result - y;
start = false;
}
else
result -= y;
break;
case '*':
case 'x':
case 'X':
if(start){
result = result * y;
start = false;
}
else
result *= y;
break;
case '/':
if(start){
result = result / y;
start = false;
}
else
result /= y;
break;
case '%':
if(start){
result = result % y;
start = false;
}
else
result %= y;
break;
case '=':
System.out.println("Result: " + result);
break loop;
default:
System.out.println("Invalid Operator!");
}
}
}
public static boolean unique(int num){
boolean status = true;
for(int i = num; i != 0; i /= 10){
int digit = i % 10;
for(int j = i / 10; j != 0; j /= 10){
int d = j % 10;
if(digit == d)
status = false;
}
}
return status;
}
public static void disarium(int num){
int p = 0;
int sum = 0;
for(int i = num; i != 0; i /= 10)
p++;
for(int i = num; i != 0; i /= 10){
sum += (int)Math.pow(i % 10, p);
p--;
}
if(num == sum)
System.out.println(num + " is a Disarium Number.");
else
System.out.println(num + " is NOT a Disarium Number.");
}
public static void twinPrime(int x, int y){
if(Math.abs(x - y) == 2 && prime(x) && prime(y))
System.out.println(x + " and " + y + " are twin primes.");
else
System.out.println(x + " and " + y + " are NOT twin primes.");
}
public static boolean prime(int n){
int f = 0;
for(int i = 1; i <= n; i++){
if(n % i == 0)
f++;
}
return f == 2;
}
public static void mercenne(int num){
int n = 1;
while(Math.pow(2, n) - 1 < num)
n++;
if(Math.pow(2, n) - 1 == num)
System.out.println(num + " is a Mercenne Number.");
else
System.out.println(num + " is NOT a Mercenne Number.");
}
public static void series1(int n){
int num = 1;
int den = 2;
int count = 2;
int sign = 1;
for(int i = 1; i <= n; i++){
if(sign == 1)
System.out.print(num + "/+" + den + " ");
else
System.out.print(num + "/-" + den + " ");
num *= count;
count++;
den++;
while(!prime(den))
den++;
if(sign == 1)
sign = -1;
else
sign = 1;
}
System.out.println();
}
public static void series2(){
for(int i = 7; i >= 1; i--){
int term = ((int)Math.pow(10, i) - 1) / 9;
System.out.print(term + "\t");
}
System.out.println();
}
public static void series3(int n){
double sum = 0.0;
int x = 2;
int y = 1;
for(int i = 1; i <= n; i++){
double term = (double)(x * (x + 1) * (x + 2)) / (y + (y + 1) + (y + 2));
sum += term;
System.out.print("+" + x + "*" + (x + 1) + "*" + (x + 2) + "/" + y + "+" + (y + 1) + "+" + (y + 2) + " ");
x++;
y++;
}
System.out.println("\nResult = " + sum);
}
}``````

## 2 replies on “ICSE Computer Application Function, Decision Making, Loops Question”

sayan ranasays:

Sir,a program is given which states that:
WAP in Java to accept n integers in SDA(single dimensional array). Now remove all the prime numbers from the array and display the resultant array.
Sample Input:
N=10
1
2
3
4
5
6
7
8
9
10
Resultant array:
1 4 6 8 9 10

sayan ranasays:

Sir,a program is given which states that:
WAP in Java to accept n integers in SDA(single dimensional array). Now remove all the prime numbers from the array and display the resultant array.

This site uses Akismet to reduce spam. Learn how your comment data is processed.