E.BALAGURUSAMY SOLUTION PROGRAMMING IN ANSI C: CHAPTER-6


 

 

 

 

 

 

6.1 Given a number, write a program using while loop to reverse the digits of the number.

 

 

 

Algorithm:–

 

Step 1:  Read Num.

 

Step 2:  Store Temp=Num & RevNum=0.

 

Step 3:  Repeat Step 4 to Step 5 while Temp! =0 do otherwise go to Step 6

 

Step 4:  Compute Dig=Temp%10 & Temp=Temp/10.

 

Step 5:  Compute RevNum=(RevNum*10)+Temp.

 

Step 6:  Display RevNum.

 

Program:–

 

//Given a number, write a program using while loop to reverse the digits of the number.

 

// Date : 15/03/2010

 

#include<stdio.h>

 

#include<conio.h>

 

void main()

 

{

 

long int Num,Temp,RevNum,Dig;

 

clrscr();

 

printf(“Enter any Number:–\n”);

 

scanf(“%ld”,&Num);

 

Temp=Num;

 

RevNum=0;

 

while(Temp!=0)

 

{

 

Dig=Temp%10;

 

Temp=Temp/10;

 

RevNum=(RevNum*10)+Dig;

 

}

 

printf(“Rverse of Number %ld is %ld\n”,Num,RevNum);

 

getch();

 

}

 

 

 

Output:–

 

 

 

Enter any Number:–

 

12345

 

Rverse of Number 12345 is 54321

 

6.3 Write a program to compute the sum of the digits of a given number.

 

Algorithm:–

 

Step 1:  Read Num.

 

Step 2:  Store Temp=Num & Sum=0.

 

Step 3:  Repeat Step 4 to Step 5 while Temp! =0 do otherwise go to Step 6

 

Step 4:  Compute Dig=Temp%10 & Temp=Temp/10.

 

Step 5:  Compute Sum=Sum+Temp.

 

Step 6:  Display Sum.

 

Program:–

 

//Write a program to compute the sum of the digits of a given number.

 

// Date : 15/03/2010

 

#include<stdio.h>

 

#include<conio.h>

 

void main()

 

{

 

long int Num,Temp,Sum,Dig;

 

clrscr();

 

printf(“Enter any Number:–\n”);

 

scanf(“%ld”,&Num);

 

Temp=Num;

 

Sum=0;

 

while(Temp!=0)

 

{

 

Dig=Temp%10;

 

Temp=Temp/10;

 

Sum=Sum+Dig;

 

}

 

printf(“Sum of Number %ld is %ld\n”,Num,Sum);

 

getch();

 

}

 

Output:–

 

 

 

Enter any Number:–

 

12345

 

Sum of Number 12345 is 15

 

6.4 The numbers in the sequence

 

                           1 1 2 3 5 8 13 21 …………………….

 

are called Fibonacci numbers. Write a program using do ……….. while loop to calculate and print the first m Fibonacci numbers.

 

 

 

 

 

Algorithm:–

 

Step 1:  Read m.

 

Step 2:  Store i=1, Fib1=0 & Fib2=1.

 

Step 3:  Check m>1 then Display Fib2 otherwise go to Step 4

 

Step 4:  Do Step 5 to Step 7

 

Step 5:  Compute Fib=Fib1+Fib2, Fib1=Fib2, Fib2=Fib, i=i+1.

 

Step 6:  Display Fib.

 

Step 7:  Check i<=m-1 if true then go to Step 5 otherwise go to Step 8.

 

Step 8:  End.

 

Program:–

 

//Rewrite the program of the Example using the for statement.

 

// A program to evaluate the equation

 

//                                     y = xn

 

//    when n is a non-negative integer.

 

// Date : 15/03/2010

 

#include<stdio.h>

 

#include<conio.h>

 

void main()

 

