Вы находитесь на странице: 1из 57

Let Us C(Chapter 5)

[D] a.Write a function to calculate the factorial value of any integer entered through the
keyboard.
Ans:#include<stdio.h>
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<=num;i++)
j=j*i;
return j;
}

[D] b.Write a function power ( a, b ), to calculate the value of a raised to b.


Ans:#include<stdio.h>
long power(int,int);
int main(int argc, char *argv[])
{
int num1,num2;
long i;
printf("Enter a numbers: ");
scanf("%d %d",&num1,&num2);
i=power(num1,num2);
printf("Power of %d to %d is: %ld\n",num1,num2,i);
return 0;

}
long power(int a,int b)
{
int i;
long j=1;
for(i=1;i<=b;i++)
j=j*a;
return j;
}

[D] c.Write a general-purpose function to convert any given year into its roman equivalent.
The following table shows the roman equivalents of decimal numbers:

Ans:#include <stdio.h>
void roman(int);
int main(int argc, char *argv[])
{ int yr;
printf("Enter yr");
scanf("%d",&yr);
roman(yr);
return 0;
}
void roman(int a)
{ int i;
if(a<5)
{
for(i=1;i<=a;i++)
printf("%c",'I');
}
if(a<10&&a>=5)
{
printf("%c",'V');

for(i=1;i<=a%5;i++)
printf("%c",'I');
}
if(a<50&&a>=10)
{ for(i=1;i<=a/10;i++)
printf("%c",'X');
a=a-10*(i-1);
for(i=1;i<=a/5;i++)
printf("%c",'V');
a=a-5*(i-1);
for(i=1;i<=a;i++)
printf("%c",'I');
}
if(a<100&&a>=50)
{ for(i=1;i<=a/50;i++)
printf("%c",'L');
a=a-50*(i-1);
for(i=1;i<=a/10;i++)
printf("%c",'X');
a=a-10*(i-1);
for(i=1;i<=a/5;i++)
printf("%c",'V');
a=a-5*(i-1);
for(i=1;i<=a;i++)
printf("%c",'I');
}
if(a<500&&a>=100)
{
for(i=1;i<=a/100;i++)
printf("%c",'C');
a=a-100*(i-1);
for(i=1;i<=a/50;i++)
printf("%c",'L');
a=a-50*(i-1);
for(i=1;i<=a/10;i++)
printf("%c",'X');

a=a-10*(i-1);
for(i=1;i<=a/5;i++)
printf("%c",'V');
a=a-5*(i-1);
for(i=1;i<=a;i++)
printf("%c",'I');
}
if(a<1000&&a>=500)
{
for(i=1;i<=a/500;i++)
printf("%c",'D');
a=a-500*(i-1);
for(i=1;i<=a/100;i++)
printf("%c",'C');
a=a-100*(i-1);
for(i=1;i<=a/50;i++)
printf("%c",'L');
a=a-50*(i-1);
for(i=1;i<=a/10;i++)
printf("%c",'X');
a=a-10*(i-1);
for(i=1;i<=a/5;i++)
printf("%c",'V');
a=a-5*(i-1);
for(i=1;i<=a;i++)
printf("%c",'I');
a=a-500*(i-1);
}
if(a>=1000)
{ for(i=1;i<=a/1000;i++)
printf("%c",'M');
a=a-1000*(i-1);
for(i=1;i<=a/500;i++)
printf("%c",'D');
a=a-500*(i-1);

for(i=1;i<=a/100;i++)
printf("%c",'C');
a=a-100*(i-1);
for(i=1;i<=a/50;i++)
printf("%c",'L');
a=a-50*(i-1);
for(i=1;i<=a/10;i++)
printf("%c",'X');
a=a-10*(i-1);
for(i=1;i<=a/5;i++)
printf("%c",'V');
a=a-5*(i-1);
for(i=1;i<=a;i++)
printf("%c",'I');
a=a-500*(i-1);
}
}

[D] d.Any year is entered through the keyboard. Write a function to determine whether the
year is a leap year or not.
Ans:/*Rule 1: A year is called leap year if it is divisible by 400.
For example: 1600, 2000 etc leap year while 1500, 1700 are not leap year.
Rule 2: If year is not divisible by 400 as well as 100 but it is divisible by 4 then that year are
also leap year.
For example: 2004, 2008, 1012 are leap year.
*/
#include <stdio.h>
void leap(int);
int main(int argc, char *argv[])
{ int yr;
printf("Enter the yr");
scanf("%d",&yr);
leap(yr);
return 0;
}
void leap(int yr)

