Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

if-else (Decision Making)

Easy
0/40
Average time to solve is 10m
profile
Contributed by
256 upvotes
Asked in companies
Tata Consultancy Services (TCS)Samsung

Problem statement

Programming languages have some conditional / decision-making statements that execute when some specific condition is fulfilled.


If-else is one of the ways to implement them.


You are given two numbers 'a' and 'b'.


Compare the numbers and print the relation.



Example :
Input: ‘a’ = 5 and ‘b’ = 3

Output: greater

Explanation: Since ‘a’ (= 5) is greater than ‘b’ (= 3), we are printing “greater”.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first and only line of the input contains two space-separated integers, ‘a’ and ‘b’, the numbers you have to compare.


Output Format :
The first and only line of the output consists of a string representing the relation between the two integers. 


Note :
You do not need to print anything; it has already been taken care of. Just implement the given function.
Sample Input 1:
5 3


Sample Output 1:
greater


Explanation of sample input 1 :
Since ‘a’ (= 5) is greater than ‘b’ (= 3), we are printing “greater”.


Sample Input 2:
2 2


Sample Output 2:
equal


Explanation of sample input 2 :
Since ‘a’ (= 2) is equal to ‘b’ (= 2), we are printing “equal”.


Expected time complexity :
The expected time complexity is O(1).


Constraints :
-10 ^ 5 <= ‘a’ <= 10 ^ 5
-10 ^ 5 <= ‘b’ <= 10 ^ 5

Time limit: 1 second
Approaches (1)
If-else conditionals

The if-else conditional statements let us choose the statements we want to execute depending on some conditions.

We can check if ‘a’ is greater than, smaller than or equal to ‘b’ and print the valid relation.

The steps are as follows:

string compareIfElse(int ‘a’, int ‘b’)

  1. If ‘a’ > ‘b’, then return “greater”.
  2. Else if ‘a’ < ‘b’, then return “smaller”.
  3. If both conditions are false, then return “equal”.
Time Complexity

O(1), that is constant time.

The comparison of two numbers is a constant time operation, and we are doing it 2 - 3 times.

Hence the time complexity is O(1).

Space Complexity

O(1), that is constant space.

We are not using any auxiliary space.

Hence the space complexity is O(1).

Code Solution
(100% EXP penalty)
if-else (Decision Making)
All tags
Sort by
Search icon

Interview problems

using python

from typing import *

 

def compareIfElse(a: int, b: int):

    if a>b:

        return 'greater'

    elif a==b:

        

        return 'equal'

    else:

        return 'smaller'

24 views
0 replies
0 upvotes

Interview problems

code in C++ using ternary operators

//using ternary opertor 

string compareIfElse(int a, int b) {

    // Write your code here

    return (a>b)?"greater":(a<b)?"smaller": "equal";

}

 

50 views
0 replies
0 upvotes

Interview problems

simple python solution

def compareIfElse(a: int, b: int)->str:

    # Write your code here

    if a>b:

 

        return "greater"

 

    elif a<b:

 

        return "smaller"

 

    return "equal"

42 views
0 replies
0 upvotes

Interview problems

code in C++ using ternary operators

string compareIfElse(int a, int b) {

    // Write your code here

    return (a>b)?"greater":(a<b)?"smaller":"equal";

}

 

60 views
0 replies
0 upvotes

Interview problems

if-else decision making

string compareIfElse(int a, int b) {

// Write your code here

 

if(a>b){

return "greater";

}

else if (a<b){

return "smaller";

}

else {

return "equal";

}

 

}

 

92 views
0 replies
0 upvotes

Interview problems

One Line answer

string compareIfElse(int a, int b) {
	return a==b?"equal":(a>b?"greater":"smaller");
}
27 views
0 replies
0 upvotes

Interview problems

Using ternary operator

public class Solution {

    public static String compareIfElse(int a, int b) {

        // Write your code here

        // if(a > b)

        // return "greater";

 

        // else if (a < b)

        // return "smaller";

 

        // else if ( a== b)

        // return "equal";

 

        // return "invalid";

 

        String data =  a > b ? "greater" : (a<b)? "smaller" : "equal";

        return data;

    }

}

50 views
0 replies
0 upvotes

Interview problems

easy question

string compareIfElse(int a, int b) 

{   

 

        if (a < b) {

          return "smaller";

        } 

        else if (a > b) {

          return "greater";

        } 

    else{

        return "equal";

    }

}

122 views
0 replies
0 upvotes

Interview problems

easy solution

string compareIfElse(int a, int b) {

  // Write your code here

  if(a==b) return "equal";

        else {

        if (a < b )

          return "smaller";

        else

          return "greater";

    }

}

 

61 views
0 replies
0 upvotes

Interview problems

no input

I don't know why the code doesn't need input but just writing the code without input will make the code pass
from typing import *

def compareIfElse(a: int, b: int):
    # Write your code here
    if a > b:
        return "greater"
    if a < b:
        return "smaller"
    if a == b:
        return "equal"
    else:
        return None
11 views
0 replies
1 upvote
Full screen
Console