C++ Examples

  1. Write a C++ Program to print a welcome text in a separate line.

Addition of two numbers using call by reference in C++

#include <iostream>
using namespace std;

    //function prototype
    
int add(int *num1, int *num2);

int main()
{
    int num1,num2,sum;
    
    //Two numbers from the users will be taken
    
    cout<<"Enter your first number: "<<endl;
    cin>>num1;
    cout<<"Enter your second number: "<<endl;
    cin>>num2;
    
    //Sum to store values from the functions
    
    sum = add(&num1,&num2);
    
    cout<<"The sum of two numbers are: "<<sum;

    return 0;
}

    //add function definations

int add(int *num1, int *num2)
{
    int result = *num1 + *num2;
    
    return result;
}

Output

Enter your first number: 
5
Enter your second number: 
4
The sum of two numbers are: 9
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments