Question:

Programming Puzzle in C++ Language?

by  |  earlier

0 LIKES UnLike

Dear friends, one of my friends asked me a programming puzzle yesterday, but I dont know the answer!

We have a piece of code in C++ language:

#include <iostream>

using namespace std;

int main() {

int a;

cin >> a;

if ( ( a == -a ) && ( a != 0) )

cout << ":-o" << endl;

return 0;

}

If we want the program print something as an output on the monitor, what should we give as an input???!

 Tags:

   Report

1 ANSWERS


  1. -2147483648 (-(2^31) or more generally -(2^(n-1)) where n is the number of bits in the integer type).

    Binary representation is

    10000...0000

    -a in two&#039;s complement is

    1. invert

    01111...1111

    2. Add one

    10000...0000

    a=-a (and a != 0)

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.