Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’,evaluates the following expressions and displays results on screen.num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3))For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators(both prefixing and postfixing are allowed). You must remember that using increment/decrement operatorschanges the original value of a number. Indent your code and include comments for improving the readabilityof your code. Your program should have the following interface.Enter a number : 2020 + 5 = 2520 – 3 = 17(20 + 3)- 2 = 21((20 + 5)*2 / (20 + 3)) = 2Enter a number : 2020 + 5 = 2520 – 3 = 17(20 + 3)- 2 = 21((20 + 5)*2 / (20 + 3)) = 2
Question
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’,evaluates the following expressions and displays results on screen.num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3))For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators(both prefixing and postfixing are allowed). You must remember that using increment/decrement operatorschanges the original value of a number. Indent your code and include comments for improving the readabilityof your code. Your program should have the following interface.Enter a number : 2020 + 5 = 2520 – 3 = 17(20 + 3)- 2 = 21((20 + 5)*2 / (20 + 3)) = 2Enter a number : 2020 + 5 = 2520 – 3 = 17(20 + 3)- 2 = 21((20 + 5)*2 / (20 + 3)) = 2
Solution
Here is the C++ program as per your requirements:
#include<iostream>
using namespace std;
int main() {
// Declare the variable
int num;
// Prompt the user to enter an integer
cout << "Enter a number: ";
cin >> num;
// Perform the operations
int numPlusFive = num;
for(int i = 0; i < 5; i++) {
numPlusFive++;
}
cout << num << " + 5 = " << numPlusFive << endl;
int numMinusThree = num;
for(int i = 0; i < 3; i++) {
numMinusThree--;
}
cout << num << " - 3 = " << numMinusThree << endl;
int numPlusThreeMinusTwo = num;
for(int i = 0; i < 3; i++) {
numPlusThreeMinusTwo++;
}
for(int i = 0; i < 2; i++) {
numPlusThreeMinusTwo--;
}
cout << "(" << num << " + 3) - 2 = " << numPlusThreeMinusTwo << endl;
int numPlusFiveTimesTwoDivideNumPlusThree = numPlusFive * 2 / (num + 3);
cout << "((" << num << " + 5) * 2 / (" << num << " + 3)) = " << numPlusFiveTimesTwoDivideNumPlusThree << endl;
return 0;
}
This program first prompts the user to enter a number, then it performs the operations as per your requirements using only increment and decrement operators for addition and subtraction. The results are then displayed on the screen.
Similar Questions
Write a program that reads in a number (integer) as input, adds 1 to it, and then prints out the result. Here is an example interaction between your program and the user:Please enter a number: 34
Write a program that does basic arithmetic operations (addition, subtraction, multiplication,and division). The inputs to the program are two numbers (in double format) and theoperation required. Provide a function for each operation and the identifiers for addition,subtraction, multiplication, and division are ‘+’, ‘-‘, ‘*’, and ‘/’ respectively.
Create a mini calculator which has the ability to add, subtract, multiply and divide. Follow the steps below to complete this assignment.Step 1: Your code should be able to add, subtract, divide and multiplyStep 2: Your program should ask the user for two numbers.Step 3: Your code should calculate and print the output for all the calculations. Use appropriate variables and output names.
Write a program to enter two numbers and print the arithmeticoperations like +,-,*,**, /, // and % using function
Daniel is working on a program for mathematical operations. Create a program that takes two floating-point numbers, multiplies them, converts the result to an integer, and based on the user's choice:If the choice is 1, add 5 to the converted integer.If the choice is 2, subtract 5 from the converted integer.If the choice is 3, divide the converted integer by 2.Display the original multiplication result as a float with two decimal places, and then show the modified integer after the chosen operation. Input format :The first line of input consists of two float values: num1 and num2 separated by a space.The second line of input consists of an integer value n, representing Daniels's choice:If the choice is 1, add 5 to the converted integer.If the choice is 2, subtract 5 from the converted integer.If the choice is 3, divide the converted integer by 2.Output format :The first line displays "Multiplication Result (as float): " followed by the result of multiplying num1 and num2 as a float value with two decimal values.The second line displays "Converted Integer: " followed by the result as an integer value after performing the required operation on the converted result based on the choice.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.