Tuesday, 6 July 2021

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.

Syntax- 
if(condition) 
     Statements.. 
else
     if(condition) 
         statements.. 
    else 
         if(condition) 
              statements.. 
          else …. 

A ladder if is used to check a condition if some other condition is false


1) WAP to read a number and check if it is a positive no. or negative no or zero.

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>0)
System.out.println("Number is Positive..");
else
if(a<0)
System.out.println("Number is Negative..");
else
System.out.println("Number is Zero..");
}
}

Output:-
Enter a Number:
7
Number is Positive..

2) WAP to read 3 different nos. a , b and c. and find greatest of them.

program:- 
package com.company;
import java.util.Scanner;
class easy
{
public static void main(String[] args)
{
Scanner likein=new Scanner(System.in);
System.out.println("Enter 3 different Numbers:");
int a=likein.nextInt();
int b=likein.nextInt();
int c=likein.nextInt();
if(a>b && a>c)
System.out.println(a+" is Greatest");
else
if(b>c)
System.out.println(b+" is Greatest");
else
System.out.println(c+" is Greatest");
}
}

Output:- 
Enter 3 different Numbers:
7 14 17
17 is Greatest

3) WAP to read color code i.e. a char value and print appropriate
color according to code.
R - red
G - green
B - blue
any other char-white].

PROGRAM:-

import java.util.Scanner;
class easy
{
public static void main(String[] args)
{
Scanner likein=new Scanner(System.in);
System.out.print("Enter Color Code:");
String str=likein.next();
char ch=str.charAt(0);
//charAt method is used to get char from pos
if(ch=='r' || ch=='R')
System.out.println("Red");
else
if(ch=='g' || ch=='G')
System.out.println("Green");
else
if(ch=='b' || ch=='B')
System.out.println("Blue");
else
System.out.println("White");
}
}

oUTPUT:- 
Enter Color Code:
R
Red

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...