Back Button
Back to Chapter
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.
Collapse_Icon
Table of Contents

Now let us apply what we've learned and make simple programs to perform basic mathematical operations. Don't just copy-paste the code try writing the code on your own use these codes as a reference. In these examples I've used only 2 inputs, try to solve the same problems by taking multiple inputs.

Questions

  1. Write a program to Prompt the user to input 2 values and display the difference.
  2. Write a program to prompt the user to input 2 values and display the sum.
  3. Write a program to prompt the user to input 2 values and display the product.
  4. Write a program to prompt the user to input 2 variables and display the quotient and remainder.
  5. Write a program to prompt the user to input marks in 3 subjects( maths, physics, and chemistry) and display the average marks.

Solutions:

Answer 1

#include using namespace std; main() { int a; int b; cout<<"enter the value of a:"; cin>>a;//the value entered by the user is stored in the 'a' container cout<<"enter the value of b:"; cin>>b;//the value entered by the user is stored in the 'b' container cout<<"the difference is:"<
Output: enter the value of a:5 enter the value of b:9 the difference is:-4

Answer 2

#include using namespace std; main() { int a; int b; cout<<"enter the value of a:"; cin>>a;//the value entered by the user is stored in the 'a' container cout<<"enter the value of b:"; cin>>b;//the value entered by the user is stored in the 'b' container cout<<"the sum is:"<
Output: enter the value of a:5 enter the value of b:9 the sum is:14

Answer 3

#include using namespace std; main() { int a; int b; cout<<"enter the value of a:"; cin>>a;//the value entered by the user is stored in the 'a' container cout<<"enter the value of b:"; cin>>b;//the value entered by the user is stored in the 'b' container cout<<"the product is:"<
Output: enter the value of a:5 enter the value of b:9 the product is:45

Answer 4

#include using namespace std; main() { int a; int b; cout<<"enter the value of a:"; cin>>a;//the value entered by the user is stored in the 'a' container cout<<"enter the value of b:"; cin>>b;//the value entered by the user is stored in the 'b' container cout<<"the quotient is:"<
Modulus operator(%) is used for finding the remainder when the number on the left side of the operator is divided by the number on the right side. In this example, 18 is completely divisible by therefore, the remainder is zero.
Note that division is possible only if a is greater than b, in the next chapter we will learn how we can resolve this problem and divide any 2 numbers given to us using conditional statements and relational operators.

Answer 5

If you successfully solve these problems, congrats!!! You've mastered the basics of coding.