Question:
How to use if-else (Decision Making) in Java?

Summary: We are given with two integers, n and m. The task is to check the relation between n and m.


Solution:

class Solution{

    static String compareNM(int n,int m){

        // code here

        // Scanner sc = new Scanner(System.in);

        // int n = sc.nextInt();

        // int m = sc.nextInt();

        

        

        if(n==m){

            return ("equal");

        }

        else if(n<m){

            return ("lesser");

        }

        else {

            return ("greater");

        }

    }

}


Explanation:

This Java code defines a static method compareNM within the class Solution. Here's a breakdown of the code:


Method Signature: The method compareNM takes two integer parameters n and m and returns a String indicating the relationship between them.


Comparison Logic: The method compares the values of n and m to determine their relationship:


  • If n is equal to m, it returns the string "equal".

  • If n is less than m, it returns the string "lesser".

  • If n is greater than m, it returns the string "greater".


Return Statement: The method returns the appropriate string based on the comparison result.


Overall, this code provides a simple method to compare two integer values n and m and return a string indicating their relationship (equal, lesser, or greater).


Suggested blogs:

>Python Error Solved: load_associated_files do not load a txt file

>Python Error Solved: pg_config executable not found

>Set up Node.js & connect to a MongoDB Database Using Node.js

>Setting up a Cloud Composer environment: Step-by-step guide

>What is microservice architecture, and why is it better than monolithic architecture?

>What is pipe in Angular?

>What makes Python 'flow' with HTML nicely as compared to PHP?

>What to do when MQTT terminates an infinite loop while subscribing to a topic in Python?

>Creating custom required rule - Laravel validation

>How to configure transfers for different accounts in stripe with laravel?


Ritu Singh

Ritu Singh

Submit
0 Answers