Let Us C / Chapter 6 (Data Types Revisited)

                               Exercise [D]


Following program calculates the sum of digits of the number 12345. Go through it and find out why is it necessary to declare the storage class of the variable sum as static.
main( )
{
int a ;
a = sumdig ( 12345 ) ;
printf ( "\n%d", a ) ;
}
sumdig ( int num )
{
static int sum ;
int a, b ;
a = num % 10 ;
b = ( num - a ) / 10 ;
sum = sum + a ;
if ( b != 0 )
sumdig ( b ) ;
else
return ( sum ) ;
}

Solution:


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

void main() {

int a;

clrscr();

a=sumdig ( 12345 ) ;

printf("\n %d ",a) ;

getch();

}
sumdig(int num)
{

static int sum;

/*

It is necessary to declare the storage class of sum as static because it is within
a recursive funtion. It is necessary to keep the value of 'num' same for all relevent
recursions of it. And to obtain running sum of all of them.

*/

int a,b;

a=num%10;
b=(num-a)/10;
sum=sum+a;

if(b!=0)
sumdig(b);

else
return(sum);

}
______________________________________________________________________

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                               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<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;            /* 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();
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);     /* 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 Euclid’s 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();

}
______________________________________________________________________

Let Us C / Chapter 4 (The Case Control Structure)

                               Exercise [C]


Write a menu driven program which has following options:

1. Factorial of a number.
2. Prime or not
3. Odd or even
4. Exit                          

Solution:


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

int num,i,j=0,k=0,choice,fact=1;
clrscr();

printf("Please enter any number: ");
scanf("%d",&num);

while(1)
{
printf("\n\n1. Factorial");
printf("\n2. Prime");
printf("\n3. Odd/Even");
printf("\n4. Exit");
printf("\n\nPlease enter your choice:  ");
scanf("%d",&choice);

switch(choice)
{

case 1:

     for(i=num;i>=1;i--) {
     fact=fact*i;
     }
     printf("\nFactorial = %d ",fact);
     break;

case 2:

     for(i=2;i<num;i++) {
     j=num%i;
     if(j==0){
     k=1;
     break;
     }
      }
     if(k==0) {
     printf("\nPrime Number");
     }
     else {
     printf("\nNot a Prime Number");
     }
     break;

case 3:

     if((num%2)==0)
     printf("\nEven Number");
     else
     printf("\nOdd Number");
     break;

case 4:

     exit();

     }
  }

  }

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

                               Exercise [D]


Write a program which to find the grace marks for a student using switch. The user should enter the class obtained by the student and the number of subjects he has failed in.

− If the student gets first class and the number of subjects he failed in is greater than 3, then he does not get any grace. If the number of subjects he failed in is less than or equal to 3 then the grace is of 5 marks per subject.

− If the student gets second class and the number of subjects he failed in is greater than 2, then he does not get any grace. If the number of subjects he failed in is less than or equal to 2 then the grace is of 4 marks per subject.

− If the student gets third class and the number of subjects he failed in is greater than 1, then he does not get any grace. If the number of subjects he failed in is equal to 1 then the grace is of 5 marks per subject

Solution:


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

int _class,f_sub;
clrscr();

printf("\nPlease enter the class obtained\n1=first 2=second 3=third:  ");
scanf("%d",&_class);
printf("\n\nPlease enter the number of failed subjects:  ");
scanf("%d",&f_sub);

switch(_class) {

case 1:
    if(f_sub<=3) {
    printf("\nGrace marks = 5 marks per subject.\n");
    }
    else {
    printf("\nNo Grace marks.\n");
    }
    break;

case 2:
    if(f_sub<=2) {
    printf("\nGrace marks = 4 marks per subject.\n");
    }
    else {
    printf("\nNo Grace marks.\n");
    }
    break;

case 3:
    if(f_sub==1) {
    printf("\nGrace marks = 5 marks per subject.\n");
    }
    else {
    printf("\nNo Grace marks.\n");
    }
    break;

default:
    printf("Error! wrong input.\n");
    break;
    }

    getch();
    return 0;

    }
_____________________________________________________________________

Let Us C / Chapter 7 (The C Pre-processor)

                               Exercise [C] (a) Write down macro definitions for the following: 1. To test whether a character ente...