Write a C++ program to find the sum of the given variables using the function with default arguments.
The algorithm to implement the program of above problem:
1) Start
2) Declare the variables and functions.
3) Give the values for two arguments in the function declaration itself.
4) Call function sum() with three values such that it takes one default arguments.
5) Call function sum() with two values such that it takes two default arguments.
6) Call function sum() with one values such that it takes three default arguments
7) Inside the function sum(), calculate the total.
8) Return the value to the main() function.
9) Display the result.
10) Stop.
Implementation of the above problem:
#include<iostream>
using namespace std;
int main()
{
float sum(float p, int q=10, int r=15, int s=20);
int a=2,b=3,c=4,d=5;
cout<<"Sum="<<sum(0)<<endl;
cout<<"Sum="<<sum(a,b,c,d)<<endl;
cout<<"Sum="<<sum(a,b,c)<<endl;
cout<<"Sum="<<sum(a,b)<<endl;
cout<<"Sum="<<sum(a)<<endl;
cout<<"Sum="<<sum(b,c,d);
return 0;
}
float sum(float i, int j, int k, int l)
{
return(i+j+k+l);
}
The Output of the above program is tested on www.jdoodle.com
Output:
Sum=45
Sum=14
Sum=29
Sum=40
Sum=47
Sum=32
Thanks
Mukesh Rajput
Post A Comment:
0 comments: