Java programs
P1. Write a program to print total number of days in a month using switch case.
import java.util.Scanner;
class P1DaysInMonth
{
public static void main()
{
int x;
System.out.println("Enter the month");
System.out.println("Press 1 for Jan");
System.out.println("Press 2 for Feb");
System.out.println("Press 3 for March");
System.out.println("Press 4 for April");
System.out.println("Press 5 for May");
System.out.println("Press 6 for June");
System.out.println("Press 7 for July");
System.out.println("Press 8 for Aug");
System.out.println("Press 9 for Sep");
System.out.println("Press 10 for Oct");
System.out.println("Press 11 for Nov");
System.out.println("Press 12 for Dec");
Scanner in = new Scanner(System.in);
x = in.nextInt();
if(x<1||x>12)
System.out.println("Invalid Input");
switch(x)
{
case 1 :
System.out.println("No of days in January is 31"); break;
case 2 :
System.out.println("No of days in February is 28 or 29"); break;
case 3 :
System.out.println("No of days in March is 31"); break;
case 4 :
System.out.println("No of days in April is 30"); break;
case 5 :
System.out.println("No of days in May is 31"); break;
case 6 :
System.out.println("No of days in June is 30"); break;
case 7 :
System.out.println("No of days in July is 31"); break;
case 8 :
System.out.println("No of days in August is 31"); break;
case 9 :
System.out.println("No of days in September is 30"); break;
case 10 :
System.out.println("No of days in October is 31"); break;
case 11 :
System.out.println("No of days in November is 30"); break;
case 12 :
System.out.println("No of days in December is 31"); break;
}
}
}
P2. Write a program to check whether a number is even or odd using switch case.
import java.util.Scanner;
class P2EvenOdd
{
public static void main()
{
int x,y;
System.out.println("Enter the number");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y=x%2;
switch(y)
{
case 0:
System.out.println("Given number is Even"); break;
case 1:
System.out.println("Given number is Odd"); break;
}
}
}
P3. Write a program to print maximum between two numbers using switch case.
import java.util.Scanner;
class P3Maximum
{
public static void main()
{
int x,y,max;
System.out.println("Enter the number");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
if(x>y)
max=1;
else
max=0;
switch(max)
{
case 0:
System.out.println(y+" is maximum"); break;
case 1:
System.out.println(x+" is maximum"); break;
}
}
}
P4. Write a program to check a year is leap year or not using nested if-else.
import java.util.Scanner;
class P4leap
{
public static void main(String args[])
{
int x;
System.out.println("ENTER A YEAR");
Scanner in = new Scanner(System.in);
x = in.nextInt();
if (x%4==0)
{
if(x%100==0)
{
if(x%400==0)
System.out.println("it is a leap year");
else
System.out.println("it is not a leap year");
}
else
System.out.println("it is a leap year");
}
}
}
P5.
Write a program to check a number is positive or negative using nested if-else.
import java.util.Scanner;class P5postivn_ngvti{ public static void main(String args[]) { int x; System.out.println("ENTER A number"); Scanner in = new Scanner(System.in);
x = in.nextInt(); if (x>0) System.out.println("it is a positie number"); else { if(x==0) System.out.println("it is neither positive or negative"); else System.out.println("it is negative number"); } }}
import java.util.Scanner;
class P5postivn_ngvti
{
public static void main(String args[])
{
int x;
System.out.println("ENTER A number");
Scanner in = new Scanner(System.in);
x = in.nextInt();
if (x>0)
System.out.println("it is a positie number");
else
{
if(x==0)
System.out.println("it is neither positive or negative");
else
System.out.println("it is negative number");
}
}
}
Comments
Post a Comment