#### Programming Exercise

Functions Programming Exercise P-6

Array Programming Exercise P-7

#### Q.17 Write a program that calculates the sum of the following series using function: // x – x2/2! + x3/3! – x4/4! + x5/5! .. . . . . + x8/8! – x9/9!// Input the value of ‘x’ from user.

```#include<iostream.h>
#include<conio.h>
#include<math.h>
main()
{
int x;
double s = 0;
unsigned long fact(int);
clrscr();
cout<<"Enter the value of x : ";
cin>>x;

for(int i = 1; i<=9; i++)
{
if(i%2==1)
s = s + pow(x,i)/fact(i);
else
s = s - pow(x,i)/fact(i);
}
cout<<"Sum of series is : "<<s;
getch();
}

// definition of the fact() function

unsigned long fact(int n)
{
long f = 1, c = 1;

while(c<=n)
{
f = f * c;
c = c + 1;
}
return f;
}```

#### Q.18 Write a complete program using function. The function should take 5 numbers as arguments and return its product.

```#include<iostream.h>
#include<conio.h>
main()
{
int a, b, c, d, e;
unsigned long prod(int, int, int, int, int);
clrscr();
cout<<"Enter first number ? ";
cin>>a;
cout<<"Enter second number ? ";
cin>>b;
cout<<"Enter third number ? ";
cin>>c;
cout<<"Enter fourth number ? ";
cin>>d;
cout<<"Enter fifth number ? ";
cin>>e;
cout<<"Product of these 5 numbers is : "<<prod(a,b,c,d,e);
getch();
}

// definition of the prod() function

unsigned long prod(int n1, int n2, int n3, int n4, int n5)
{
return n1*n2*n3*n4*n5;
}```

#### Q.19 Write a program that checks whether a number is prime number, even number, or odd number using function.

```#include<iostream.h>
#include<conio.h>
main()
{
void check(int);
long n;
clrscr();
cout<<"Enter an integer number : ";
cin>>n;
check(n);
getch();
}
// definition of check() function

void check(int x)
{
int d = 2, p = 1;
while(d < x)
{
if (x%d ==0)
{
p = 0;
break;
}
d++;
}
if(p == 1 && x%2 == 0)
cout<<x<<" is prime and even number ";
else if(p == 1 && x%2 == 1)
cout<<x<<" is prime and odd number ";
else if(x%2 == 0)
cout<<x<<" is only even number ";
else if(x%2 == 1)
cout<<x<<" is only odd number ";
}```

#### Q.20 Write a program that inputs two values in the main() function and passes these values to a function ‘LCM’. The function finds out the LCM of these values and returns the result to the main() function.

```#include<iostream.h>
#include<conio.h>
main()
{
double LCM(int, int);
int n, m;
clrscr();
cout<<"Enter first value ? ";
cin>>m;
cout<<"Enter second value ? ";
cin>>n;
cout<<"LCM of two values : "<<LCM(m, n)<<endl;
getch();
}

// definition of LCM() function

double LCM(int x, int y)
{
int i, GCD, res, g = 1;
clrscr();
i = 2 ;
while(i<=x)
{

if(x%i== 0 && y%i== 0)
g = i;
GCD = g;
i++;

}
res = (x * y)/GCD;
return res;
}```

#### Q.21 Write a program that inputs three values in the main() function and passes these values to a user-defined function. The function finds out the smallest of these values and returns this value to the main() function.

```#include<iostream.h>
#include<conio.h>
main()
{
int smallest(int, int, int);
int a, b, c, mini;
clrscr();
cout<<"Enter first number ? ";
cin>>a;
cout<<"Enter second number ? ";
cin>>b;
cout<<"Enter third number ? ";
cin>>c;
mini = smallest(a, b, c);
cout<<"Smallest value is : "<<mini;
getch();
}
// definition of smallest() function

int smallest(int x, int y, int z)
{

int m;
if(x<y)
if(x<z)
m = x;
else
m = z;
else
if(y<z)
m = y;
else
m = z;
return m;
}```

#### Q.22 Write a program that inputs an integer in the main() function and passes this value to a user-defined function. The function should find out the sum of the first and last digit of this number. For example, if input integer is 94386, sum of first and last digits is 15.

```#include<iostream.h>
#include<conio.h>
main()
{
int sum(int);
unsigned long n;
clrscr();
cout<<"Enter an integer value ? ";
cin>>n;
cout<<"Sum of first and last digits of the number : "<<sum(n);
getch();
}
// definition of sum() function

int sum(int x)
{

int first, last, s;
first = x % 10;
while(x!=0)
{
last = x;
x = x /10;
}

s = first + last;
return s;
}```

#### Q.23 Write a program that inputs an integer and passes it to a function. The function should return the number of digits in the integer. For example, if the integer is 35, the function should return 2 and if integer is 3572, the function should return 4.

```#include<iostream.h>
#include<conio.h>
main()
{
int digits(int);
long n;
clrscr();
cout<<"Enter an integer value ? ";
cin>>n;
cout<<"Number of digits :: "<<digits(n);
getch();
}
// definition of digits() function

int digits(int x)
{

int i = 0;
while(x!=0)
{
x = x /10;
i++;
}

return i;
}```