83.2k views
1 vote
Write a function named print_sum that accepts three numbers as parameters and prints the sum.

1 Answer

7 votes

Answer:

Complete C++ code along with output and comments for explanation is provided below.

C++ Code with Explanation:

#include <iostream>

using namespace std;

// implement a function named print_sum which takes three input numbers of type int and returns the sum of these three numbers.

int print_sum(int num1, int num2, int num3)

{

// initialize the variable to hold the value of sum

int sum;

// add the three numbers and store the result in the variable sum

sum = num1 + num2 + num3;

// print the sum of three numbers

cout<<"The sum of three numbers is: "<<sum<<endl;

}

// driver code starts here

int main()

{

// initialize the three input numbers

int x,y,z;

// get the three numbers from the user

cout << "Please enter number 1: "<<endl;

cin >> x;

cout << "Please enter number 2: "<<endl;

cin >> y;

cout << "Please enter number 3: "<<endl;

cin >> z;

// call the function print_sum to calculate the sum

print_sum(x,y,z);

return 0;

}

Output:

Please enter number 1:

24

Please enter number 2:

13

Please enter number 3:

7

The sum of three numbers is: 44

Write a function named print_sum that accepts three numbers as parameters and prints-example-1
User Rick Rat
by
6.7k points