Page 47 - Sanko_Sixth Edition
P. 47
false part to execute }
else if (condition)
}
{
Example :
Write a program in java to input a number and check }
it is even or odd.
…...............
…...................
Solution :
else
Import java.util.Scanner;
{
Class EvenorOdd
{ }
Public static void main() Example:
{ Write a Program to input 3 numbers and print the
Scanner sc=new Scanner(System.in); largest number.
Int a; Import java.util.Scanner;
System.out.println(“Enter a Number :”); Class largest
a=sc.nextInt(); {
If(a%2==0) Public static void main()
{ {
System.out.println(“The number is EVEN”); Scanner sc=new Scanner(System.in);
} Int a,b,c;
else System.out.println(“Enter three Number :”);
{ a=sc.nextInt();
System.out.println(“The number is ODD”); b=sc.nextInt();
} c=sc.nextInt();
}
If(a>b && a>c)
}
{
System.out.println(“The Greatest Number is”+a);
2. if...............else if.......................else
}
This statement is required when multiple condition
Else if(b>a && b>c)
has to be check.
{
Syntax of if …............else if................else
System.out.println(“The Greatest number is”+b);
if(condition)
}
{
41