{

 

int Count,n;

 

float x,y;

 

clrscr();

 

printf(“Enter The Value of x and n:–\n”);

 

scanf(“%f %n”,&x,&n);

 

y=1.0;

 

for(Count=1;Count<=n;Count++)

 

{

 

y=y*x;

 

}

 

printf(“\nx = %f; n = %d; x to power n = %f\n”,x,n,y);

 

getch();

 

}

 

Output:–

 

Enter The Value of x and n:– 2.5 4

 

nx = 2.500000; n = 4; x to power n = 39.062500

 

6.5 Rewrite the program of the Example using the for statement.

 

 

 

   A program to evaluate the equation

 

                                       y = xn

 

    when n is a non-negative integer.

 

 

 

 Algorithm:–    

 

 

 

Step 1: Read x, n.

 

Step 2: Store 1.0 to y.

 

Step 3: For Count=1 to n repeat Step

 

Step 4: Compute y=y*x & Count=Count+1.

 

Step 5: Display x, n, y.

 

Program:–

 

//Rewrite the program of the Example using the for statement.

 

// A program to evaluate the equation

 

//                                     y = xn

 

//    when n is a non-negative integer.

 

// Date : 15/03/2010

 

#include<stdio.h>

 

#include<conio.h>

 

void main()

 

{

 

int Count,n;

 

float x,y;

 

clrscr();

 

printf(“Enter The Value of x and n:–\n”);

 

scanf(“%f %n”,&x,&n);

 

y=1.0;

 

for(Count=1;Count<=n;Count++)

 

{

 

y=y*x;

 

}

 

printf(“\nx = %f; n = %d; x to power n = %f\n”,x,n,y);

 

getch();

 

}

 

Output:–

 

Enter The Value of x and n:– 2.5 4

 

nx = 2.500000; n = 4; x to power n = 39.062500

 

6.6 Write a program to evaluate the following investment equation

 

 

 

                                       V=P (1+r) n

 

And print the tables which would give the values of various combination of the following values of P, r and n.

 

 

 

               P: 1000, 2000, 3000, ……………….10000

 

               r:  0.10, 0.11, 0,12,…………………..0.20

 

               n: 1,2,3…………….10

 

 

 

 

 

 

 

 

 

 

 

Algorithm:–

 

 

 

Step 1: Read P.

 

Step 2: For r=0.1 to 0.15 repeat Step 3 to Step 4

 

Step 3: For n=1 to 2 repeat Step Step 4

 

Step 4: Compute V=P (1+r) n

 

Step 5: Display r, n, V

 

Program:–

 

 

 

//Write a program to evaluate the following investment equation

 

//                                     V=P(1+r)n

 

//And print the tables which would give the values of various combination of the following values of P, r and n.

 

//             P: 1000, 2000, 3000, ……………….10000

 

//             r:  0.10, 0.11, 0,12,…………………..0.20

 

//             n: 1,2,3…………….10

 

// Date : 15/03/2010

 

#include<stdio.h>

 

#include<conio.h>

 

void main()

 

{

 

int P,n;

 

float V,r,temp;

 

clrscr();

 

printf(“Enter Principal Amount:–\n”);

 

scanf(“%d”,&P);

 

printf(“For P:– %d\n”,P);

 

for(r=0.1;r<=0.15;r+=0.01)

 

{

 

printf(“For Rate %f\n”,r);

 

printf(“n      V”);

 

for(n=1;n<=5;n++)

 

{

 

printf(“%d    “,n);

 

temp=pow((1+r),n);

 

V=P*temp;

 

printf(“%f”,V);

 

}

 

}

 

printf(“\nx = %f; n = %d; x to power n = %f\n”,x,n,y);

 

getch();

 

}

 

 

 

6.7  Write a program to print the following outputs using for loops

 

 

 

a)      1                                           b) *

 

2 2                                             * *

 

   3 3 3                                          * * *

 

   4 4 4 4                                       * * * *

 

      5 5 5 5 5                                    * * * * *

 

 

 

