What will be the output of the following program in c++?
when value of a is input as (i)6 , (ii)0:
int a, b=3 ;
cin >> a ;
if (a)
b=a++ -1;
cout<<"a="<<a<<endl ;
cout<<"b="<<b<<endl ;
--------------------------------------…
I want to know to know how the result came?
so please explain me.
int a, b=3 ;
cin >> a ;
if (a)
b=a++ -1;
cout<<"a="<<a<<endl ;
cout<<"b="<<b<<endl ;
--------------------------------------…
I want to know to know how the result came?
so please explain me.
Answer:
if (a), when a is either positive or negative as int x = -1;
if (x)
cout << "R";// This will be printed => Condition is true.
When anything within brackets is +ve or negative in if's brackets condition is considered as true.
Similarly if within the bracket of if 0 comes, the condition is False i.e.
if (0)
cout << "R"; // R will not be printed.
In your program, when user enters 6, a number other than ZERO, condition becomes true and the thing with in the body of if is executed => b's value will be replaced by (6-1) = 5. (if user enters 6 as 'a')
When user enters 0, the statement within the if body will not be executed, and value stored in b will remain unchanged (i.e. b = 3).
(i) a = 6 + 1 = 7 // 6 + 1 because it was incremented.
b = 6 - 1 = 5
(ii) a = 0
b = 3.
Question:
1. Which of the following is true?
A. 1
B. 66
C. .1
D. -1
E. All of the above
if (x)
cout << "R";// This will be printed => Condition is true.
When anything within brackets is +ve or negative in if's brackets condition is considered as true.
Similarly if within the bracket of if 0 comes, the condition is False i.e.
if (0)
cout << "R"; // R will not be printed.
In your program, when user enters 6, a number other than ZERO, condition becomes true and the thing with in the body of if is executed => b's value will be replaced by (6-1) = 5. (if user enters 6 as 'a')
When user enters 0, the statement within the if body will not be executed, and value stored in b will remain unchanged (i.e. b = 3).
(i) a = 6 + 1 = 7 // 6 + 1 because it was incremented.
b = 6 - 1 = 5
(ii) a = 0
b = 3.
Question:
1. Which of the following is true?
A. 1
B. 66
C. .1
D. -1
E. All of the above
No comments:
Post a Comment