Java fundamental programs to practice: Conditional statements(if,else)

             Conditional statements are used to check the conditions. It uses the conditional statements listed below.

  • 1.       If statement
  • 2.       If else statement
  • 3.       If else if statement

Java fundamental program: To check a number is greater than five(if statement example)

              This program reads a input value. Check the value is greater than 5 or not and print the value.

import java.util.Scanner;

class great

{

 public static void main(String args[])

 {

   Scanner scanner = new Scanner(System.in);

   System.out.print("Enter a number: ");

   int n = scanner.nextInt();

   if (n >5)

     System.out.println("The given number is greater than five");

 }

}

The output is given below.

C:\raji\blog>javac great.java

C:\raji\blog>java great

Enter a number: 7

The given number is greater than five

Next,if else statement example is given below.

Java fundamental program: To check your examination status pass or fail(if else statement example)

              This program reads a mark of a student. It checks the mark value is greater than 35 or not. If it is greater than 35, it prints the pass message. otherwise, it prints the fail message.

import java.util.Scanner;

class pos

{

 public static void main(String args[])

 {

   Scanner scanner = new Scanner(System.in);

   System.out.print("Enter a mark: ");

   int ma = scanner.nextInt();

   if (ma > 35)

     System.out.println("You have passed in the examination");

   else

     System.out.println("sorry,you have failed in the examination");

 }

}

Here is the output.

C:\raji\blog>javac pos.java

C:\raji\blog>java pos

Enter a mark: 67

You have passed in the examination

C:\raji\blog>java pos

Enter a mark: 32

sorry,you have failed in the examination

Next program is using if else ladder.

Java fundamental program: To check your mark grade (if else if ladder statement example)

              This program reads the mark. Prints the grade according to the mark using multiple if else statement conditional statement checking.

import java.util.Scanner;

public class ifelseif

{

 public static void main(String args[])

 {

   Scanner scanner = new Scanner(System.in);

   System.out.print("Enter the mark:");

   int m= scanner.nextInt();

   if(m>90)

    System.out.println("You have scored well. Your grade is A");

   else if(m<=89 && m>80)

    System.out.println("Your grade is B");

   else if(m<=79 && m>60)

    System.out.println("Your grade is C");

   else if(m<=59 && m>=35)

    System.out.println("Your grade is D");

   else

    System.out.println("Sorry. You have failed in the examination");

 }

}

The output is shown below.

C:\raji\blog>javac ifelseif.java

C:\raji\blog>java ifelseif

Enter the mark: 97

You have scored well. Your grade is A

C:\raji\blog>java ifelseif

Enter the mark: 87

Your grade is B

C:\raji\blog>java ifelseif

Enter the mark:76

Your grade is C

C:\raji\blog>java ifelseif

Enter the mark:45

Your grade is D

C:\raji\blog>java ifelseif

Enter the mark:31

Sorry. You have failed in the examination

These are the ways of using if else conditional statements in java programs in basic level. Enjoy coding.


No comments:

Post a Comment