Algorithm:–

 

 

 

a)

 

Step 1: for i=1 to 5 repeat Step 2 to Step 4

 

Step 2: for j=1 to 5 repeat Step 3

 

Step 3: Display i

 

Step 4: go to newline

 

 

 

b)

 

Step 1: for i=1 to 5 repeat Step 2 to Step 4

 

Step 2: for j=1 to 5 repeat Step 3

 

Step 3: Display *

 

Step 4: go to newline

 

Program:-

 

 

 

a)

 

// Write a program to print the following output using for loops :

 

//1

 

//2 2

 

//3 3 3

 

//4 4 4 4

 

//5 5 5 5 5

 

// Date: 15/03/2010

 

#include<conio.h>

 

#include<stdio.h>

 

void main()

 

{

 

int i,j;

 

clrscr();

 

for(i=1;i<=5;i++)

 

{

 

for(j=1;j<=i;j++)

 

{

 

printf(“%d”,i);

 

}

 

printf(“\n”);

 

}

 

getch();

 

}

 

Output:–

 

1                                          

 

2 2                                           

 

   3 3 3                                        

 

   4 4 4 4                                    

 

      5 5 5 5 5

 

 

 

b)

 

//Write a program to print the following output using for loops :

 

//*

 

//* *

 

//* * *

 

//* * * *

 

//* * * * *

 

// Date: 15/03/2010

 

#include<conio.h>

 

#include<stdio.h>

 

void main()

 

{

 

int i,j,k;

 

clrscr();

 

for(i=5;i>=1;i–)

 

{

 

for(k=5;k>i;k–)

 

printf(” “);

 

for(j=1;j<=i;j++)

 

{

 

printf(“*”);

 

}

 

printf(“\n”);

 

}

 

getch();

 

}

 

 

 

 

 

Output:–

 

 

 

*

 

* *

 

* * *

 

* * * *

 

* * * * *

 

6.8  Write a program to read the age of 100 persons and count the number of persons in the age group 50 to 60.

 

 

 

Algorithm:–

 

 

 

 

 

Step 1: for i=1 to 10 repeat Step 2 to Step 4

 

Step 2: Read age

 

Step 3: Check age>=50 && age<=60 then go Step 4 otherwise go Step 1

 

Step 4: Compute c=c+1

 

Step 5: Display c.

 

 

 

Program:–

 

//Write a program to read the age of 10 persons and count the numbers of

 

//persons in teh group 50 to 60 /

 

// Date: 15/03/2010

 

#include<conio.h>

 

#include<stdio.h>

 

void main()

 

{

 

int i,age,c=0;

 

clrscr();

 

for(i=1;i<=10;i++)

 

{

 

printf(“Enter the age of the person%d:”,i);

 

scanf(“%d”,&age);

 

if (age>=50 && age<=60)

 

c=c+1;

 

}

 

printf(“The number of persons in the age group 50 to 60 are : %d”,c);

 

getch();

 

}

 

Output:–

 

 

 

 

 

6.9  Rewrite the program of case study 6.4 using else……if constructs instead of continue

 

Statement.

 

 

 

Program:–

 

//Rewrite the program of case study 6.4 using else……if constructs instead of continue

 

//Statement.

 

// Date: March15,2010

 

#include<conio.h>

 

#include<stdio.h>

 

#include<math.h>

 

void main()

 

