Solutions: Programming Exercises of Programming in ANSI C (E Balagurusamy) 4E

Solutions are not available for too easy questions. All solutions of one exercise are available in a program using switch case keyword. Question taken form Fourth Edition. You can directly copy and past to notepad and save named <filename>.c

Quick Links

HOME PAGE

C Pgogramming Codes

Download Center

Product Review

About us

Contact us

Exercise 1

#include<conio.h>
#include<stdio.h>
#include<math.h>
#define pi 3.14
void main()
{
 int i,j,k,l;
 float a,b,c,x1,x2,y1,y2;
 char exa,chr1,chr2;
 printf("\n\t\tMain Menu\n\t\t---------\n1.5 = 1\t1.6 = 2\t1.8 = 3\t1.11 = 4\n\nSelect a Example: ");
 exa=getche();
 switch(exa)
 {
  case '1':
  printf("\nEnter radius of circle :");
  scanf("%f",&a);
  printf("The Area of circle is : %f",(pi*(a*a)));
  break;

  case '2':
  printf("Enter Table :");
  scanf("%d",&j);
  for (i=1;i<11;i++)
   printf("\n%d X %d = %d",j,i,j*i);
  break;

  case '3':
  printf("\nEnter 3 nos. :");
  scanf("%d%d%d",&i,&j,&k);
  printf("The Valuof x is : %d",i/(j-k));
  break;

  case '4':
  printf("\nEnter two nos. for cordinate of first point :");
  scanf("%f%f",&x1,&y1);
  printf("\nEnter two nos. for cordinate of Second point point :");
  scanf("%f%f",&x2,&y2);
  a=sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
  printf("The distance between (%.0f,%.0f) and (%.0f,%.0f) cordinates is %f",x1,y1,x2,y2,a);
  break;
  default:
  printf("Invalid choice");
 }
 getch();
}

Exercise - 2

/*Exercise-2 By ankit dhir*/
#include<conio.h>
#include<stdio.h>
int main()
{
 /*
 DECLEARITIONS
 */
 int i,j,k,l,no;
 float a,b,c;
 typedef int ankit;
 ankit x,y;
 short z;
 /*
 STATEMENTS
 */
 textcolor(GREEN);
 clrscr();
 textattr(9);
 printf("\t\t\t");
 cprintf("Exercise-2 Solutions By Ankit dhir\n");
main:
 printf("\n\nEnter Q No. : 2.");
 scanf("%d",&no);
 switch(no)
 {
  case 1:
   printf("\nenter value of n :");
   scanf("%d",&j);
   a = 0;
   for(i=1;i<j;i++)
   {
    printf("1/%d + ",i);
    a+=1.0/i;
   }
   printf("1/%d = %.2f",j,(a+1.0/j));
   break;
  case 2:
   printf("\nEnter price in format Rupees.Paise :");
   scanf("%f",&a);
   printf("Net Price is %.0f Paise",(a*100));
   break;
  case 3:
   printf("\nEnter Range of Even no. (ex.= 1 100) :");
   scanf("%d%d",&k,&l);
   if (l<k)
   {
    printf("Invalid Range");
    break;
   }
   else
   {
    printf("\nEven Numbers in Range %d to %d is :\n",k,l);
    for (i=k;i<=l;i++)
    {
     if(i%2==0)
     printf("%d ",i);
    }
   }
   break;
  case 4:
   printf("\nEnter two float nos. :");
   scanf("%f%f",&a,&b);
   printf("\n%f/%f = %f",a,b,(a/b));
   break;
  case 5:
   printf("Enter Price of both items:");
   scanf("%f%f",&a,&b);
   printf("*** LIST OF ITEAMS *** \nIteam\tPrice\nRice\tRs. %0.2f\nSugar\tRs. %0.2f\n\nTotal\tRs. %0.2f",a,b,a+b);
   break;
  case 7:
   q9:
   printf("\nEnter values for x and y :");
   scanf("%d%d",&x,&y);
   z=x+y;
   printf("\nx is %d, y is %d and z is %d",x,y,z);
   break;
  case 8:
   printf("\nEnter two float nos. :");
   scanf("%f%f",&a,&b);
   i=a+b;
   printf("\na = %f b = %f i = %d",a,b,i);
   break;
  case 9: goto q9;

  default:
   printf("\nSolution for this question or question not available ");
   getch();
   return 0;
 }
 getch();
 goto main;
}

Exercise - 3

