To
Create Simple Calculator (all arithmetic operation) using Switch Case
import java.util.Scanner;
public class Cal {
public static void main(String[] args) {
Scanner sc =
new Scanner(System.in);
System.out.println("Enter an operator (+, -, *,/)");
char
op=sc.next().charAt(0);
System.out.println("Enter 1stno:\n");
int
firstNumber=sc.nextInt();
System.out.println("Enter 2ndno:\n");
int
secondNumber=sc.nextInt();
switch(op)
{
case '+':
System.out.println("sum is"+(firstNumber + secondNumber));
break;
case '-':
System.out.println("sub is"+(firstNumber - secondNumber));
break;
case '*':
System.out.println("mult is"+(firstNumber * secondNumber));
break;
case '/':
System.out.println("mod is"+(firstNumber / secondNumber));
break;
//
operator doesn't match (+, -, *, /)
default:
System.out.println("invalid operator");
}
}
}
To check vowel or consonant using switch case
import java.util.Scanner;
public class Vow {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter any alphabet: ");
char ch=sc.next().charAt(0);
switch(ch)
{
case 'a':
System.out.println("Vowel");
break;
case 'e':
System.out.println("Vowel");
break;
case 'i':
System.out.println("Vowel");
break;
case 'o':
System.out.println("Vowel");
break;
case 'u':
System.out.println("Vowel");
break;
case 'A':
System.out.println("Vowel");
break;
case 'E':
System.out.println("Vowel");
break;
case 'I':
System.out.println("Vowel");
break;
case 'O':
System.out.println("Vowel");
break;
case 'U':
System.out.println("Vowel");
break;
default:
System.out.println("Consonant");
}
}
}
To print number of
days in a month using switch case
import
java.util.Scanner;
public class B {
public static void main(String[] args) {
System.out.println("Enter month number(1-12): ");
switch(month)
{
case 1:
System.out.println("31 days");
break;
case 2:
System.out.println("28/29 days");
break;
case 3:
System.out.println("31 days");
break;
case 4:
System.out.println("30 days");
break;
case 5:
System.out.println("31 days");
break;
case 6:
System.out.println("30 days");
break;
case 7:
System.out.println("31 days");
break;
case 8:
System.out.println("31 days");
break;
case 9:
System.out.println("30 days");
break;
case 10:
System.out.println("31 days");
break;
case 11:
System.out.println("30 days");
break;
case 12:
System.out.println("31 days");
break;
default:
System.out.println("Invalid month! Please enter month number
between 1-12");
}
}
To check Even or Odd number using switch case
import java.util.Scanner;
public class Check {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("enter number\n");
int num=sc.nextInt();
switch(num % 2)
{
/* If n%2 == 0
*/
case 0:
System.out.println("Number is Even");
break;
/* Else if n%2
== 1 */
case 1:
System.out.println("Number is
Odd");
break;
}
}
}
To print day of week
using switch case
import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter week number(1-7): ");
int
week=sc.nextInt();
switch(week)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid week! Please enter week number between
1-7.");
}
}
}
Write a program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer, calculate percentage and grade according to given conditions using switch case
If percentage >= 90% : Grade A
If percentage >= 80% : Grade B
If percentage >= 70% : Grade C
If percentage >= 60% : Grade D
If percentage >= 40% : Grade E
If percentage < 40% : Fail
If percentage >= 90% : Grade A
If percentage >= 80% : Grade B
If percentage >= 70% : Grade C
If percentage >= 60% : Grade D
If percentage >= 40% : Grade E
If percentage < 40% : Fail
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
float per;
System.out.print("Enter physics:\n ");
int phy = sc.nextInt();
System.out.print("Enter chemistry \n ");
int chem = sc.nextInt();
System.out.print("Enter biology \n ");
int bio = sc.nextInt();
System.out.print("Enter math \n ");
int math = sc.nextInt();
System.out.print("Enter computer \n ");
int comp = sc.nextInt();
/* Calculate percentage */
per = (phy + chem + bio + math + comp) / 5;
System.out.print("Percentage ="+ per);
if(per >= 90)
{
n=1;
}
else if(per >= 80)
{
n=2;
}
else if(per >= 70)
{
n=3;
}
else if(per >= 60)
{
n=4;
}
else if(per >= 40)
{
n=5;
}
else
{
n=6;
}
switch(n)
{
case 1:
System.out.print("\nGrade A");
break;
case 2:
System.out.print("\nGrade B");
break;
case 3:
System.out.print("\nGrade C");
break;
case 4:
System.out.print("\nGrade D");
break;
case 5:
System.out.print("\nGrade E");
break;
default:
System.out.print("\nFail");
}
}
No comments:
Post a Comment