Skip to main content

Java Flow Control And Functions If Else | Day 1

Problem:

In computer programming, we use the if statement to control the flow of the program. For example, if a certain condition is met, then run a specific block of code. Otherwise, run another code.

For example:

 class Main {
  public static void main(String[] args) {

    int number = 0;

    // checks if number is greater than 0
    if (number > 0) {
      System.out.println("The number is positive.");
    }

    // checks if number is less than 0
    else if (number < 0) {
      System.out.println("The number is negative.");
    }

    // if both condition is false
    else {
      System.out.println("The number is 0.");
    }
  }
}


Output

The number is 0.

In the above example, we are checking whether the number is positive, negative, or zero.

Here, we have two condition expressions:

  • number > 0 - checks if the number is greater than 0
  • number < 0 - checks if the number is less than 0

Here, the value of the number is 0. So both the conditions evaluate to false. Hence the statement inside the body of else is executed.

Task:

Given an integer M perform the following conditional actions:

  • If M is multiple of 3 and 5 then print "Good Number" (without quotes).
  • If M is only multiple of 3 and not of 5 then print "Bad Number" (without quotes).
  • If M is only multiple of 5 and not of 3 then print "Poor Number" (without quotes).
  • If M doesn't satisfy any of the above conditions then print "-1" (without quotes).

 

Solution:

import java.lang.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        /***Don't change the code here***/
        
        Scanner inp  = new Scanner(System.in);
        int M = inp.nextInt();
        inp.close();
        /*********************************/
        
        /**Write your code here**/
        
        if(M%3==0 && M%5==0){
            System.out.println("Good Number");
        }else if(M%3==0)
            System.out.println("Bad Number");
        else if(M%5==0)
            System.out.println("Poor Number");
        else 
            System.out.println(-1);
    }
}

Comments

Must Read:

Course DH ASE B3 Slot3 Mock 1 Handson | RDBMS

DH ASE B3          Slot3              Mock 1                  Handson: RDBMS

Software Engineering Concepts Software Engineering Fundamentals Post-Quiz

  Software Engineering Concepts       Software Engineering Fundamentals            Post-Quiz

Accenture Mock Quiz | Part 4

  Question  31 Correct Marked out of 1.00 Flag question Question text What will be the output of the following Java code? class Test extends Throwable { } class Base extends Test {} public class Main { public static void main(String args[]) { try { throw new Base(); } catch(Test t) { System.out.println("Test Exception"); } finally { System.out.println("Finally block "); } } } Select one: a.  Complilation error : Bass calss can't extends Test b.  print-"Test Exception" c.  Complilation error: Test Class cant extends Throwable d.  print - "Test Exception" "Finally block "   Feedback The correct answer is: print - "Test Exception" "Finally block " Question  32 Correct Marked out of 1.00 Flag question Question text Which of the following statement(s) is/are TRUE? (i) In a non-correlated(independent) subquery, the subquery is always executed only onc...

Subscribe to Get's Answer by Email