COMPUTER SCIENCE C++

1.e) Write the output of the following program.

#include<iostream.h>
int func(int &x,int y=10)
{ if(x%y==0) return ++x;else return y- -;
}
void main( )
{ int p=20,q=23;
q=func(p,q);
cout<<p<<q<<endl;
p=func(q);
cout<<p<<q<<endl;
q=func(p);
cout<<p<<q<<endl;
}

3

Ans: Output:

2023
1023
1111

f) Write a function seqsum( ) in C++ with two arguments, double x and int n. The function should return a value of type double and it should find the sum of the following series. 1+ x/2! + x2/4! + x3/6! + x4/8! + x5/10! + ---- + xn/(2n)!

#include<iostream.h>
#include<math.h>
#include<conio.h>
double seqsum(int x1,int m1);
void main()
{ int x;
int m;
clrscr();
cout<<"Enter the vaue of X and M";
cin>>x>>m;
cout<<"\nThe sum of the series =
"<<seqsum(x,m);
getch();
}
double seqsum(int x1,int m1)
{ double sum=1;
for(int i=1;i<=m1;i++)
{ int f=1;
for(int j=1;j<=2*i;j++)
{ f=f*j;
}
sum=sum+pow(x1,i)/f;
}
return sum;
}

4

1999 Annual Paper:

1.a) Why main( ) function is so special. Give two reasons?

1

Ans: Execution of the program starts and ends at main( ). The main( ) is the driver function of the program. If it is not present in a program, no execution can take place.

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )