35.1k views
16 votes
Write a C++ program to add two integers. Make a function add() to add integers and display sum in main() function.

User Btimby
by
3.6k points

1 Answer

7 votes

Answer:

In C++:

#include <iostream>

using namespace std;

int add(int a, int b){

int c = a + b;

return c;

}

int main(){

int a, b;

cout<<"Enter two numbers: ";

cin>>a;

cin>>b;

cout<<add(a,b);

return 0;

}

Step-by-step explanation:

The add() function begins here

int add(int a, int b){

This adds the two integers

int c = a + b;

This returns the sum of the two integers

return c;

}

The main begins here

int main(){

This declares two numbers

int a, b;

This prompts user for two numbers

cout<<"Enter two numbers: ";

The next two lines gets the two numbers

cin>>a;

cin>>b;

This gets the sum of the two numbers from the add() function. The sum is also printed

cout<<add(a,b);

return 0;

}

User TheSnooker
by
3.6k points