Galgotias College of Engineering & Technology, Greater Noida
StudentName:….....................
INDEX Roll No:…...............................
Assessment by Faculty Member
Page No.
From….... In time
Date of
Exp No. Name of the Experiment ... Pre-Lab
Implementati Graph, submission of
Practical on/Active Results/Outp Lab Total Signature
To…........ Writing work
Participation ut, Cal. Report/Viva
... MM: 10 MM: 20
MM:50 with Date
MM:10 Voce,
MM:10
10
11
12
13
14
Teacher Remark (If any): AVERAGE MARKS
Name and Sign of Faculty members(s) with date Name & Sign of Lab Incharge HoD
Note:-
1 *Pre-Lab writing work should include program statement, objective, Algorithm(If applicable) and Methodology.
2 Faculty members will check pre Lab writing work & Lab work readings/output in each class and sign with date.
3 Students will use pen with blue color ink only.
Program No.1
Aim: Write a menu-driven program(using Switch-case) in Java to perform following
Opertions :
a. Display the addition result of any two integers
b. Swapping of two numbers
c. To check whether a number is prime or not
Program:
import java.util.Scanner;
class MenuDriven{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a,b,choice;
System.out.println("1.Addition of 2 numbers.");
System.out.println("2.Swapping of 2 numbers.");
System.out.println("3.Check given number is prime or not.");
System.out.print("Enter the choice:");
choice=sc.nextInt();
switch (choice) {
case 1:
int c;
System.out.println("Enter the numbers for addition:");
a=sc.nextInt( );
b=sc.nextInt( );
c=a+b;
System.out.println("Addition of " + a + " and " + b + " is: " +c);
break;
case 2:
int temp;
System.out.println("Enter the numbers for swapping:");
a=sc.nextInt();
b=sc.nextInt();
temp=b;
b=a;
a=temp;
System.out.println("Values after swapping:");
System.out.println(a);
System.out.println(b);
break;
case 3:
int num;
System.out.print("Enter number to check prime or not:");
num=sc.nextInt();
if(num==1)
{
System.out.println("num is not prime");
break;
}
for(int i=2;i<num;i++)
{
if(num % i==0)
{
System.out.print("num is not prime");
return;
}
}
System.out.println("num is prime.");
break;
default:
System.out.println("Invalid choice.");
break;
}
}
Output:
PS D:\Programs\Java> javac MenuDriven.java
PS D:\Programs\Java> java MenuDriven
1.Addition of 2 numbers.
2.Swapping of 2 numbers.
3.Check given number is prime or not.
Enter the choice:1
Enter the numbers for addition:
15
26
Addition of 15 and 26 is: 41
PS D:\Programs\Java> java MenuDriven
1.Addition of 2 numbers.
2.Swapping of 2 numbers.
3.Check given number is prime or not.
Enter the choice:2
Enter the numbers for swapping:
46
64
Values after swapping:
64
46
PS D:\Programs\Java> java MenuDriven
1.Addition of 2 numbers.
2.Swapping of 2 numbers.
3.Check given number is prime or not.
Enter the choice:3
Enter number to check prime or not:21
num is not prime
PS D:\Programs\Java> java MenuDriven
1.Addition of 2 numbers.
2.Swapping of 2 numbers.
3.Check given number is prime or not.
Enter the choice:3
Enter number to check prime or not:23
num is prime.