/*Exercise-3 By ankit dhir*/
#include<conio.h>
#include<stdio.h>
#include<math.h>
#define pi 3.14
int main()
{
 /*
 DECLEARITIONS
 */
 int i,j,k,l,m,no;
 float a,b,c,L,r,f;
 /*
 STATEMENTS
 */
 printf("\t\t\t");
 cprintf("Exercise-3 Solutions By Ankit dhir\n");
main:
 printf("\n\nEnter Q No. : 3.");
 scanf("%d",&no);
 printf("\n");
 switch(no)
 {
  case 1:
   printf("Enter values of i j and k :");
   scanf("%d%d%d",&i,&j,&k);
   l=i;i=j;j=k;k=l;
   printf("i=%d\tj=%d\tk=%d",i,j,k);
   break;
  case 2:
   printf("Enter a float no.:");
   scanf("%f",&a);
   i=(int)a;
   printf("%d",(((i%10000)%1000)%100)%10);
   break;
  case 3:
   printf("Enter a float no.:");
   scanf("%f",&a);
   i=(int)a;
   printf("%d",((i%10000)%1000)%100);
   break;
  case 4:
   printf("Enter length and width of rectangle :");
   scanf("%d%d",&i,&j);
   printf("\nArea of rectangle is :%d\t\t Perimeter is : %d",i*j,(2*(i+j)));
   break;
  case 5:
   printf("Enter a number");
   scanf("%d",&i);
   printf("\nI \t:\t%d",i);
   printf("\nII \t:\t%d",i%10000);
   printf("\nIII \t:\t%d",(i%10000)%1000);
   printf("\nIV \t:\t%d",((i%10000)%1000)%100);
   printf("\nV \t:\t%d",(((i%10000)%1000)%100)%10);
   break;
  case 10:
   printf("Enter values of L and R:");
   scanf("%f%f",&L,&r);
   printf("\nCapacitance\tFrequency");
   for(c=0.01;c<=0.1;c+=0.01)
   {
    f=sqrt((1/(L*c))-((r*r)/(4*(c*c))));
    printf("\n%.2f\t\t%f",c,f);
   }
   break;
  case 13:
   printf("Enter 3 nos.:");
   scanf("%d%d%d",&i,&j,&k);
   l=(i>j)?((i>k)?i:k):((j>k)?j:k);
   m=(i<j)?((i<k)?i:k):((j<k)?j:k);
   printf("The largest no. is %d\tand smallest no. is %d",l,m);
   break;
  case 17:
   printf("x(degrees)\tsin(x)\t\tcos(s)\n");
   for(i=0;i<13;i++)
   {
    a=sin((22.0/(7.0*180.0))*(i*15.0));
    b=cos((22.0/(7.0*180.0))*(i*15.0));
    printf("%d\t\t%.3f\t\t%.3f\n",i*15,a,b);
   }
   break;
  case 18:
   printf("Number\tsqrt\tsqr\n");
   for(i=0;i<11;i++)
   {
    printf("%d\t%.2f\t%d\n",i*10,sqrt(i*10.0),i*i*100);
   }
   break;

  default:
   printf("\nSolution for this question is not available (may be easy) or question not valid");
   getch();
   return 0;
 }
 getch();
 goto main;
}

Exercise - 4

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

main()
{
 int i,j,k,qno,x,y;
 char word[]="WORDPROCESSING";


 textcolor(9);
 clrscr();
 textattr(10);
 printf("\t\t\t");
 cprintf("Ankit Dhir Solutions");
 master:
 printf("\nSelect Q. No. : 4.");
 if(scanf("%d",&qno)==0)
 {

 }
 printf("\n");
 switch(qno)
 {
 case 1:
  printf("%.4s ",word);
  for(i=4;i<14;i++)
  {
   printf("%c",word[i]);
  }
  printf("\n%.4s\n",word);
  for(i=4;i<14;i++)
  {
   printf("%c",word[i]);
  }
  printf("\n%c.%c.",word[0],word[4]);
  break;
 case 2:
  printf("Enter values of x and y:");
  if(scanf("%d%d",&x,&y)!=2)
  goto error;
  if (x==y)
  goto error;
  printf("\n x+y     x+y");
  printf("\n (1)  --- = %-9.2f(2) --- =%d      (3)(x+y)(x-y)=%d",(float)(x+y)/(float)(x-y),(x+y)/2,(x+y)*(x-y));
  printf("\n x-y      2");
  break;
 case 5:
  printf("Enter two nos.(2 disits):");
  scanf("%d%d",&x,&y);
  printf("Process of Multiplication:\n");
  printf("\t\t%d\n\tX\t%d\n\t------------\n\t%10d\n\t%9dX\n\t------------\n\t%10d\n\t------------\n",x,y,x*(y%10),x*(y/10),x*y);
  break;

 default:
  printf("\nQuestion does not available \nPress any key to continue...");
  getch();
  return 0;


 }
 getch();
 goto master;

 error:
  printf("Invalid value entered!!\n Error in process\npress any key to continue...");
  getch();
  return 0;

}

Exercise - 5

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

