Write a program to sum of two numbers, sum of three numbers and sum of four numbers using a single function. Also find the product of two numbers, product of three numbers
and four numbers using a single function.
#include<iostream>
#include<cstdarg>
using namespace std;
double MakeSum(int n, ...)
{
double res=0;
va_list vl;
va_start(vl,n);
for(int i=0;i<n;i++)
{
res+=va_arg(vl,double);
}
va_end(vl);
return res;
}
double MakeMult(int n, ...)
{
double res=1;
va_list vl;
va_start(vl,n);
for(int i=0;i<n;i++)
{
res*=va_arg(vl,double);
}
va_end(vl);
return res;
}
int main()
{
double a,b,c,d;
cout<<"Please, enter a value a: ";
cin>>a;
cout<<"Please, enter a value b: ";
cin>>b;
cout<<"Please, enter a value c: ";
cin>>c;
cout<<"Please, enter a value d: ";
cin>>d;
cout<<"Sum of two numbers is\t\t"<<MakeSum(2,a,b);
cout<<"\nSum of three numbers is\t\t"<<MakeSum(3,a,b,c);
cout<<"\nSum of four numbers is\t\t"<<MakeSum(4,a,b,c,d);
cout<<"\nProduct of two numbers is\t"<< MakeMult(2,a,b);
cout<<"\nProduct of three numbers is\t"<< MakeMult(3,a,b,c);
cout<<"\nProduct of four numbers is\t"<< MakeMult(4,a,b,c,d);
}
Comments
Leave a comment