Chapterd 5 c Solutions
-
Upload
azharjaved -
Category
Documents
-
view
216 -
download
0
Transcript of Chapterd 5 c Solutions
-
8/10/2019 Chapterd 5 c Solutions
1/57
Let Us C(Chapter 5)[D] a.Write a function to calculate the factorial value of any integer entered through thekeyboard.
Ans:-#include
long Factorial(int);
int main(int argc, char *argv[]){
int num;long i;printf("Enter a number: ");scanf("%d",&num);
i=Factorial(num);printf("Factorial of %d is: %ld\n",num,i);
return 0;}
long Factorial(int num){
int i;long j=1;for(i=1;i
-
8/10/2019 Chapterd 5 c Solutions
2/57
}
long power(int a,int b){
int i;long j=1;
for(i=1;i
-
8/10/2019 Chapterd 5 c Solutions
3/57
for(i=1;i
-
8/10/2019 Chapterd 5 c Solutions
4/57
a=a-10*(i-1);for(i=1;i
-
8/10/2019 Chapterd 5 c Solutions
5/57
for(i=1;i
-
8/10/2019 Chapterd 5 c Solutions
6/57
{if(yr%400==0)
printf("Leap Yr");else if(yr%4==0&&yr%100!=0&&yr%400!=0)
printf("Leap Yr");else
printf("Not Leap Yr");}
[D] e.A positive integer is entered through the keyboard. Write a function to obtain the primefactors of this number.
Ans:-#include #includevoid prime(int);
int main(int argc, char *argv[]){ int num;
printf("\nEnter a number:");scanf("%d",&num);prime(num);
getch();return 0;
}
void prime(int num){
int i,j;
for(i=1;i
-
8/10/2019 Chapterd 5 c Solutions
7/57
[F] b.Write a function that receives 5 integers and returns the sum, average and standarddeviation of these numbers. Call this function from main( ) and print the results in main( ).Ans:-#include #include
void std_avg(float*,float*,float*,int*);int main(int argc, char *argv[]){
int num[4],i;float std,avg,sum; /*They are variable*/printf("\nEnter 5 number:");
for(i=0;i
-
8/10/2019 Chapterd 5 c Solutions
8/57
(1) Without using recursion(2) Using recursion
Ans:-#include int sum(int);int main(int argc, char *argv[]){ int num,s;
printf("\nEnter a number:");scanf("%d",&num);
s=sum(num);printf("Sum is %d ",s);return 0;
}
//Sum using Function
/*
Left As an Exercise For You*/
//Sum of digits of a given number using Recursionint sum(int g){ int f,h;
if(g==0)return 0;
else{ h=g%10;
g=g/10;f=h+sum(g);
}
return f;}
[J] b.A positive integer is entered through the keyboard, write a program to obtain the primefactors of the number. Modify the function suitably to obtain the prime factors recursively.
Ans:-#includevoid prime(int);void prime_rec(int);
int main(){
int x;printf("\nInput an integer\n");scanf("%d",&x);printf("Prime Factors Are: ");prime_rec(x);
}
-
8/10/2019 Chapterd 5 c Solutions
9/57
/*Prime Factor Without Recursion*/void prime(int num){
int i,j;
for(i=1;i
-
8/10/2019 Chapterd 5 c Solutions
10/57
}}
}}
}
[J] c.Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In aFibonacci sequence the sum of two successive terms gives the third term. Following are thefirst few terms of the Fibonacci sequence:1 1 2 3 5 8 13 21 34 55 89...
Ans:-#include void recursive(int,int,int);int main(int argc, char *argv[]){ int a,b,d;
a=b=1;d=25;printf("Fb series is ");recursive(d-1,a,b);return 0;
}
void recursive(int g,int a,int b){ int c;
if(g==25){printf("%d %d ",a,b);recursive(g-1,a,b); /*used so that function can be called more than once*/
}else
{printf("%d ",a+b);c=a;a=b;b=c+b;if(g>0) /*Necessary to terminate the function call*/recursive(g-1,a,b);}
}
[J] d.A positive integer is entered through the keyboard, write a function to find the binaryequivalent of this number using recursion.Ans:-#include void binary(int);int main(int argc, char *argv[])
-
8/10/2019 Chapterd 5 c Solutions
11/57
{ int no;printf("Enter the no");scanf("%d",&no);binary(no);
return 0;}
void binary(int n){ int y;
if(n>0){ y=n;
n=n/2;binary(n);printf("%d",y%2);
}}
[J] e.Write a recursive function to obtain the running sum of first 25 natural numbers.Ans:-#include int sum(int,int);int main(int argc, char *argv[]){
int no,s1;printf("Enter the number");scanf("%d",&no);
s1=sum(no,1);printf("%d",s1);return 0;
}
int sum(int s,int i){
int y;/*Change the value of i to find the consecutive sum of Nthdigit starting from s i.e. s+(s+1)+...(s+N)*/if(i==6)
{ y=0;return 0;
}else{ i=i+1;
y=s+sum(s+1,i);}return y;
-
8/10/2019 Chapterd 5 c Solutions
12/57
}
[J] f.(f) Write a C function to evaluate the series
to five significant digits.Ans:-#include #include#includeunsigned int findFactorial(int);float sin1(float x);int main(int argc, char *argv[]){
float f1,no;printf("Enter the no");scanf("%f",&no);f1=sin1(no);
printf("%f",f1);getch();return 0;
}
//Function for Finding The Sine Seriesfloat sin1(float y){ int i,j;
unsigned int k;float sum;
j=0;sum=0;for(i=1;i
-
8/10/2019 Chapterd 5 c Solutions
13/57
{int i,f=1;
for(i=1;i 0;j--)printf("%d",binaryNumber[j]);
return 0;
}
[J] k.Write a function to compute the greatest common divisor given by Euclids algorithm,exemplified for J = 1980, K = 1617 as follows:
http://3.bp.blogspot.com/-kvhUpMuzDYo/Ug5kkVTsCRI/AAAAAAAAAgk/-DCJtOsMLt0/s1600/Untitled.png -
8/10/2019 Chapterd 5 c Solutions
14/57
Thus, the greatest common divisor is 33.Ans:-#include void gcd(int,int);int main(int argc, char *argv[]){ int x1,y1;
printf("Enter the no.");scanf("%d%d",&x1,&y1);gcd(x1,y1);return 0;
}
void gcd(int x,int y){
int n,j;
j=x/y; /*divide by zero*/n=x-j*y;
if(n==0)printf("GCD is %d ",y);if(n>0)gcd(y,n);
}
/*
Enjoy The WorldHappy Coding
*/
-
8/10/2019 Chapterd 5 c Solutions
15/57
[D] Answer the following:
(a)Write a function to calculate the factorial value of any integerentered throughthe keyboard.
hide
Answer:#include
main()
{
int i, f;
int factorial();
printf("Enter the number to evaluate its factorial:");
scanf ("%d", &i);
f=factorial(i);
printf("%d! = %d\n", i, f);
}
factorial(int num)
{int temp, fact;
for (temp=1,fact=1;temp
-
8/10/2019 Chapterd 5 c Solutions
16/57
Answer : #include
main()
{
int power (a,b);
int a, b, result;
printf("Enter the value of a and b:");
scanf ("%d %d", &a, &b);
result=power(a,b);
printf("%d raised to %d is %d", a, b, result);
}
power (int a, int b)
{
int calculation=1, calc;
for (calc=1; calc
-
8/10/2019 Chapterd 5 c Solutions
17/57
hide
-
8/10/2019 Chapterd 5 c Solutions
18/57
Answer:#include
main()
{
int year;
int convert (int year);
{
printf("Note:Enter a four year digit year.\n\n");
printf("Enter the year that you wanna convert to Roman: " );
scanf ("%d", &year);
if (year> 1999)
{
printf("Invalid Year.Please enter again.\n\n");
}
}
convert(year);
}
convert(int year)
{
int i;
printf("\nYear converted to Roman:");
i=(year/1000); //thousands place
if(i==1)
-
8/10/2019 Chapterd 5 c Solutions
19/57
{
printf("m");
}
i=((year/100)%10); //hundreds place
switch (i)
{
case 1:
printf("c");
break;
case 2:
printf("cc");
break;
case 3:
printf("ccc");
break;
case 4:
printf("cd");
break;
case 5:
printf("d");
break;
case 6:
printf("dc");
break;
case 7:
-
8/10/2019 Chapterd 5 c Solutions
20/57
printf("dcc");
break;
case 8:
printf("dccc");
break;
case 9:
printf("dcccc"); //this part you may think is wrong..9 -> cm
break; //but i have taken a hint from the example in the question.
}
i=((year/10)%10); //tens place
switch(i)
{
case 1:
printf("x");
break;
case 2:
printf("xx");
break;
case 3:
printf("xxx");
break;
case 4:
printf("xl");
break;
-
8/10/2019 Chapterd 5 c Solutions
21/57
case 5:
printf("l");
break;
case 6:
printf("lx");
break;
case 7:
printf("lxx");
break;
case 8:
printf("lxxx");
break;
case 9:
printf("lxxxx"); //had it not been for this example, it would have been xc
break;
}
i=year%10; //ones place
switch(i)
{
case 1:printf("i");
break;
case 2:
printf("ii");
-
8/10/2019 Chapterd 5 c Solutions
22/57
break;
case 3:
printf("iii");
break;
case 4:
printf("iv");
break;
case 5:
printf("v");
break;
case 6:
printf("vi");
break;
case 7:
printf("vii");
break;
case 8:
printf("viii");
break;
case 9:
printf("ix");
break;
}
printf ("\n\n");
-
8/10/2019 Chapterd 5 c Solutions
23/57
return 0;
}
(d) Any year is entered through the keyboard. Write a function todetermine whether the year is a leap year or not.
hide
-
8/10/2019 Chapterd 5 c Solutions
24/57
Answer: #include
main()
{
int leap_year(year);
int year, lp;
printf("Enter the year:");
scanf ("%d", &year);
lp=leap_year(year);
if (lp)
{printf("\nThe entered year is a leap year.");
}
else
{
printf("\nThe entered year is not a leap year.");
}
}
leap_year(int y)
{
int lp;
if (y%4==0)
{
lp=1;}
else
lp=0;
return(lp);
-
8/10/2019 Chapterd 5 c Solutions
25/57
}
(e) A positive integer is entered through the keyboard. Write a
function to obtain the prime factors of this number. For example,prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35are 5 and 7.
hide
-
8/10/2019 Chapterd 5 c Solutions
26/57
Answer:#include
main()
{
int number;
int prime(int number);
int primefactor(int number);
printf("Enter the number whose prime factors are to be calculated:");
scanf ("%d", &number);
primefactor(number);
}
//The following function detects a Prime number.
prime(int num)
{
int i, ifprime;
for (i=2; i
-
8/10/2019 Chapterd 5 c Solutions
27/57
{
int factor,ifprime;
for (factor=2; factor
-
8/10/2019 Chapterd 5 c Solutions
28/57
-
8/10/2019 Chapterd 5 c Solutions
29/57
(b) Write a function power ( a, b ), to calculate the value
of a raised to b.
Solution:
#include
#include
void main() {
int num1,num2 ;
clrscr();
printf("Please enter the value of a: ");
scanf("%d",&num1);
printf("\n\nPlease enter the value of b: ");
scanf("%d",&num2);
power(num1,num2);
getch();
}
power(int a , int b) {
int c=1,i;
for(i=1;i
-
8/10/2019 Chapterd 5 c Solutions
30/57
year into its roman equivalent. The following table shows
the roman equivalents of decimal numbers:
Decimal Roman
1 i
5 v
10 x
50 l
100 c
500 d
1000 m
Example:
Roman equivalent of 1988 is mdcccclxxxviii
Roman equivalent of 1525 is mdxxv
Solution:#include
#include
void main() {
int yr;
void func();
clrscr();
printf("Please enter the year: ");
scanf("%d",&yr);
printf("\n\nRoman Equivalent = ");
func(yr);
getch();
}
-
8/10/2019 Chapterd 5 c Solutions
31/57
void func(int y) {
int d1,d2,d3,d4,d5,d6,d7,a,b,c,d,e,f;
char thsnd='m',hndr_5='d',hndr='c',ffty='l',tn='x',fv='v',one='i';
/******* Roman Convertion ********/
/* To find all thousands */
d1=y/1000;
a=y%1000;
for(;d1>=1;d1--) {
printf("%c",thsnd);
if(a==0)
break;
}
/* To find all five-hundreds */
d2=a/500;
b=a%500;
for(;d2>=1;d2--) {
printf("%c",hndr_5);
if(b==0)
break;
}
/* To find all hundreds */
d3=b/100;
c=b%100;
for(;d3>=1;d3--) {
printf("%c",hndr);
if(c==0)
break;
}
/* To find all fifties */
/***********************/
d4=c/50;
-
8/10/2019 Chapterd 5 c Solutions
32/57
d=c%50;
for(;d4>=1;d4--) {
printf("%c",ffty);
if(d==0)
break;}
/* To find all tens */
/********************/
d5=d/10;
e=d%10;
for(;d5>=1;d5--) {
printf("%c",tn);
if(e==0)break;
}
/* To find all fives */
d6=e/5;
f=e%5;
for(;d6>=1;d6--) {
printf("%c",fv);
if(f==0)
break;
}
/* To find all ones */
for(d7=f;d7>=1;d7--) {
printf("%c",one);
}
}
------------------------------------------------------------------------------------------------------------
(d) Any year is entered through the keyboard. Write a
-
8/10/2019 Chapterd 5 c Solutions
33/57
function to determine whether the year is a leap year or
not.
Solution:
#include
#include
void main() {
int yr;
void func();
clrscr();
printf("Please enter the year: ");
scanf("%d",&yr);
func(yr);
getch();
}
void func(int y) {
if((y%4)==0)
printf("\nThis is a LEAP YEAR.\n");
else
printf("\nThis is NOT A LEAP YEAR.\n");
}
------------------------------------------------------------------------------------------------------------
(e) A positive integer is entered through the keyboard.
Write a function to obtain the prime factors of this
number.
For example, prime factors of 24 are 2, 2, 2 and 3,
whereas prime factors of 35 are 5 and 7.
Solution:
#include
-
8/10/2019 Chapterd 5 c Solutions
34/57
#include
void main() {
int i,j,k;
clrscr();
printf("enter the number: ");
scanf("%d",&j);
printf("\n\nprime factors:");
for(i=2;i
-
8/10/2019 Chapterd 5 c Solutions
35/57
-
8/10/2019 Chapterd 5 c Solutions
36/57
#include
void main() {
int d1,d2,d3,d4,d5,i,sum,avg;
float sd;
clrscr();
printf("enter five digits: \n\n");
scanf("%d%d%d%d%d",&d1,&d2,&d3,&d4,&d5);
func(d1,d2,d3,d4,d5,&sum,&avg,&sd);
printf("\tsum = %d\n\taverage = %d\n\tstandard deviation = %f ",sum,avg,sd);
getch();
}func(int a, int b, int c, int d, int e, int *s, int *av, float *ssd) {
float temp;
*s=a+b+c+d+e; /* sum of digits */
*av=(a+b+c+d+e)/5; /* average */
a=a-(*av);
b=b-(*av);
c=c-(*av);
d=d-(*av);
e=e-(*av);
temp=((a*a)+(b*b)+(c*c)+(d*d)+(e*e))/4;
/* standard deviation */
*ssd=sqrt(temp);
return 0;
}
------------------------------------------------------------------------------------------------------------
(c) Write a function that receives marks received by a
student in 3 subjects and returns the average and
percentage of these marks. Call this function from main(
) and print the results in main( ).
-
8/10/2019 Chapterd 5 c Solutions
37/57
Solution:
#include
#include
void main() {
int s1,s2,s3,*avg,*prcnt;void func();
clrscr();
printf("Please enter the marks of 3 subjects: \n");
scanf("%d%d%d",&s1,&s2,&s3);
func(s1,s2,s3,&avg,&prcnt);
printf(" Average = %d\nPercentage = %d%\n",avg,prcnt);
getch();
}
void func(int a, int b, int c, int *d, int *f) {
*d=(a+b+c)/3;
*f=(a+b+c)/3;
}
_____________________________________________________________________
Exercise [J]
a) A 5-digit positive integer is entered through the
keyboard, write a function to calculate sum of digits of
the 5-digit number:
(1) Without using recursion
(2) Using recursion
Solution:
#include
#include
void main() {
long num,s=0,ch;
clrscr();
-
8/10/2019 Chapterd 5 c Solutions
38/57
printf("Enter any number: ");
scanf("%ld",&num);
printf("\n\nChoose: 1: obtain sum of digits non-recursively\n\n");
printf(" 2: obtain sum of digits recursively\n\n\n");
printf("Your choice: ");
ch=getche();
switch(ch) {
case '1':
while(num!=0) {
s=s+(num%10);
num=num/10;
}
printf("\n\n\nsum of digits = %d\n",s);
break;
case '2':
s=sum(num);
printf("\n\n\nSum of digits = %d\n",s);
break;
}
getch();
}
sum(long n) {
static s=0;
if(n==0)return s;
else {
s=s+n%10;
n=sum(n/10);
-
8/10/2019 Chapterd 5 c Solutions
39/57
-
8/10/2019 Chapterd 5 c Solutions
40/57
break;
case 2:
printf("\n\n\nPrime factors: ");
factors(d);
break;
default:
printf("\n\nwrong input!");
break;
}
getch();
}
int factors (int n) {
int b=2;
if(n==1)
return 1;
else {
while(n!=1) {
if((n%b)==0) {
n=factors(n/b); /* recursive function */
printf("%d ",b);
}
else
b++;
}return n;
}
}
------------------------------------------------------------------------------------------------------------
-
8/10/2019 Chapterd 5 c Solutions
41/57
(c) Write a recursive function to obtain the first 25
numbers of a Fibonacci sequence. In a Fibonacci
sequence the sum of two successive terms gives the third
term. Following are the first few terms of the Fibonaccisequence:
1 1 2 3 5 8 13 21 34 55 89...
Solution:
#include
#include
void main() {
unsigned i,num=25,c=1;
clrscr();
for(i=0;i
-
8/10/2019 Chapterd 5 c Solutions
42/57
write a function to find the binary equivalent of this
number using recursion.
Solution:
#include
#include
void main() {
int num;
clrscr();
printf("Enter the number: ");
scanf("%d",&num);
printf("\n\n\nBinary equivalent: ");
binary(num);
gotoxy(20,7);
printf("
-
8/10/2019 Chapterd 5 c Solutions
43/57
#include
#include
void main() {
int i=25,j;
clrscr();
j=recsum(i);
printf("Addition of 25 natural numbers = %d",j);
getch();
}
int recsum(int n) {
if(n==1)return 1;
else
n = n + recsum(n-1); /* recursive addition */
return n;
}
------------------------------------------------------------------------------------------------------------
(f) Write a C function to evaluate the series
sin(x) = x - (x3/3!) + ( x5/5!) - (x7/7!) + ........
to five significant digits.
Solution:
------------------------------------------------------------------------------------------------------------
(g) Given three variables x, y, z write a function to
circularly shift their values to right. In other words if x =
5, y = 8, z = 10 after circular shift y = 5, z = 8, x =10
-
8/10/2019 Chapterd 5 c Solutions
44/57
after circular shift y = 5, z = 8 and x = 10. Call the
function with variables a, b, c to circularly shift values.
Solution:
#include
#include
void main() {
int x,y,z;
char choice;
void func();
clrscr();
printf("Please enter values of X,Y,Z\n");
scanf("%d",&x);
scanf("%d",&y);
scanf("%d",&z);
printf("\n\nBefore shift: X=%d Y=%d Z=%d\n",x,y,z);
func(&x,&y,&z); /* Call by reference */
printf("\n\nAfter shift: X=%d Y=%d Z=%d\n\n",x,y,z);
/* Circular Shifting continuously */
do {
printf("\nShift again(Y/N): ");
scanf(" %c",&choice);
clrscr();
func(&x,&y,&z);
printf("\nAfter another shift: X=%d Y=%d Z=%d\n\n",x,y,z);
}
while(choice=='y');
}
void func(int *a,int *b,int *c) {
int d,e,f;
d=*a;
e=*b;
-
8/10/2019 Chapterd 5 c Solutions
45/57
f=*c;
*a=f;
*b=d;
*c=e;
}
------------------------------------------------------------------------------------------------------------
(h) Write a function to find the binary equivalent of a
given decimal integer and display it.
Solution:
#include#include
void main() {
int num;
void binary();
clrscr();
printf("\t\tDecimal Integer to Binary conversion\n\n\n");
printf("Enter the number: ");
scanf("%d",&num);
printf("\n\n\nBinary equivalent: ");
binary(num);
gotoxy(20,10);
printf("
-
8/10/2019 Chapterd 5 c Solutions
46/57
}
------------------------------------------------------------------------------------------------------------
(i) If the lengths of the sides of a triangle are denoted by
a, b, and c, then area of triangle is given by
rootover ( S * (S-a) * (S-b) * (S-c))
where, S = ( a + b + c ) / 2
Solution:
#include
#include
#include
void main() {
int s1,s2,s3,s;
int area;
clrscr();
printf("enter 3 sides of triangle: \n\n");
scanf("%d%d%d",&s1,&s2,&s3);
s=s1+s2+s3/2;
area=func(s1,s2,s3,s);
printf("\narea = %d",area);
getch();
}
func(int i, int j, int k, int h) {
int ar,area;
ar=sqrt(h*((h-i)*(h-j)*(h-k)));
return (ar);
}
------------------------------------------------------------------------------------------------------------
-
8/10/2019 Chapterd 5 c Solutions
47/57
(j) Write a function to compute the distance between
two points and use it to develop another function that
will compute the area of the triangle whose vertices areA(x1, y1), B(x2, y2), and C(x3, y3). Use these functions
to develop a function which returns a value 1 if the point
(x, y) lines inside the triangle ABC, otherwise a value 0.
Solution:
------------------------------------------------------------------------------------------------------------
(k) Write a function to compute the greatest commondivisor given by Euclids algorithm, exemplified for J =
1980, K = 1617 as follows:
1980 / 1617 = 1
1980 1 * 1617 = 363
1617 / 363 = 4
1617 4 * 363 = 165
363 / 165 = 2
363 2 * 165 = 33
5 / 33 = 5
165 5 * 33 = 0
Thus, the greatest common divisor is 33.
Solution:
#include#include
void main() {
int a,b,r,d1,d2,temp;
clrscr();
printf("Enter first number: ");
scanf("%d",&a);
-
8/10/2019 Chapterd 5 c Solutions
48/57
printf("\n\nEnter second number: ");
scanf("%d",&b);
while(b!=0) {
r=a%b;
a=b;
b=r;
}
d1=a; /* devisor of first number */
temp=a;
a=b;
b=temp;
while(b!=0) {
r=a%b;
a=b;
b=r;
}
d2=a; /* devisor of second number */
printf("\n\n\nGreatest common devisor: ");
if(d1==d2) {
printf("%d",d1);
}
getch();
}
-
8/10/2019 Chapterd 5 c Solutions
49/57
Function C Programing
1.Write a function to calculate the factorial value of any integer entered
through the keyboard.
#include
#includelong fact(int n)
{
int i;
long f=1;
for (i=1;in)
{
printf("r is greter than n. ");
goto aa;
}
printf("\n %d! = %ld",n,fact(n));
printf("\n %dP%d = %ld",n,r,fact(n)/fact(n-r));
printf("\n %dC%d = %ld ",n,r,fact(n)/(fact(r)*fact(n-r)));
getch();
}
2.Write a function power (a,b) to calculate the value of a raised to b.
#include
#include
#include
double powr(int x,int y);void main()
{
int n;
double x,result;
clrscr();
printf("Programme for calculation of 'x' to the power 'n' : ");
printf("\nEnter a value for x : ");
-
8/10/2019 Chapterd 5 c Solutions
50/57
scanf("%lf",&x);
printf("\nEnter a intiger value for n : ");
scanf("%d",&n);
result = powr(x, n);
printf("\n%.2lf\ to the power %d is : %.3lf",x,n,result);
getch();}
double powr(int x,int y)
{
double c;
c= pow(a,b);
return c;
}
3.Write a function to calculate LCM of two numbers
#include
#includeint lcm(int x,int y);
int gcd(int p,int q);
void main()
{
int a,b;
clrscr();
scanf("%d%d",&a,&b);
printf("The lcm is %d",lcm(a,b));
getch();
}
int lcm(int x,int y)
{
int m,v;
m=gcd(x,y);
v=(x*y)/m;
return v;
}
int gcd(int p,int q)
{
int c;
do{c=p%q;
p=q;
q=c;
}
while(c!=0);
return p;
}
-
8/10/2019 Chapterd 5 c Solutions
51/57
4.Write a function to calculate GCD of two numbers.
#include
#include
int gcd(int a,int b);
void main()
{int a,b,c;
clrscr();
printf("Please enter two number for 'GCD' :");
scanf("%d%d",&a,&b);
c= gcd(a,b);
printf("GCD = %d.",c);
getch();
}
int gcd(int a,int b)
{ int c;
while(a!=0)
{
c=b%a;
b=a;
a=c;
}
return b;
}
5.Any year is entered through the keyboard. Write a function to determine
whether the year is a leap year or not.
#include
#include
int lyear(int year);
void main()
{
int year,c;
clrscr();
printf("Please enter a year to be tested : ");
scanf("%d", &year);
c=lyear(year);getch();
}
int lyear(int year)
{
if((year%4==0 && year%100 !=0)|| year%400==0)
printf("\n%d is a leap year.",year);
-
8/10/2019 Chapterd 5 c Solutions
52/57
else
printf("\n%d is not a leap year.",year);
return 0;
}
6.A prime integer is entered through the keyboard. Write a function to
obtain the prime factors of this number. For example, prime factors of 24
are 2, 2, 2 and 3 whereas prime factor of 35 are 5 and 7
#include
#include
int primf(int n);
void main()
{
int n,c;
clrscr();
printf("Prime factor : Enter a number :");
scanf("%d",&n);c=primf(n);
getch();
}
int primf(int n)
{
int i;
for(i=2;n!=1;i++)
{
if((n%i)==0)
{
n=n/i;
printf("%3d",i);
i=1;
}
}
return 0;
}
7.Write a function which receives a float and an int from main (), finds the
product of these two and returns the product which is printed through
main().
#include
#include
float prd(float x,int y);
void main()
{
float a,c;
-
8/10/2019 Chapterd 5 c Solutions
53/57
int b;
clrscr();
printf("Please enter a float & a intger number : ");
scanf("%f%d",&a,&b);
c=prd(a,b);
printf("Product = %.3f",c);getch();
}
float prd(float x,int y)
{
float d;
d =x*y;
return d;
}
8. Write a program which receives 5 integers and returns the sum,
average and standard deviation of these numbers. Call this function frommain() and print the results in main().
#include
#include
#include
int sum(int p,int q,int r,int s,int v);
float av1(int p,int q,int r,int s,int v);
double dv1(int p1,int q1,int r1,int s1,int v1);
float av;
void main()
{
int a,b,c,d,e,sum1;
long float dv;
clrscr();
printf("Enter five intger number :\n");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
sum1=sum(a,b,c,d,e);
av=av1(a,b,c,d,e);
dv=dv1(a,b,c,d,e);
printf("\n\nThe avarage is %.3f",av);
printf("\n\nThe sum is %d",sum1);printf("\n\nThe dev is %.3lf",dv);
getch();
}
float av1(int p,int q,int r,int s,int v)
{
-
8/10/2019 Chapterd 5 c Solutions
54/57
return (p+q+r+s+v)/5;
}
int sum(int p,int q,int r,int s,int v)
{
return (p+q+r+s+v);
}double dv1(int p1,int q1,int r1,int s1,int v1)
{
double t,g;
t=pow((av-p1),2)+pow((av-q1),2)+pow((av-r1),2)+pow((av-s1),2)+pow((av-v1),2);
g=pow((t/5),.5);
return g;
}
9. A 5 digit positive integer is entered through the keyboard, write a
function to calculate sum of digits of the 5 digit number.
(i) Using recursion
(ii) Without using recursion
Using recursion#include
#include
long int Sum(long int digits );
void main()
{
long int num,n,c;clrscr();
printf("\nEnter 5 digits number : ");
scanf("%ld",&n);
c = Sum(n);
printf("%ld",c);
getch();
}
long int Sum(long int digits )
{
if(digits < 10)
return digits;
else
return digits%10 + Sum(digits/10);
}
Without using recursion
-
8/10/2019 Chapterd 5 c Solutions
55/57
#include
#include
void main()
{
long int n;clrscr();
printf("\nEnter 5 digits number : ");
scanf("%ld",&n);
long int(long int n);
getch();
}
long int(long int n)
{
int rdgt;
long int num,sum = 0;
num = n;
do
{
rdgt = n%10;
n = n/10;
sum = (sum + rdgt);
}while(n !=0);
printf("\nSum of Digits of the number %ld is %ld ",num,sum);
return 0;
}
11. write a recursive function to obtain the first 25 numbers of a
Fibonacci sequence. In a Fibonacci sequences the sum of two successive
terms given the third term. Following are the first few term of the
Fibonacci sequence:
1 1 2 3 5 8 13 21 34 55. . .
#include
#include
long int fib(int m);
void main()
{int i,n;
clrscr();
printf("First 25 Fibonacci sequence are :\n");
for(i=1;i
-
8/10/2019 Chapterd 5 c Solutions
56/57
}
getch();
}
long int fib(int m)
{
if(m
-
8/10/2019 Chapterd 5 c Solutions
57/57
void main()
{
long int n;
clrscr();
printf("\nEnter 5 digits number : ");scanf("%ld",&n);
long int(long int n);
getch();
}
long int(long int n)
{
int rdgt;
long int num,sum = 0;
num = n;
do
{
rdgt = n%10;
n = n/10;
sum = (sum + rdgt);
}while(n !=0);
printf("\nSum of Digits of the number %ld is %ld ",num,sum);
return 0;
}