Write a program to find the greatest of two numbers.
Example 1:
Input: num1=13,num2=15
Output: Greater is second number
Example 2:
Input: num1=17,num2=15
Output: Greater is first number
Example 3:
Input: num1=13,num2=13
Output: Equal
Approach
Java
public class GreatestTwoNumber {public static void main(String[] args) {int a = 13, b = 15;int greatest = findGreatest(a, b);System.out.println("Greatest number is " + greatest);}private static int findGreatest(int a, int b) {if (a > b)return a;elsereturn b;}}
C++
#include <bits/stdc++.h>using namespace std;//funtion to find the greatest of//two numberint greaterOfTwo(int num1,int num2){if(num1>num2)return 1;else if(num2>num1)return 2;elsereturn 0;}int main(){int num1=13;int num2=15;int greater=greaterOfTwo(num1,num2);if(greater==1)cout<<"Greater is first number ";else if(greater==2)cout<<"Greater is second number ";elsecout<<"Equal ";return 0;}
No comments:
Post a Comment