{

 

int i;

 

float a,x,y1,y2;

 

a=0.4;

 

printf(”                    Y—–>             \n”);

 

printf(“0—————————————\n”);

 

for(x=0;x<5;x=x+0.25)

 

{

 

y1=(int) (50*exp(-a*x)+0.5);

 

y2=(int) (50*exp(-a*x*x/2)+0.5);

 

if(y1==y2)

 

{

 

if(x==2.5)

 

printf(“X   |”);

 

else

 

printf(“|”);

 

for(i=1;i<=y1-1;++i)

 

printf(” “);

 

printf(“#\n”);

 

}

 

else

 

{

 

if(y1>y2)

 

{

 

if(x==2.5)

 

printf(“X  |”);

 

else

 

printf(”  |”);

 

for(i=1;i<y2-1;i++)

 

printf(” “);

 

printf(“*”);

 

for(i=1;i<=(y1-y2-1);++i)

 

printf(“-“);

 

printf(“0\n”);

 

continue;

 

}

 

else

 

{

 

if(x==2.5)

 

printf(“X   |”);

 

else printf(”   |”);

 

for(i=1;i<=(y1-1);++i)

 

printf(” “);

 

printf(“0”);

 

for(i=1;i<=(y2-y1-1);++i)

 

printf(“-“);

 

printf(“*\n”);

 

}

 

}

 

printf(”   |\n”);

 

}

 

}

 

6.10 Write a program to print a table of values of the function

 

                           y = exp (-x)

 

        for varying from 0.0 to 10.0 in steps of 10.0.  

 

 

 

Algorithm:–

 

 

 

Step 1: Display ‘x’.

 

Step 2: For j=0.1 to 0.5 repeat Step 3

 

Step 3: Display j & go to newline.

 

Step 4: For i=1 to 5 repeat Step 5 to Step 9

 

Step 5: Display i

 

Step 6: For j=0.1 to 0.5 repeat Step 7 to Step 8

 

Step 7: Compute sum=i+j, Ex=exp(sum).

 

Step 8: Display Ex.

 

Step 9: Go to newline.

 

Program:–

 

 

 

//Write a program to print a table of values of the function

 

//                         y = exp (-x)

 

//                  for varying from 0.0 to 10.0 in steps of 10.0.

 

// Date : 15/03/2010

 

#include<stdio.h>

 

#include<conio.h>

 

#include<math.h>

 

void main()

 

{

 

float Ex,sum,i,j;

 

clrscr();

 

printf(“X”);

 

for(j=0.1;j<=0.5;j+=0.1)

 

printf(”    %f”,j);

 

printf(“\n”);

 

for(i=1;i<=5;i++)

 

{

 

printf(“%f”,i);

 

for(j=0.1;j<=0.5;j+=0.1)

 

{

 

sum=i+j;

 

Ex=exp(sum);

 

printf(”    %f”,Ex);

 

}

 

printf(“\n”);

 

}

 

getch();

 

}

 

6.11 Write a program that will read a positive integer and determine and print its binary equivalent.

 

 

 

Algorithm:–

 

 

 

Step 1: Read Num.

 

Step 2: Store Temp=Num, Count=0.

 

Step 3: Repeat Step while Temp!=0

 

Step 4: Compute Dig=Temp%2, Temp=Temp/2, Bin[Count]=Dig, Count=Count+1.

 

Step 5: Display Bin.

 

Program:–

 

 

 

//Write a program that will read a positive integer and determine and print its binary equivalent.

 

// Date : 15/03/2010

 

#include<stdio.h>

 

#include<conio.h>

 

void main()

 

{

 

int Num,Dig,Bin[10],i,Temp,Count;

 

clrscr();

 

printf(“Enter any Number:–\n”);

 

scanf(“%d”,&Num);

 

Temp=Num;

 

Count=0;

 

while(Temp!=0)

 

{

 

Dig=Temp%2;

 

Temp=Temp/2;

 

Bin[Count]=Dig;

 

Count++;

 

}

 

printf(“Binary Number of Integer Number %d is \n”,Num);

 

for(i=(Count-1);i>=0;i–)

 

printf(“%d”,Bin[i]);

 

getch();

 

}

 

Output:–

 

Enter any Number:–

 

5

 

Binary Number of Integer Number 5 is 101

 

6.12 Write a program using for and if statement to display the capital letter S in a grid of 15

 

         rows & 18 columns as shown below.

 

 

 

******************

 

******************

 

******************

 

****

 

****

 

****

 

******************

 

******************

 

******************

 

                            ****

 

                            ****

 

                            ****

 

******************

 

******************

 

******************

 

Algorithm:–

 

 

 

Step 1: Store 1 to i, j & k

 

Step 2: Display S using for & if

 

Program:–

 

// Write a program using for and if statement to display the S.

 

//15/03/2010

 

#include<stdio.h>

 

#include<conio.h>

 

void main()

 

{

 

int i,j,k;

 

clrscr();

 

j=1;

 

//first

 

for(i=1;i<=3;i++)

 

for(j=1;j<=18;j++)

 

{

 

printf(“*”);

 

if(j==18)

 

printf(“\n”);

 

}

 

//second

 

for(i=1;i<=3;i++)

 

for(j=1;j<=4;j++)

 

{

 

printf(“*”);

 

if(j==4)

 

printf(“\n”);

 

}

 

//3rd

 

for(i=1;i<=3;i++)

 

for(j=1;j<=18;j++)

 

{

 

printf(“*”);

 

if(j==18)

 

printf(“\n”);

 

}

 

//4th

 

for(i=1;i<=3;i++)

 

{

 

for(k=1;k<=14;k++)

 

printf(” “);

 

for(j=15;j<=18;j++)

 

{

 

printf(“*”);

 

if(j==18)

 

printf(“\n”);

 

}

 

}

 

for(i=1;i<=3;i++)

 

for(j=1;j<=18;j++)

 

{

 

printf(“*”);

 

if(j==18)

 

printf(“\n”);

 

}

 

getch();

 

}

 

Output:–

 

******************

 

******************

 

******************

 

****

 

****

 

****

 

******************

 

******************

 

******************

 

                            ****

 

                            ****

 

                            ****

 

******************

 

******************

 

******************

 

6.13 Write a program to compute the value of Euler’s number that is used as the base of natural logarithms. Use the following formula.

 

 

 

                           e= 1+ 1/1! +1 /2! + 1/3+……………. 1/n!

 

Algorithm:–

 

 

 

Step 1: Read n.

 

Step 2: Store 1 to e1 & e2.

 

Step 3: For i=1 to n repeat Step 4 to Step 6

 

Step 4: Compute e1=e1+(1/i!)

 

Step 5: Check (e1-e2)<0.00001 then break otherwise go to Step 5

 

Step 6: Compute e2=e1.

 

Step 7: Display e1.

 

 

Program:–

 

 

 

// Write a program to compute the value of Euler’s number that is used as the base of natural logarithms.

 

//Use the following formula.

 

//                         e= 1+ 1/1! +1 /2! + 1/3+……………. 1/n!

 

//15/03/2010

 

#include<stdio.h>

 

#include<conio.h>

 

void main()

 

{

 

float i,n;

 

float e1,e2;

 

clrscr();

 

printf(“Enter No.”);

 

scanf(“%f”,&n);

 

e2=1;

 

e1=1;

 

for(i=1;i<=n;i++)

 

{

 

e1=e1+((float)1/fact(i));

 

if((e1-e2)<0.00001)

 

break;

 

e2=e1;

 

}

 

printf(“The value of e is  :  %f”,e1);

 

getch();

 

}

 

6.14 Write programs to evaluate the following functions to 0.0001% accuracy.

 

               a) sinx = x – x3/3! + x5/5! – x7/7! + ………….

 

               b) cosx= 1 – x2/2! + x4/4! – x6/6! + ………..

 

               c) SUM= 1 + (½)2 + (1/3)3 +(1/4)4 + ………

 

 

 

 

 