{
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 prime
factors of this number.
Ans:#include <stdio.h>
#include<conio.h>
void 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<=num;i++)
{ //Finding The Prime Number
for(j=2;j<=i;j++)
{
if(i%j==0 && i==j)
{
//Checking if The number is factor of The Given Number
if(num%i==0)
{
num=num/i;
printf("%d ",i);
i=1;
break;
}
}
}
}
}

[F] b.Write a function that receives 5 integers and returns the sum, average and standard
deviation of these numbers. Call this function from main( ) and print the results in main( ).
Ans:#include <stdio.h>
#include<math.h>
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<5;i++)
scanf("%d",&num[i]); /*Array is entered through loop*/
std_avg(&std,&avg,&sum,&num); /*Address of array is passed*/
printf("Std %f",std);
printf("\nAvg %f",avg);
printf("\nSum %f",sum);
return 0;
}
void std_avg(float *s,float*a,float*su,int*p)
{
int i;
*su=0;
*s=0;
for(i=0;i<5;i++)
*su=*su+*(p+i);
*a=*su/5;
for(i=0;i<5;i++)
*s=*s+(*(p+i)-*a)*(*(p+i)-*a);
*s=*s/5;
*s=sqrt(*s);
}

[J] a.(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
Ans:#include <stdio.h>
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 Recursion
int 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 prime
factors of the number. Modify the function suitably to obtain the prime factors recursively.
Ans:#include<stdio.h>
void 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);
}

/*Prime Factor Without Recursion*/


void prime(int num)
{
int i,j;
for(i=1;i<=num;i++)
{ //Finding The Prime Number
for(j=2;j<=i;j++)
{
if(i%j==0 && i==j)
{
//Checking if The number is factor of The Given Number
if(num%i==0)
{
num=num/i;
printf("%d ",i);
i=1;
break;
}
}
}
}
}

/*Prime Factor Using Recursion*/


void prime_rec(int num)
{
int i,j;
if(num<=0)
return;
for(i=1;i<=num;i++)
{ //Finding The Prime Number
for(j=2;j<=i;j++)
{
if(i%j==0 && i==j)
{
//Checking if The number is factor of The Given Number
if(num%i==0)
{
printf("%d ",i);
prime_rec(num/i);
i=num+1;
break;

}
}
}
}
}

[J] 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 Fibonacci sequence:
1 1 2 3 5 8 13 21 34 55 89...
Ans:#include <stdio.h>
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 binary
equivalent of this number using recursion.
Ans:#include <stdio.h>
void binary(int);
int main(int argc, char *argv[])

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 <stdio.h>
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 Nth
digit 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;

[J] f.(f) Write a C function to evaluate the series

to five significant digits.


Ans:#include <stdio.h>
#include<math.h>
#include<conio.h>
unsigned 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 Series


float sin1(float y)
{ int i,j;
unsigned int k;
float sum;
j=0;
sum=0;
for(i=1;i<15;i=i+2)
{ k=findFactorial(i);
printf("\n%u ",k);
sum=sum + (pow(y,i)/k)*pow(-1.0,j);
j=j+1;
}
return sum;
}

//Function For FInding Factorial Of A Given Number


unsigned int findFactorial(int num)

{
int i,f=1;
for(i=1;i<=num;i++)
f=f*i;
return f;
}

[J] h.Write a function to find the binary equivalent of a given decimal integer and display it.
Ans:#include<stdio.h>
int main(int argc, char *argv[])
{
long int decimalNumber,remainder,quotient;
int binaryNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0)
{
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
}
printf("Equivalent binary value of decimal number %d: \n",decimalNumber);
for(j = i -1 ;j> 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:

Thus, the greatest common divisor is 33.


Ans:#include <stdio.h>
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 World
Happy Coding
*/

[D] Answer the following:


(a)Write a function to calculate the factorial value of any integer
entered through
the 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<=num; temp++) { fact=fact*temp; continue; } return (fact); }

(b) Write a function power ( a, b ), to calculate the value of a raised


to b
hide

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 <=b; calc++) { calculation=calculation*a; continue; } return(calculation); }

(c) Write a general-purpose function to convert any given 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:
Romanequivalent of 1988 is mdcccclxxxviii
Roman equivalent of 1525 is mdxxv

hide

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)

{
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:

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;

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");

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");

return 0;
}

(d) Any year is entered through the keyboard. Write a function to


determine whether the year is a leap year or not.
hide

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);

(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.
hide

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<=num-1; i++)


{
if (num%i==0)
{
ifprime=0;
}
else
ifprime=1;
}
return
(ifprime);
}
//The following function prints the prime factors of a number.
primefactor(int num)

{
int factor,ifprime;
for (factor=2; factor<=num;
)
{
prime(factor);
//so that the factors are only prime and nothing else.
if (ifprime)
{
if(num%factor==0) //diving by all the prime numbers less than the number itself.
{
printf("%d ", factor);
num=num/factor;
continue;
}
else
{
factor++;//this cannot be made a part of the for loop
}
}
}
return 0;
}

et Us C / Chapter 5 (Functions & Pointers)


Exercise [D]

(a) Write a function to calculate the factorial value of


any integer entered through the keyboard.
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
int num;
void func();
clrscr();
printf("Please enter any number: ");
scanf("%d",&num);

func(num);

getch();
}
void func(int n) {
int fact=1;

for(;n>=1;n--) {
fact=fact*n;
}
printf("\n\nFactorial value = %d \n",fact);
}
------------------------------------------------------------------------------------------------------------

(b) Write a function power ( a, b ), to calculate the value


of a raised to b.
Solution:
#include<stdio.h>
#include<conio.h>
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<=b;i++) {
c=c*a;
if(i==b) {
break;
}
}
printf("\n\n\nValue of %d raised to the power of %d is = %d\n",a,b,c);
return 0;
}
------------------------------------------------------------------------------------------------------------

(c) Write a general-purpose function to convert any given

year into its roman equivalent. The following table shows


the roman equivalents of decimal numbers:
Decimal

Roman

1
5
10
50
100
500
1000

i
v
x
l
c
d
m

Example:
Roman equivalent of 1988 is mdcccclxxxviii
Roman equivalent of 1525 is mdxxv
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
int yr;
void func();
clrscr();
printf("Please enter the year: ");
scanf("%d",&yr);
printf("\n\nRoman Equivalent = ");
func(yr);
getch();
}

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;

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

function to determine whether the year is a leap year or


not.
Solution:
#include<stdio.h>
#include<conio.h>
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<stdio.h>

#include<conio.h>
void main() {
int i,j,k;
clrscr();
printf("enter the number: ");
scanf("%d",&j);

printf("\n\nprime factors:");

for(i=2;i<=j;) {
if(j%i==0) {

/* divide only if remainder is supposed to be 0 */

j=j/i;
printf(" %d ",i); /* print the divisor */
}
else

/* if divisor cannot divide completely */

i=i+1;

/* increase it's value and try again */

getch();
}
________________________________________________________________________
Exercise [F]

(a) 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( ).
Solution:
#include<stdio.h>

#include<conio.h>
void main() {
int i;
float j,k,product();
clrscr();

printf("Please enter an integer number: ");


scanf("%d",&i);
printf("\nPlease enter a decimal number: ");
scanf("%f",&j);
k=product(&i,&j);
printf("\nProduct = %.1f\n",k);

getch();

}
float product(int *a,float *b) {
float *c;
*c=(*a)*(*b);
return *c;
}

------------------------------------------------------------------------------------------------------------

(b) Write a function that receives 5 integers and returns


the sum, average and standard deviation of these
numbers. Call this function from main( ) and print the
results in main( ).
Solution:
#include<stdio.h>
#include<conio.h>

#include<math.h>
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;
*av=(a+b+c+d+e)/5;

/* sum of digits */
/* 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( ).

Solution:
#include<stdio.h>
#include<conio.h>
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<stdio.h>
#include<conio.h>
void main() {
long num,s=0,ch;
clrscr();

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);

return n;
}
}
-----------------------------------------------------------------------------------------------------------

(b) A positive integer is entered through the keyboard,


write a program to obtain the prime factors of the
number. Modify the function suitably to obtain the prime
factors recursively.
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
int d,ch,b=2;
clrscr();
printf("Enter the number: ");
scanf("%d",&d);
printf("\n\nObtain prime factors:\n\n");
printf("1: non-recursively\n\n");
printf("2: recursively\n\n");
printf("your choice: ");
scanf("%d",&ch);

switch(ch) {
case 1:
printf("\n\n\nPrime factors: ");
while(d!=1) {
if(d%b==0) {
d=d/b;
printf("%d ",b);
}
else
b++;
}

/* non recursive method */

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;
}
}
------------------------------------------------------------------------------------------------------------

