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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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

Expert Solution
Step 1

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;
}

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Types of Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education