C++ | Function overloading

Function Overloading with different data types of arguments

Question: 5:
C++ program to calculate and print the square of a number, the number may be an integer, float, or double
Sol

#include<iostream>
using namespace std;
int square(int n)
{
	return(n*n);
}
float square(float n)
{
	return(n*n);
}
double square(double n)
{
	return(n*n);
}
int main()
{
	int a1,a2;
	float b1,b2;
	double d1,d2;
	//int
	cout<<"Enter any no (int) ";
	cin>>a1;
	a2=square(a1);
	cout<<"square = "<<a2<<endl;
	//float
	cout<<"Enter any no (float) ";
	cin>>b1;
	b2=square(b1);
	cout<<"square = "<<b2<<endl;
	//double
	cout<<"Enter any no ";
	cin>>d1;
	d2=square(d1);
	cout<<"square = "<<d2<<endl;
	return(0);
}
/* Output */
Enter any no (int) 2
square = 4
Enter any no (float) 2.3
square = 5.29
Enter any no 6536.3698
square = 4.27241e+007

Question:6
C++ program to calculate and print the cube of a number, the number may be an integer, float, or double
Sol

#include<iostream>
using namespace std;
int cube(int n)
{
	return(n*n*n);
}
float cube(float n)
{
	return(n*n*n);
}
double cube(double n)
{
	return(n*n*n);
}
int main()
{
	int a1,a2;
	float b1,b2;
	double d1,d2;
	//int
	cout<<"Enter any no (int) ";
	cin>>a1;
	a2=cube(a1);
	cout<<"cube = "<<a2<<endl;
	//float
	cout<<"Enter any no (float) ";
	cin>>b1;
	b2=cube(b1);
	cout<<"cube = "<<b2<<endl;
	//double
	cout<<"Enter any no ";
	cin>>d1;
	d2=cube(d1);
	cout<<"cube = "<<d2<<endl;
	return(0);
}
/* Output */
Enter any no (int) 2
cube = 8
Enter any no (float) 3.2
cube = 32.768
Enter any no 2365.3698
cube = 1.32342e+010

Question: 7
C++ program to calculate and print sum of 2 numbers, the number may be an integer , float or double
Sol:

#include<iostream>
using namespace std;
int sum(int n1,int n2)
{
	return(n1+n2);
}
float sum(float n1,float n2)
{
	return(n1+n2);
}
double sum(double n1,double n2)
{
	return(n1+n2);
}
int main()
{
	int a1,a2,a3;
	float b1,b2,b3;
	double d1,d2,d3;
	//int
	cout<<"Enter 2 nos (int) ";
	cin>>a1>>a2;
	a3=sum(a1,a2);
	cout<<"sum = "<<a3<<endl;
	//float
	cout<<"Enter 2 nos (float) ";
	cin>>b1>>b2;
	b3=sum(b1,b2);
	cout<<"sum = "<<b3<<endl;
	//double
	cout<<"Enter 2 no2 (double) ";
	cin>>d1>>d2;
	d3=sum(d1,d2);
	cout<<"sum = "<<d3<<endl;
	return(0);
}
/* Output */
Enter 2 nos (int) 10
20
sum = 30
Enter 2 nos (float) 2.3
6.3
sum = 8.6
Enter 2 no2 (double) 23.36
563.3256
sum = 586.686