Program to find sum of two numbers in c++


Asked by admin @ in Computer Science viewed by 352 People


Write a program in C++ to display the sum of two numbers 20 and 30.

Answered by admin @



C++ Program to add two numbers

BY CHAITANYA SINGH | FILED UNDER: C++ PROGRAMS

In this tutorial, we will see three ways to add two numbers in C++. 1) Simple C++ program to add two numbers 2) adding numbers using function overloading 3) adding numbers using class and functions.

1) Simple C++ program to add two numbers

In this program we are asking user to input two integer numbers and then we are adding them and displaying the result on screen.

#include <iostream>

using namespace std;

int main(){

//Declaring two integer variables

int num1, num2;

/* cout displays the string provided in the

* double quotes as it is on the screen

*/

cout<<"Enter first integer number: ";

/* cin is used to capture the user input

* and assign it to the variable.

*/

cin>>num1;

cout<<"Enter second integer number: ";

cin>>num2;

cout<<"Sum of entered numbers is: "<<(num1+num2);

return 0;

}

Output:

Enter first integer number: 10

Enter second integer number: 20

Sum of entered numbers is: 30

2) C++ program to add two numbers using function overloading

In this example, we will see how to add two numbers using function overloading. Function overloading is a feature that allows us to have more than one function having same name but different number, type of sequence of arguments. Here we are defining three functions for the same purpose addition, based on the data type of arguments different function is called.

/* Function overloading example. Where we can have

* more than one functions with same name but different

* number, type or sequence of arguments

*/

#include <iostream>

using namespace std;

int sum(int, int);

float sum(float, float);

float sum(int, float);

int main(){

int num1, num2, x;

float num3, num4, y;

cout<<"Enter two integer numbers: ";

cin>>num1>>num2;

//This will call the first function

cout<<"Result: "<<sum(num1, num2)<< endl;

cout<<"Enter two float numbers: ";

cin>>num3>>num4;

//This will call the second function

cout<<"Result: " <<sum(num3, num4)<< endl;

cout<<"Enter one int and one float number: ";

cin>>x>>y;

//This will call the third function

cout<<"Result: " <<sum(x, y)<< endl;

return 0;

}

int sum(int a, int b){

return a+b;

}

float sum(float a, float b){

return a+b;

}

/* Remember that sum of int and float is float

* so the return type of this function is float

*/

float sum(int a, float b){

return a+b;

}

Output:

Enter two integer numbers: 21 88

Result: 109

Enter two float numbers: 10.2 30.7

Result: 40.9

Enter one int and one float number: 20 16.4

Result: 36.4


Similar Questions

Find two consecutive natural numbers whose sum is 75

Asked by admin @ in Math viewed by 342 persons

Find two consecutive natural number whose sum is 75 in step by step explanation

Find two consecutive natural numbers whose sum is 63

Asked by admin @ in Math viewed by 321 persons

Find two consecutive natural number whose sum is 63

C++ program to add two complex numbers using class

Asked by admin @ in Computer Science viewed by 401 persons

Write a c++ program to perform addition of two complex numbers using classes​

Sum and difference of two numbers in c hackerrank solution

Asked by admin @ in Math viewed by 379 persons

Sum and difference of two numbers hackerrank solution

The sum of two irrational numbers is always

Asked by admin @ in Math viewed by 355 persons

Is the sum of two irrational numbers always irrational? justify your answer.

The sum of two digit number is 9

Asked by admin @ in Math viewed by 334 persons

The sum of digits of a two-digit number is 9.Also,nine times this number is twice the number obtained by reversing the order of the digits.Find the number.

The sum of two irrational number is always

Asked by admin @ in Math viewed by 297 persons

Is the sum of two irrational numbers always irrational? justify your answer.

The sum of two numbers is 1000

Asked by admin @ in Math viewed by 318 persons

The sum of two numbers is 1000 and the difference between their squares is 25600 then find the numbers

The sum of the two digit number is 12

Asked by admin @ in Math viewed by 359 persons

The sum of the digit of two digit number is 12. The number obtained by interchanging the digits exceeds the given number by 18. Find the number

Find three consecutive even numbers whose sum is 96

Asked by admin @ in Math viewed by 381 persons

What is three consecutive even numbers whose sum is 96.

The sum of a two digit number is 12

Asked by admin @ in Math viewed by 416 persons

The sum of the digit of two digit number is 12. The number obtained by interchanging the digits exceeds the given number by 18. Find the number

Find 4 numbers in ap whose sum is 20

Asked by admin @ in Math viewed by 397 persons

Find 4 numbers in ap whose sum is 20 and the sum of whose square is 120

Write a program to find average of three numbers

Asked by admin @ in Computer Science viewed by 336 persons

Write a program to find average of three numbers.

Sum of digits of two digit number is 9

Asked by admin @ in Math viewed by 352 persons

Sum of digits of a two-digit number is 9. when we interchange the digits, it is found that the resulting new number is greater than the original number by 27. …

The sum of a two digit number is 9

Asked by admin @ in Math viewed by 347 persons

The sum of digits of a two-digit number is 9.Also,nine times this number is twice the number obtained by reversing the order of the digits.Find the number.

Most viewed questions in Computer Science


A programmer mistakenly writes gor instead of for

Asked by admin @ in Computer Science viewed by 19017 persons


Domestic data entry operator ncert book class 9 solutions

Asked by admin @ in Computer Science viewed by 15802 persons


Information technology code 402 class 9 solutions chapter 1

Asked by admin @ in Computer Science viewed by 10968 persons



Ncert solutions for class 8 computer chapter 1 computer system

Asked by admin @ in Computer Science viewed by 10399 persons


A scrum team works on a 4 weeks sprint

Asked by admin @ in Computer Science viewed by 8720 persons


Which of the following occupies more memory in c

Asked by admin @ in Computer Science viewed by 8134 persons



A database of motor vehicles has the base entity

Asked by admin @ in Computer Science viewed by 7839 persons


The network architecture can be termed as a mcq

Asked by admin @ in Computer Science viewed by 6483 persons


Cat5 and cat6 cabling refer to which reference model layer

Asked by admin @ in Computer Science viewed by 5786 persons



Class 7 computer chapter 3 questions and answers

Asked by admin @ in Computer Science viewed by 5687 persons


Choose the correct options about usability and user experience

Asked by admin @ in Computer Science viewed by 5472 persons


Which advocates daily team meetings for coordination and integration

Asked by admin @ in Computer Science viewed by 4979 persons



Which three security features match the database security level

Asked by admin @ in Computer Science viewed by 4969 persons


Which of the dbms package is not available

Asked by admin @ in Computer Science viewed by 4681 persons


________ aid a business grow and attain its objectives

Asked by admin @ in Computer Science viewed by 4485 persons