Write any Program to check multiple conditions using if statement.
Logic.java
class Logic
{
public static void main( String args[] )
{
int a,b,c;
a= Integer.parseInt(args[0]);
b= Integer.parseInt(args[1]);
c= Integer.parseInt(args[2]);
if(a>b && a>c)
System.out.println("\n\n Greatest number is= "+a);
else if(b>a && b>c)
System.out.println("\n\n Greatest number is= "+b);
else if(c>a && c>b)
System.out.println("\n\n Greatest number is= "+c);
else
System.out.println("\n\n All are equals");
}
}
1) List operators used in if conditional statement.
Ans=> Operators :
1) >
2) <
3) ==
4) !=
5) =
6) &&
7) ||
8) >=
9) <=
2) In if-else construct which part will be executed if condition is true.
Ans=> If part will be executed if condition is true.
3) State the condition when the else part will be executed with example.
Ans=> when if part become false then else part will be executed.
ex: if( 0 )
{
}
else
{
}
In this time the else part will execute.
4) Which of the following operator is used in if:
a. Assignment Operator(=) b.Comparison Operator(==)
Ans=> b
Exercise:
1) State the output:
1)
Output:
2)
Output:
2) Write a program to make the use of Logical Operators.
Output:
3) Write a program to check no is even or odd.
Output:
Logic.java
class Logic
{
public static void main( String args[] )
{
int a,b,c;
a= Integer.parseInt(args[0]);
b= Integer.parseInt(args[1]);
c= Integer.parseInt(args[2]);
if(a>b && a>c)
System.out.println("\n\n Greatest number is= "+a);
else if(b>a && b>c)
System.out.println("\n\n Greatest number is= "+b);
else if(c>a && c>b)
System.out.println("\n\n Greatest number is= "+c);
else
System.out.println("\n\n All are equals");
}
}
Output:
1) List operators used in if conditional statement.
Ans=> Operators :
1) >
2) <
3) ==
4) !=
5) =
6) &&
7) ||
8) >=
9) <=
2) In if-else construct which part will be executed if condition is true.
Ans=> If part will be executed if condition is true.
3) State the condition when the else part will be executed with example.
Ans=> when if part become false then else part will be executed.
ex: if( 0 )
{
}
else
{
}
In this time the else part will execute.
4) Which of the following operator is used in if:
a. Assignment Operator(=) b.Comparison Operator(==)
Ans=> b
Exercise:
1) State the output:
1)
public class NestedIfExample
{
public static void main( String args[] )
{
int num=70;
if( num < 100 )
{
System.out.println("number is less than 100");
if(num > 50)
{
System.out.println("number is greater than 50");
}
}
}
}
{
public static void main( String args[] )
{
int num=70;
if( num < 100 )
{
System.out.println("number is less than 100");
if(num > 50)
{
System.out.println("number is greater than 50");
}
}
}
}
Output:
2)
class IfStatement
{
public static void main( String args[] )
{
int number = 10;
if( number > 0 )
{
System.out.println(" Number is Positive ");
}
System.out.println(" This statement is always executed ");
}
}
{
public static void main( String args[] )
{
int number = 10;
if( number > 0 )
{
System.out.println(" Number is Positive ");
}
System.out.println(" This statement is always executed ");
}
}
Output:
2) Write a program to make the use of Logical Operators.
class Logic
{
public static void main(String[] args)
{
int marks=65;
if(marks<50)
{
System.out.println("fail");
}
else if(marks>=50 && marks<60)
{
System.out.println("D grade");
}
else if (marks>=60 && marks<70)
{
System.out.println("C grade");
}
else if (marks>=70 && marks<80)
{
System.out.println("B grade");
}
else if(marks>=80 && marks<90)
{
System.out.println("A grade");
}
else if(marks>=90 && marks<100)
{
System.out.println("A+ grade");
}
else
{
System.out.println("Invalid!");
}
}
}
{
public static void main(String[] args)
{
int marks=65;
if(marks<50)
{
System.out.println("fail");
}
else if(marks>=50 && marks<60)
{
System.out.println("D grade");
}
else if (marks>=60 && marks<70)
{
System.out.println("C grade");
}
else if (marks>=70 && marks<80)
{
System.out.println("B grade");
}
else if(marks>=80 && marks<90)
{
System.out.println("A grade");
}
else if(marks>=90 && marks<100)
{
System.out.println("A+ grade");
}
else
{
System.out.println("Invalid!");
}
}
}
Output:
3) Write a program to check no is even or odd.
class EvenOdd
{
public static void main( String args[] )
{
int a = Integer.parseInt( args[0] );
if( a%2 == 0 )
System.out.println(" \n\n Number is Even ");
else
System.out.println(" \n\n Number is Odd ");
}
}
{
public static void main( String args[] )
{
int a = Integer.parseInt( args[0] );
if( a%2 == 0 )
System.out.println(" \n\n Number is Even ");
else
System.out.println(" \n\n Number is Odd ");
}
}
Output: