Add two numbers using ++ operator in C++.


In programming, the ++ operator is the increment operator that increases the value of the operand by 1. We can add two numbers using this operator by adding 1 to the number a, b number of times.

Example,

Input: a = 31 , b = 4
Output: 35

Explanation − adding 1 to 31 four times, sums up to 31 +1+1+1+1 = 35.

Algorithm

Input: two integers a and b.
Step 1: loop from 0 to b and follow step 2.
Step 2: add 1 to b.
Step 3: print the value of a.

Example

 Live Demo

#include <iostream>
using namespace std;
int main(){
   int x = 324 , y= 76;
   cout<<"The sum of "<<x<<" & "<<y;
   if(y>0){
      for(int i= 0; i<y;i++){
         x++;
      }
   } else {
      for(int i= y; i<0;i++){
         x--;
      }
   }
   cout<<" is "<<x;
   return 0;
}

Output

The sum of 324 & 76 is 400

Updated on: 19-Sep-2019

640 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements