Computer_Applications_Type3_switch_case
Computer_Applications_Type3_switch_case
[2015]
import java.util.Scanner;
// Display menu
System.out.println("Menu:");
System.out.println("1. Find factors of a number");
System.out.println("2. Find factorial of a number");
System.out.print("Enter your choice (1 or 2): ");
int choice = sc.nextInt();
case 2:
// Calculate and display factorial of the number
int fact = 1;
for (int i = 1; i <= n; i++)
{
fact = fact * i;
}
System.out.println(n + "! = " + fact);
break;
default:
// Display error message for incorrect choice
System.out.println("Invalid choice! Please select 1 or 2.");
}
}
}
Explanation:
1. Menu Display:
o The program displays a menu with two options:
Option 1: Find factors of a number.
Option 2: Find factorial of a number.
o The user is asked to enter their choice (1 or 2).
2. Input Number:
o The user is prompted to enter a number (n) for which they want to
perform the operation.
3. Switch Statement:
o The switch statement handles the user's choice:
Case 1: Finds and displays factors of the number.
A loop runs from 1 to n-1. If n is divisible by the
current number (i), it is printed as a factor.
Case 2: Calculates and displays the factorial of the number.
A loop multiplies all numbers from 1 to n to compute the
factorial.
Default: If the user enters an invalid choice, an error
message is displayed.
[2016]
import java.util.Scanner;
// Display menu
System.out.println("Menu:");
System.out.println("1. Print Floyd's Triangle");
System.out.println("2. Display Pattern");
System.out.print("Enter your choice (1 or 2): ");
int choice = sc.nextInt();
case 2:
// Display the given pattern
String s = "ICSE";
for(int i = 0; i <= s.length()-1; i++)
{
for(int j = 0 ; j <= i; j++)
{
System.out.print(s.charAt(j));
}
System.out.println();
}
break;
default:
// Display error message for incorrect choice
System.out.println("Invalid choice! Please select 1 or 2.");
}
}
}
Variable Name Data Type Description
sc Scanner Used to take input from
the user.
choice int Stores the user's menu
choice (1 or 2).
num int Stores the current
number to print in
Floyd's Triangle.
i int Loop variable for rows
in Floyd's Triangle and
ICSE pattern.
j int Loop variable for
columns in Floyd's
Triangle and ICSE
pattern.
s String To store the String ICSE
which is used to display
the pattern
1. Menu Display:
o The program displays a menu with two options:
Option 1: Print Floyd’s Triangle.
Option 2: Display a specific pattern.
o The user is asked to enter their choice (1 or 2).
2. Switch Statement:
o The switch statement handles the user's choice:
Case 1: Prints Floyd’s Triangle.
A nested loop is used to print numbers in a triangular
pattern.
The outer loop (i) controls the rows, and the inner loop
(j) controls the columns.
The variable num is incremented after each number is
printed.
Case 2: Displays the given pattern.
The pattern is printed using same logic for other
patterns.
Default: If the user enters an invalid choice, an error
message is displayed.
[2017]
import java.util.Scanner;
// Display menu
System.out.println("Menu:");
System.out.println("1. Find sum of the series");
System.out.println("2. Display the series ");
System.out.print("Enter your choice (1 or 2): ");
int choice = sc.nextInt();
case 2:
// Display the series
System.out.println("Series:");
for(int j = 1; j <= 5; j++)
{
for (int i = 1; i <= j; i++)
{
System.out.print(i);
}
System.out.print(" ");
}
break;
default:
// Display error message for incorrect choice
System.out.println("Invalid choice! Please select 1 or 2.");
}
}
}
Explanation:
1. Menu Display:
o The program displays a menu with two options:
Option 1: Find the sum of the
series S=x1−x2+x3−x4+…−x20S=x1−x2+x3−x4+…−x20 (where x=2x=2).
Option 2: Display the series 1,11,111,1111,11111.
o The user is asked to enter their choice (1 or 2).
2. Switch Statement:
o The switch statement handles the user's choice:
Case 1: Calculates and displays the sum of the series.
A for loop runs from 1 to 20.
For each term, xi is calculated using Math.pow(x, i).
If the power is even, the term is subtracted from the
sum; if odd, it is added.
Case 2: Displays the series 1,11,111,1111,11111.
Outer for loop j runs from 1 to 5.
For every iteration of j, inner loop i prints that many
1s
Default: If the user enters an invalid choice, an error
message is displayed.
[2018]
import java.util.Scanner;
// Display menu
System.out.println("Menu:");
System.out.println("1. Display Pattern 1");
System.out.println("2. Display Pattern 2");
System.out.print("Enter your choice (1 or 2): ");
int choice = sc.nextInt();
case 2:
// Display Pattern 2
System.out.println("Pattern 2:");
String s2 = "BLUE";
for (int i = 0; i < s2.length(); i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print(s2.charAt(i));
}
System.out.println();
}
break;
default:
// Display error message for incorrect choice
System.out.println("Invalid choice! Please select 1 or 2.");
}
}
}
import java.util.Scanner;
// Display menu
System.out.println("Menu:");
System.out.println("1. Print letters from A to Z with their ASCII");
System.out.println("2. Display the numeric pattern");
System.out.print("Enter your choice (1 or 2): ");
int choice = sc.nextInt();
case 2:
// Display the numeric pattern
System.out.println("Numeric Pattern:");
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
break;
default:
// Display error message for incorrect choice
System.out.println("Invalid choice! Please select 1 or 2.");
}
}
}
Variable Name Data Type Description
sc Scanner Used to take input from
the user.
choice int Stores the user's menu
choice (1 or 2).
ch char Loop variable for
generating letters from
A to Z.
i int Loop variable for rows
in the numeric pattern.
j int Loop variable for
columns in the numeric
pattern.
Explanation:
Switch Statement:
o The switch statement handles the user's choice:
Case 1: Generates and prints letters from A to Z with their
Unicode values.
A for loop runs from 'A' to 'Z'.
For each character, its ASCII value is printed
using (int)ch.
Case 2: Displays the numeric pattern.
A nested for loop is used to print the pattern.
The outer loop (i) controls the rows, and the inner loop
(j) prints numbers from 1 to the current row number.
Default: If the user enters an invalid choice, an error
message is displayed.
[2020]
import java.util.Scanner;
// Display menu
System.out.println("Menu:");
System.out.println("1. Calculate c = a^2 + 2ab");
System.out.println("2. Display the character pattern");
System.out.print("Enter your choice (1 or 2): ");
int choice = sc.nextInt();
case 2:
// Display the pattern
System.out.println("Character Pattern:");
String s = "ABCDE";
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print(s.charAt(j));
}
System.out.println();
}
break;
default:
// Display error message for incorrect choice
System.out.println("Invalid choice! Please select 1 or 2.");
}
}
}
Variable Name Data Type Description
sc Scanner Used to take input from
the user.
choice int Stores the user's menu
choice (1 or 2).
b double Stores the constant
value of b (3.0).
a double Loop variable for
calculating c = a^2 +
2ab.
c double Stores the calculated
value of c = a^2 + 2ab.
s String Stores the string
"ABCDE" for the
character pattern.
i int Loop variable for rows
in the character
pattern.
j int Loop variable for
columns in the character
pattern.
Explanation:
The switch statement handles the user's choice:
o Case 1: Calculates and prints the value of c=a2+2ab.
A for loop runs from a = 1.0 to a = 20.0 with an increment of
2.0.
For each value of a, the value of c is calculated and printed.
o Case 2: Displays the character pattern by looping over the
string "ABCDE".
A nested for loop is used to print the pattern.
The outer loop (i) controls the rows, and the inner loop (j)
prints characters from the start of the string up to the
current row.
o Default: If the user enters an invalid choice, an error message is
displayed.