6.16 Write a program to print a square of size 5 by using the character S as shown below

 

a) S S S S S                                          b)  S S S S S

 

    S S S S S                                                S           S

 

    S S S S S                                                S          S

 

    S S S S S                                                S          S

 

    S S S S S                                                S S S S S

 

Algorithm:–

 

 

 

a)

 

 

 

Step 1: For i=1 to 5 repeat Step 2 to Step 4

 

Step 2: For j=1 to 5 repeat Step 3

 

Step 3: Dispay S

 

Step 4: go to newline

 

b) 

 

Step 1: For i=1 to 5 repeat Step 2

 

Step 2: Dispay S

 

Step 3: For j=1 to 3 repeat Step 4

 

Step 4: Dispay S   S

 

Step 5: For i=1 to 5 repeat Step 2

 

Step 6: Dispay S

 

Program:–

 

 

 

a) //Write a program to print a square of size 5 by using the character S as shown below

 

//a)     S S S S S

 

//        S S S S S

 

//        S S S S S

 

//        S S S S S

 

//        S S S S S

 

#include<stdio.h>

 

#include<conio.h>

 

#include<math.h>

 

void main()

 

{

 

int j,i;

 

clrscr();

 

for (i=1;i<=5;i++)

 

{

 

for(j=1;j<=5;j++)

 

printf(“S”);

 

printf(“\n”);

 

}

 

getch();

 

}

 

