programing chapter no 5

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

Upload: no-company

Post on 21-Aug-2015

121 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Programing chapter no 5

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:

Page 2: Programing chapter no 5

#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                               i5                               v

Page 3: Programing chapter no 5

10                             x50                             l100                           c500                           d1000                         m

Example:Roman equivalent of 1988 is mdcccclxxxviiiRoman 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;

Page 4: Programing chapter no 5

}

/* 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;

Page 5: Programing chapter no 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");

}

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

Page 6: Programing chapter no 5

(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

Page 7: Programing chapter no 5

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>

Page 8: Programing chapter no 5

#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;            /* 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( ).

Solution:

#include<stdio.h>#include<conio.h>void main() {

int s1,s2,s3,*avg,*prcnt;void func();

Page 9: Programing chapter no 5

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

Page 10: Programing chapter no 5

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;

Page 11: Programing chapter no 5

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);}elseb++;}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) {

Page 12: Programing chapter no 5

if((n%b)==0) {

n=factors(n/b);     /* recursive function */

printf("%d ",b);}

elseb++;

}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

Page 13: Programing chapter no 5

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.

Page 14: Programing chapter no 5

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 seriessin(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:

Page 15: Programing chapter no 5

#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.

Page 16: Programing chapter no 5

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 byrootover ( 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;

Page 17: Programing chapter no 5

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 Euclid’s algorithm, exemplified for J = 1980, K = 1617 as follows:1980 / 1617 = 11980 – 1 * 1617 = 3631617 / 363 = 4

Page 18: Programing chapter no 5

1617 – 4 * 363 = 165363 / 165 = 2363 – 2 * 165 = 335 / 33 = 5165 – 5 * 33 = 0Thus, 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);

Page 19: Programing chapter no 5

}

getch();

}______________________________________________________________________Posted by Chetan Raikwar at 07:02 Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest

4 comments:

1.

AMAR KALDATE 19 May 2014 at 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;

Page 20: Programing chapter no 5

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

ReplyReplies

Page 21: Programing chapter no 5

1.

AMAR KALDATE 20 May 2014 at 06:57

i have considered only addition of sin function i.e. sin(x) = x + (x3/3!) + ( x5/5!) + (x7/7!) + ........ but actual question was sin(x) = x - (x3/3!) + ( x5/5!) - (x7/7!) + ........ so u can just add code alternatively change the sign

Reply

Page 22: Programing chapter no 5

2.

AMAR KALDATE 19 May 2014 at 11:01

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

#include#include

int distance(int, int , int, int, int, int);float area(float, float, float);

void main(){int x1, x2, x3, y1, y2, y3, p1, p2, ap, bp, cp, abc;printf("Please Enter The 3 points co-rdinate \t");scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3);printf("The distance Betwwen points as follows\n");abc = distance(x1, y1, x2, y2, x3, y3);printf("Checking is point lies inside the traingle\n");printf("Please Enter The Co-rdinate of the point\t");

Page 23: Programing chapter no 5

scanf("%d %d", &p1, &p2);

ap = distance(p1, p2, x2, y2, x3, y3);bp = distance(x1, y1, p1, p2, x3, y3);cp = distance(x1, y1, x2, y2, p1, p2);

if ((ap+bp+cp)== abc)printf("1\n");elseprintf("0\n");}

int distance(int x1, int y1, int x2, int y2, int x3, int y3){float a, b, c, area1;a = sqrt(((y2-y1)*(y2-y1))+ ((x2-x1)*(x2-x1)));b = sqrt(((y2-y3)*(y2-y3))+ ((x2-x3)*(x2-x3)));c = sqrt(((y3-y1)*(y3-y1))+ ((x3-x1)*(x3-x1)));printf("a = %f, b = %f , c= %f\n", a, b, c);area1 = area(a, b, c);//printf("Area of Triangle is %f\n", area1);return area1;}

float area(float a, float b, float c){float arg, area2;arg = (a+b+c)/2.0;area2 = sqrt(arg*(arg-a)*(arg-b)*(arg-c)) ;return area2;}