C++ Functions Practice
C++ Functions Practice
Code:
#include<iostream>
using namespace std;
int max3(int x, int y, int z);
int main()
{
int x,y,z,a;
cout<<"Enter the first integer: ";
cin>>x;
cout<<"Enter the second integer: ";
cin>>y;
cout<<"Enter the third integer: ";
cin>>z;
a= max3(x,y,z);
cout<<"Largest = "<<a<<endl;
Output:
Problem 2:
Write a program that first asks the user to input a positive integer N. It then computes
and prints the double number Z using the following series :
Your main() program should use a looping structure, and should call a user-defined
function int factorial (int x); to return the factorial of a positive integer number x to
be used in the computation of Z. Also note that all divisions in Z are real divisions.
All user inputs and printouts should be done in main().
Code:
#include <iostream>
using namespace std;
int factorial(int x)
{
int res = 1;
for (int i=2; i<=x; i++)
res *= i;
return res;
}
double sum(int x)
{
double sum = 0;
for (int i = 1; i <= x; i++)
if(i%2==0)
{
sum += -1.0/factorial(i);
}
else
{
sum += 1.0/factorial(i);
}
return sum;
}
int main()
{
int n;
cout<<"Enter the value for N: ";
cin>>n;
cout <<"Z = "<< sum(n)<<endl;
return 0;
}
Output: