heron heron - 1 year ago 81
C++ Question

Ways to find maximum digit

Currently my mini algorithm looks like this.

int a,b,c,max;
cout <<"Enter 3 digits: \t";
if(a>b && a>c)
else if(b>c && b>a)
cout <<"max: "<<max<<endl;

It works but Is there any other way to find maximum of 3 digits?

Answer Source

In C++11, you could do this:

int max_value = std::max({a, b, c});

It makes use of an overload of std::max which takes std::initializer_list<T> as argument. It implies you could pass more than 3 arguments!

int max_value = std::max({1,2,3,4,5,6,98,10});

Demo : http://ideone.com/FLifw

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download