Write a program to find the greatest of three numbers.
Example 1:
Input : a=3, b=7,c=2
Output: 7 is greatest number
Approach
Java
public class GreatestNumber {public static void main(String[] args) {int a = 3, b = 7, c = 2;int greatest = findGreatest(a, b, c);System.out.println("Greatest number is " + greatest);}private static int findGreatest(int a, int b, int c) {if (a > b && a > c)return a;else if (b > a && b > c)return b;return c;}}
C++
#include <bits/stdc++.h>using namespace std;//function to find greatest of three//numbersint findGreatest(int a,int b,int c){//if a is greate than or eqaul to// both b and cif(a>=b&&a>=c)return a;//if b is greater than or equal to//both a and celse if(b>=a&&b>=c)return b;//if c is greater than or equal to//both b and creturn c;}int main(){int a=3,b=7,c=2;int greatest=findGreatest(a,b,c);cout<<"Greatest is ";cout<<greatest<<"\n";return 0;}
No comments:
Post a Comment