C++ Write a code statment to read two integer values into varibales val1 and val2. To do so you need to do the follwing: if the first value is greater than the second, add 20 to val1 and 50 to val2; othervise subtract 30 from val1 and add 9 to val 2 and print result
C++
Write a code statment to read two integer values into varibales val1 and val2. To do so you need to do the follwing: if the first value is greater than the second, add 20 to val1 and 50 to val2; othervise subtract 30 from val1 and add 9 to val 2 and print result
You are asked to check for conditions that are true or false and based on the result perform some arithmetic operations.
- The condition is to check whether the given value in the variable 1(Val1) is greater than the second variable(val2)
- Based on the conditions
- IF TRUE:
- Add 20 to variable (val1)
- Add 50 to variable(val2)
- IF FALSE:
- Subtract 30 from variable 1(val1)
- Add 9 to variable 2(val)
C++ Code:
#include <iostream>
using namespace std;
int main()
{
int val1, val2;
cout << "Enter two values: " << endl;
cin >> val1 >> val2;
if(val1 > val2) //Here if condition will check whether the val1 is greater than val2 is TRUE OR NOT
{
//This block will execute if it's True
val1 += 20; //here val1 += 20 can also be written as val1 = val1 + 20
val2 += 50; //here val2 += 50 can also be written as val2 = val2 + 50
cout<<"Value1 is: "<<val1<<endl;
cout<<"Value2 is: "<<val2<<endl;
}
else
{
//This block will execute if it's FALSE
val1 -= 30; //here val1 -= 30 can also be written as val1 = val1 + 30
val2 += 9; //here val2 += 9 can also be written as val2 = val2 + 9
cout<<"Value1 is: "<<val1<<endl;
cout<<"Value1 is: "<<val2<<endl;
}
return 0;
}
Step by step
Solved in 2 steps with 1 images