Answer to Question #273832 in C++ for sonu

Question #273832

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.


1
Expert's answer
2021-12-02T06:47:32-0500
#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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS