You are on page 1of 26

Let 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) {
j=j/i;

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

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

/* non recursive method */

d=d/b;
printf("%d ",b);
}
else
b++;
}
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);
printf("%d ",b);
}

/* recursive function */

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();
}
______________________________________________________________________
Posted by Chetan Raikwar at 07:02
Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest
4 comments:

1.
AMAR KALDATE19 May 2014 10:58
Write a C function to evaluate the series sin(x) = x - (x3/3!) + ( x5/5!) - (x7/7!)
+ ........
to five significant digits.
#include
float sin(int);
void main()
{
int no;
float sum;
printf("Please Enter The No\t");
scanf("%d", &no);
sum = sin(no);

printf("sin(%d) = %f\n", no, sum);


}
float sin (int x)
{
int i=1;
float y = 0.0;
while(i<=5)
{
y += ((float)raised(x,i)/fact(i)) ; // Type casting concept.
i++;
}
return y;
}
int raised(int x, int i)
{
int a=1, b=1, b1;
while(a<=i*2)
{
b = b*x;
if (a%2!=0)
{
b1= b;
}
a++;
}
return b1;
}
int fact(int i)
{
int j=1 ;
int f= 1, f1;
while(j<=i*2)
{
f *= j;
if (j%2!=0)
{
f1= f;
}
j++;
}
return(f1);
}

You might also like