main()
{
 int i,j,k,qno,x,y;
 char word[]="WORDPROCESSING";


 textcolor(9);
 clrscr();
 textattr(10);
 printf("\t\t\t");
 cprintf("Ankit Dhir Solutions");
 master:
 printf("\nSelect Q. No. : 4.");
 if(scanf("%d",&qno)==0)
 {

 }
 printf("\n");
 switch(qno)
 {
 case 1:
  printf("%.4s ",word);
  for(i=4;i<14;i++)
  {
   printf("%c",word[i]);
  }
  printf("\n%.4s\n",word);
  for(i=4;i<14;i++)
  {
   printf("%c",word[i]);
  }
  printf("\n%c.%c.",word[0],word[4]);
  break;
 case 2:
  printf("Enter values of x and y:");
  if(scanf("%d%d",&x,&y)!=2)
  goto error;
  if (x==y)
  goto error;
  printf("\n x+y     x+y");
  printf("\n (1)  --- = %-9.2f(2) --- =%d      (3)(x+y)(x-y)=%d",(float)(x+y)/(float)(x-y),(x+y)/2,(x+y)*(x-y));
  printf("\n x-y      2");
  break;
 case 5:
  printf("Enter two nos.(2 disits):");
  scanf("%d%d",&x,&y);
  printf("Process of Multiplication:\n");
  printf("\t\t%d\n\tX\t%d\n\t------------\n\t%10d\n\t%9dX\n\t------------\n\t%10d\n\t------------\n",x,y,x*(y%10),x*(y/10),x*y);
  break;

 default:
  printf("\nQuestion does not available \nPress any key to continue...");
  getch();
  return 0;


 }
 getch();
 goto master;

 error:
  printf("Invalid value entered!!\n Error in process\npress any key to continue...");
  getch();
  return 0;

}

Exercise - 6

/*Exercise-6 By ankit dhir*/
#include<conio.h>
#include<stdio.h>
#include<math.h>
int fact(int);
int main()
{
 /*
 DECLEARITIONS
 */
 int i,j,k,l,no,dig[4],age[10];
 int num;
 float a,b,c;

 /*
 STATEMENTS
 */
 textcolor(GREEN);
 clrscr();
 textattr(9);
 printf("\t\t\t");
 cprintf("Exercise-6 Solutions By Ankit dhir\n");
main:
 printf("\n\nEnter Q No. : 2.");
 scanf("%d",&no);
 switch(no)
 {
  case 1:
   printf("HITECH CODES IN constracution");
   /*
   printf("\nEnter a number :");
   scanf("%d",&num);
   for (i=4;i>0;i--)
   {
   j=pow(10,(i-1));
   dig[i-1]=num%(j);
   }
   printf("Number is now :");
   for (i=0;i<4;i++)
   {
   printf("%d",dig[i]);
   }
   */
   break;
  case 6:
   /* v=(1+r)^n */
   clrscr();
   printf("S. No.\tP\tr\tn\tV\n");
   for (i=1;i<11;i++)
   {
    a=(i*1000);
    for(j=i;j>0;j--)
    {
    a=a*((((float)(i+9))/100)+1);
    }
    printf("%d\t%d\t%.2f\t%d\t%.2f\n",i,(i*1000),(((float)(i+9))/100),i,a);
   }
   break;
  case 7:
   for(i=1;i<6;i++)
   {
    for(j=i;j>0;j--)
    {
     printf("%d",i);
    }
    printf("\n");
   }
   printf("\n");
   for(i=5;i>0;i--)
   {
    for(j=(5-i);j>0;j--)
    {
     printf(" ");
    }
    for(j=i;j>0;j--)
    {
     printf("*");
    }
    printf("\n");
   }
   break;
  case 8:
   printf("Enter Age of 10 persons:");
   for(i=0;i<10;i++)
   {
    scanf("%d",&age[i]);
   }
   printf("Age between 50 to 60:\n");
   for (i=0;i<10;i++)
   {
    if(age[i]>50 && age[i]<60)
    printf("%d, ",age[i]);
   }
   break;
  case 12:
   for(i=0;i<15;i++)
   {
    printf("\n");
    if(i<3 || (i>5 && i<9) || (i>11 && i<15))
    {
     for(j=0;j<18;j++)
     {
      printf("*");
     }
    }
    if(i>2 && i<6)
    {
     for (j=0;j<4;j++)
     {
      printf("*");
     }
    }
    if(i>8 && i<12)
    {
     for(j=0;j<14;j++)
     printf(" ");
     printf("****");
    }

   }
   break;
  case 13:
   printf("Enter value of n:");
   scanf("%d",&no);
   a=1;
   for(i=1;i<no;i++)
   {
    a=a+(1/(float)fact(i));
   }
   printf("%f",a);
   break;
  case 21:
   printf("Enter value of n:");
   scanf("%d",&no);
   a=fact(no);
   printf("%f",a);
   break;
  default:
   printf("\nSolution for this question or question not available ");
   getch();
   return 0;
 }
 getch();
 goto main;
}
int fact (int n)
{
 int f=1;
 if(n!=1)
 f = n*fact(n-1);
 return(f);
}