(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 Fibonacci
sequence:
1 1 2 3 5 8 13 21 34 55 89...
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
unsigned i,num=25,c=1;
clrscr();
for(i=0;i<num;i++) {
printf("%u ",fib(c));
c++;
}
getch();
}
fib(unsigned n) {
if(n==0)
return 0;
if(n==1)
return 1;
else
return fib(n-1)+fib(n-2);
}
------------------------------------------------------------------------------------------------------------

(d) A positive integer is entered through the keyboard,

write a function to find the binary equivalent of this


number using recursion.
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
int num;
clrscr();
printf("Enter the number: ");
scanf("%d",&num);
printf("\n\n\nBinary equivalent: ");
binary(num);
gotoxy(20,7);
printf("<%c%c%c%c%c read right to left",196,196,196,196,196);
getch();
}
binary(int n) {
if(n==0)
return 0;
else {
printf("%d",n%2);
n= binary( n/2 ); /* recursive function */
return n;
}
}
------------------------------------------------------------------------------------------------------------

(e) Write a recursive function to obtain the running sum


of first 25 natural numbers.
Solution:

#include<stdio.h>
#include<conio.h>
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

after circular shift y = 5, z = 8 and x = 10. Call the


function with variables a, b, c to circularly shift values.
Solution:
#include<stdio.h>
#include<conio.h>
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;

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<stdio.h>
#include<conio.h>
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("<%c%c%c%c%c",196,196,196,196,196);
printf(" read right to left");
getch();
}
void binary(int n) {
while(n!=0) {
printf("%d",n%2);
n=n/2;
}

}
------------------------------------------------------------------------------------------------------------

(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<stdio.h>
#include<conio.h>
#include<math.h>
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);
}
------------------------------------------------------------------------------------------------------------

(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 are
A(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 common


divisor 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<stdio.h>
#include<conio.h>
void main() {
int a,b,r,d1,d2,temp;
clrscr();
printf("Enter first number: ");
scanf("%d",&a);

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();
}

Function C Programing
1.Write a function to calculate the factorial value of any integer entered
through the keyboard.
#include<stdio.h>
#include<conio.h>
long fact(int n)
{
int i;
long f=1;
for (i=1;i<=n;i++)
f*=i;
return (f);

}
void main()
{
int n,r;
clrscr();
aa:
printf("Please enter a value for n & r(r<=n)");
scanf("%d %d",&n,&r);
if(r>n)
{
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<stdio.h>
#include<conio.h>
#include<math.h>
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 : ");

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<stdio.h>
#include<conio.h>
int 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;
}

4.Write a function to calculate GCD of two numbers.


#include<stdio.h>
#include<conio.h>
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<stdio.h>
#include<conio.h>
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);

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<stdio.h>
#include<conio.h>
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<stdio.h>
#include<conio.h>
float prd(float x,int y);
void main()
{
float a,c;

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 from
main() and print the results in main().
#include<stdio.h>
#include<conio.h>
#include<math.h>
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)
{

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)
(ii)

Using recursion
Without using recursion

Using recursion
#include<stdio.h>
#include<conio.h>
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

#include<stdio.h>
#include<conio.h>
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<stdio.h>
#include<conio.h>
long int fib(int m);
void main()
{
int i,n;
clrscr();
printf("First 25 Fibonacci sequence are :\n");
for(i=1;i<=25;i++)
{
printf("%ld\n",fib(i));

}
getch();
}
long int fib(int m)
{
if(m<=2)
return 1;
else
return fib(m-1)+fib(m-2);
}
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)
(ii)

Using recursion
Without using recursion

Using recursion
#include<stdio.h>
#include<conio.h>
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


#include<stdio.h>
#include<conio.h>

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;
}