b) //Write a program to print a square of size 5 by using the character S as shown below

 

//S S S S S

 

//S       S

 

//S       S

 

//S       S

 

//S S S S S

 

#include<stdio.h>

 

#include<conio.h>

 

void main()

 

{

 

int j,i,k;

 

clrscr();

 

for (i=1;i<=5;i++)

 

printf(“S”);

 

for(j=2;j<=4;j++)

 

{

 

printf(“\nS   S”);

 

}

 

printf(“\n”);

 

for (i=1;i<=5;i++)

 

printf(“S”);

 

getch();

 

}

 

6.17 Write a program to graph the function

 

                           y = sin(x)

 

         in the interval 0 to 180 degrees in steps of 15 degrees.

 

 

 

Algorithm:–

 

Step 1: For x=0 to 180 repeat Step 2 to Step 3

 

Step 2: Compute y = sin(x)

 

Step 3: Display x, y.

 

program:–

 

//Write a program to graph the function

 

//                         y = sin(x)

 

//       in the interval 0 to 180 degrees in steps of 15 degrees.

 

// Date : 15/03/2010

 

#include<stdio.h>

 

#include<conio.h>

 

#include<math.h>

 

void main()

 

{

 

float y;

 

int x,i;

 

clrscr();

 

printf(“X      Sin(X)\n”);

 

for(i=0;i<=180;i+=15)

 

{

 

y=sin(x);

 

printf(“%d     %f\n”,x,y);

 

}

 

getch();

 

}

 

6.18 Write a program to print all integers that are not divisible by either 2 or 3 and lie

 

        between 1 and 100. Program should also account the number of such integers and print

 

        the result.

 

 

 

Algorithm:–

 

Step 1: For i=1 to 100 repeat Step 2 to Step 4

 

Step 2: Check i%2!=0 && i%3!=0 then go to Step 3 otherwise go to Step 1

 

Step 3: Compute Count=Count+1

 

Step 4: Display i & Count.

 

Program:–

 

//Write a program to print all integers that are not divisible by either 2 or 3 and lie

 

//        between 1 and 100. Program should also account the number of such integers and print

 

//        the result.

 

// Date : 15/03/2010

 

#include<stdio.h>

 

#include<conio.h>

 

#include<math.h>

 

void main()

 

{

 

int i,Count;

 

clrscr();

 

Count=0;

 

for(i=1;i<=100;i++)

 

{

 

if(i%2!=0 && i%3!=0)

 

{

 

Count=Count+1;

 

printf(“%d”,i);

 

}

 

printf(“%d\n”,Count);

 

}

 

getch();

 

}

 

 

Advertisements

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s