Tuesday, 6 July 2021

what is Nested if Statement || Java Nested if

 Nested if statement


Nested if

If a if statement is used within if statement then such a control structure is called as nested if


if(condition) 
     if(condition) 
     { 
Statements…3
 ………………….. 
     } 
}

Nested If statement is used to check a condition only if another condition is true


WAP to read a number and check if it is a 2 digit no. or not. If it is a 2 digit no. then check if both digits are same or not.

program:- 

import java.util.Scanner;
public class easy
{
public static void main(String[] args)
{
Scanner likein=new Scanner(System.in);
System.out.print("Enter a Number:");
int a=likein.nextInt();
if(a>=10 && a<=99)
{
System.out.println("It is a 2 digit Number");
int x=a%10;
int y=a/10;
if(x==y)
System.out.println("Both digits are same");
else
System.out.println("Both digits are Not same");
}
else
System.out.println("It is not a 2 digit Number");
}
}

Output:- 
Enter a Number:22
It is a 2 digit Number
Both digits are same.

WAP to read a number and check if it is a positive number or not. If it is a positive number then
check if it is even or not.
program:- 
package com.company;
import java.util.Scanner;
public class easy
{
public static void main(String[] args)
{
Scanner likein=new Scanner(System.in);
System.out.print("Enter a Number:");
int a=likein.nextInt();
if(a>0)
{
System.out.println("Number is Positive");
if(a%2==0)
System.out.println("Even Number");
else
System.out.println("Odd Number");
}
else
System.out.println("Number is Not Positive");
}
}

Output:- 
Enter a Number:
44
Number is Positive
Even Number

WAP to read 3 angles and check if triangle can be formed or not. If triangle can be formed
then check if it is an isosceles or equilateral or right angled triangle.

package com.company;
import java.util.Scanner;
public class easy
{

public static void main(String[] args)
{
Scanner likein=new Scanner(System.in);
System.out.print("Enter 3 Angles:");
int a=likein.nextInt();
int b=likein.nextInt();
int c=likein.nextInt();
if(a+b+c==180)
{
System.out.println("Triangle can be formed");
if(a==b && b==c)
System.out.println("Eq.Triangle");
if(a==b || b==c || c==a)
System.out.println("Iso.Triangle");
if(a==90 || b==90 || c==90)
System.out.println("Rt.Ag.Triangle");
}
else
System.out.println("Triangle cannot be formed");
}
}
Output:- 
Enter 3 Angles:
90 60 30
Triangle can be formed
Rt.Ag.Triangle

No comments:

Post a Comment

What is ladder if || Java Ladder if statement.

 Ladder if statement If a if statement is used within an else statement then such a control structure is called as ladder if statement. Synt...