Nested If: Defining if block inside another if block.
Syntax:
if(condition){
if(condition){
if-if-Stat;
}
else{
if-else-stat;
}
}
else{
if(condition){
else-if-stat;
}
else{
else-else-stat;
}
}
Flow Chart:

Check Even Number or Not only if it is Positive
import java.util.Scanner;
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter number : ");
int n = scan.nextInt();
if(n>0){
if(n%2==0)
System.out.println("Even number");
else
System.out.println("Not even number");
}
else{
System.out.println("Negative number given");
}
}
}
Biggest of two only if the two numbers or not equal
import java.util.Scanner;
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a, b values : ");
int a = scan.nextInt();
int b = scan.nextInt();
if(a!=b){
if(a>b)
System.out.println("a is big");
else
System.out.println("b is big");
}
else{
System.out.println("a and b are equal");
}
}
}
Program to check the person can donate blood or not
Requirements: Age between 18 to 60 and Weight must be greater than 50
import java.util.Scanner;
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter age : ");
int age = scan.nextInt();
System.out.println("Enter weight : ");
int weight = scan.nextInt();
if(age>=18 && age<=60){
if(weight>=50)
System.out.println("Can donate blood");
else
System.out.println("Under weight");
}
els
System.out.println("Not suitable age");
}
}
Display Student Grade only if the student passed in all Subjects
import java.util.Scanner;
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter marks of 3 subjects : ");
int m1 = scan.nextInt();
int m2 = scan.nextInt();
int m3 = scan.nextInt();
if(m1>=40 && m2>=40 && m3>=40){
int avg = (m1+m2+m3)/3;
if(avg>=60)
System.out.println("Grade-A");
else if(avg>=50)
System.out.println("Grade-B");
else
System.out.println("Grade-C");
}
else{
System.out.println("Student failed");
}
}
}
Check the Triangle is Equilateral or Isosceles or Scalene
- Equilateral: All sides are equal a==b==c
- Isosceles: Any two sides are equal a==b or a==c or b==c
- Scalene: No sides are equal a != b != c
import java.util.Scanner;
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter 3 sides of triangle : ");
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
if(a==b && b==c)
System.out.println("Equilateral");
else if(a==b ||a==c||b==c)
System.out.println("Isosceles");
else
System.out.println("Scalene");
}
}
Display days for the specified month
import java.util.Scanner;
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter Month number : ");
int n = scan.nextInt();
if(n>=1 && n<=12){
if(n==2)
System.out.println("28 days / 29 days");
else if(n==4 || n==6 || n==9 || n==11)
System.out.println("30 days");
else
System.out.println("31 days");
}
else{
System.out.println("Invalid month number");
}
}
}