answersLogoWhite

0


Best Answer

This snippet assumes you have two values, x and y and you want to find the greater.

int max (int x, int y)
{
if (x > y)
{
return x;
}

return y;
}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

if (x > y) System.out.println("The larger number is " + x);
else System.out.println("The larger number is " + y);

Or shorter, with the ternary operator:

System.out.println("The larger number is " + x > y ? x : y);

Yet another option is to use the max() method in the Match() class.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

double firstNumber = 0;

double secondNumber = 0;

...

cout << "Enter a first number: ";

cin >> firstNumber;

...

cout << "Enter a second number: ";

cin >> secondNumber;

...

if (firstNumber > secondNumber)

{

cout << "The first number is greater!";

}

else

{

bool result = true;

...

if (firstNumber < secondNumber)

{

result = false;

}

else

{

result = true;

}

...

switch (result)

{

case true:

cout << "The numbers are equal!";

break;

case false:

cout << "The second number is greater!";

break;

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The following demonstrates how to compare three values, returning the greatest.

The function, GreatestOf3(), is declared as a template function. This means you can call the function with any type of data, so long as all three parameters are of the same data type (int, char, float, etc). The compiler will automatically generate explicit versions of the function to cater for all the types you pass, which saves you having to write the same code over and over to cater for all possible types you might pass now or in the future. It will also cater for class instances, structs and enums, provided you implement the greater-than operator (>) for each specific type.

Note that all parameters are passed as const references. The function does not alter the parameters, hence they are declared const. They are passed by reference to avoid the creation of automatic variables (as would occur if they were passed by value, which would copy all three values locally). The return value is also a reference, but is declared non-const. In other words, it will return the largest parameter, rather than a copy of the largest parameter. The return value can be cast to a const reference by the caller, if required.

#include <iostream>

template<class T>

T& GreatestOf3(const T& t1, const T& t2, const T& t3)

{

return((T&)(t3>t2&&t3>t1?t3:t2>t1?t2:t1));

// The above line is C++ shorthand.

// Using longhand it would essentially equate to:

// if(t3>t2 && t3>t1 ) return(t3);

// else if(t2>t1) return(t2);

// Note: a final else is optional here, but is not required as all possibilities have been covered by the previous lines.

// return(t1);

};

int main()

{

// Example usage using ints and doubles:

int x=5, y=10, z=3;

std::cout<<"The greatest of {"<<x<<", "<<y<<" and "<<z<<"} is "<<GreatestOf3(x, y, z)<<std::endl;

double a=1.1, b=1.0, c=1.2;

std::cout<<"The greatest of {"<<a<<", "<<b<<" and "<<c<<"} is "<<GreatestOf3(a, b, c)<<std::endl;

return(0);

}

Output:

The greatest of {5, 10 and 3} is 10

The greatest of {1.1, 1 and 1.2} is 1.2

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

if (x > y) System.out.println("The larger number is " + x);
else System.out.println("The larger number is " + y);

Or shorter, with the ternary operator:

System.out.println("The larger number is " + x > y ? x : y);

Yet another option is to use the max() method in the Match() class.

if (x > y) System.out.println("The larger number is " + x);
else System.out.println("The larger number is " + y);

Or shorter, with the ternary operator:

System.out.println("The larger number is " + x > y ? x : y);

Yet another option is to use the max() method in the Match() class.

if (x > y) System.out.println("The larger number is " + x);
else System.out.println("The larger number is " + y);

Or shorter, with the ternary operator:

System.out.println("The larger number is " + x > y ? x : y);

Yet another option is to use the max() method in the Match() class.

if (x > y) System.out.println("The larger number is " + x);
else System.out.println("The larger number is " + y);

Or shorter, with the ternary operator:

System.out.println("The larger number is " + x > y ? x : y);

Yet another option is to use the max() method in the Match() class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c plus plus program to find the greatest of the three numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Shell program for gcd of three given numbers?

write a shell program for finding out gcd of three given numbers? write a shell program for finding out gcd of three given numbers? write a shell program for finding out gcd of three given numbers? check bellow link http://bashscript.blogspot.com/2009/08/gcd-of-more-than-two-numbers.html


What three numbers have a greatest common factor?

Any three.


Program to print greatest of three numbers?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int a,b,c; clrscr(); printf("enter the three numbers: "); scanf(%d%d%d:,&amp;a&amp;b&amp;c); if(a&gt;b | a&gt;c) { printf("the greatest no: is %d",a); } if(b&gt;c) { printf("the greatest no: is %d",b); } else { printf("the greatest no: is %d",c); } getch(); }


How do you write in numbers three thousand three hundred sixty-nine?

How wood you write in numbers three thousand three hundred sixty-nine


What is the sum of the three greatest prime numbers that are less than 39?

The sum of the three greatest prime numbers that are less than 39 is 97.


Write a 'c' program to fine a largest of three numbers?

largest of a, b, c :a > b ? a > c ? a : c : b > c ? b : c


Find the three numbers whose greatest common factor of three numbers is 9 and the sum of the numbers is 90?

63, 18 and 9


Name the greatest common factor of the following numbers three and 37?

The greatest common factor for the numbers 3 and 37 is 1.


Write a program in COBOL to find the largest of N number?

identification division. program-id. greatest. environment division. data division. working-storage section. 77 a pic 999. 77 b pic 999. 77 c pic 999. procedure division. greatest. display "ENTER THE THREE NUMBERS:". accept a. accept b. accept c. if a &gt; b and a &gt; c display a "is greatest" else if b &gt; c display b "is greatest" else display c "is greatest". display " thank you". stop run.


What is the greatest three digit even numbers?

The answer is 998.


How do you write three thousand in numbers?

3000


How do you write three trillion in numbers?

3,000,000,000,000