C++ Programming GalleryThe Programming Challenge

Challenge 14 – Compare 2 numbers in one line of code in C++

Difficulty x10
x3

This Challenge was proposed for the now defunct C/C++ programming list CWORLD in 1999.
The source code of the challenge has been updated to modern C++, but maintaining the essence of the challenge, which its original author wanted to transmit.

Date: 20-II-2001
Created by: José Luis De la Cruz
Challenge winner: Nyder Menéndez

This challenge is not as difficult as its predecessors, but it is also tricky. The good thing about this challenge is that you don’t need to have a compiler at hand to answer it, which is why, I believe, the previous challenges were not very well answered.

The challenge consists of that the program delivers the comparison of 2 integers entered by keyboard, for this it is provided of source code already written, reason why only the code that compares the entered values is missing to then show the respective results.

Conditions

Only the USEFUL CODE LINE(*) that is dotted must be implemented, without touching the code that is already written.

(*) A useful line of code in C++ is the line of code between two consecutive semicolons.

And so that they do not take it so easy, you can not use if, switch, break, while, do, for and ?:.
You cannot declare more variables than are already declared.

Source Code

#include <iostream>
using std::cout;
using std::cin;

void main()
{
 int a,b;

 cout << "\n\nEnter integer A: ";
 cin >> a;
 cout << "\nEnter integer B: ";
 cin >> b;

 ............ ; //just complete code here

 if( a==b )
	 cout << "\n\nThey are equal numbers."; 
 else 
	 cout << "\n\nThe largest is " << a << " and the smallest is " << b << ".";
}

AND FINALLY I CAN ONLY SAY …. BEST OF LUCK AND MAY THE BEST CODE WIN….

If you liked this challenge remember to leave a comment in our guestbook

Solution

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button