Write a program to overload function "sum" which calculates sum of the squares of the inputs if two inputs are provided and sum of the cubes of the input if three inputs are provided.
Aug 31, 2018Program:
#include<iostream> using namespace std; // function prototypes int sum(int n1, int n2); int sum(int n1, int n2, int n3); int main(){ int num1 = 10, num2 = 20, num3 = 30; cout<<"Calling two input sum result = "<<sum(num1, num2)<<endl; cout<<"Calling three input sum result = "<<sum(num1, num2, num3)<<endl; return 0; } // function defination int sum(int n1, int n2){ return n1 * n1 + n2 * n2; } int sum(int n1, int n2, int n3){ return n1 * n1 * n1 + n2 * n2 * n2 + n3 * n3 * n3; }
Sample Run:
Calling two input sum result = 500 Calling three input sum result = 36000