Programming exercises
Problem 6.1: Given a integer number write using while loop to reverse the digit of the number. For example, the number
12345
should be written as
54321
Solution:
/*……………..…..reverse number ……………………*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
long int n,b;
printf("\n\n\n Input number:");
scanf("%ld",&n);
while(n>10)
{
b=n%10;
printf("The reversed number is:%ld",b);
n=n/10;
}
printf("%ld",n);
getch();
}
Problem 6.2: The factorial of an integer m is the product of consecutive integers from 1 to m. that is
Factorial m=m!=m*(m-1)*…………………*1.
Write a program that computes and print the result for any given m.
Solution :
/*……………………………factorial…………………………*/
#include<stdio.h>
#include<conio.h>
void main()
{
int sum,i,m;
clrscr();
printf(“Input number:”);
scanf("%d",&m);
sum=1;
for(i=m;i>1;i--)
sum*=i;
printf("The result is: %d",sum);
getch();
}
Problem 6.3: Write a program that compute the sum of the digit of a given integer number.
Solution:
/*………………..sum of given integer number………………….*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
long int n,b,sum=0;
printf("\n\n\nInput number:");
scanf("%ld",&n);
while(n>10)
{
b=n%10;
printf("%ld+",b);
n=n/10;
sum+=b;
}
sum+=n;
printf("the result is= %ld",sum);
getch();
}
Problem 6.4: The numbers in the sequence
1 1 2 3 5 8 13 21……………………..
Are called Fibonacci numbers. Write a program using a do-while loop to calculate and print the Fibonacci numbers.
Solution :
/*……………………Fibonacci sequence…….……………….*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int m,x,y,n,z;
m=0;
x=0;
y=1;
printf("\n\nInput number of count:");
scanf("%d",&n);
printf("%d",y);
do{
z=x+y;
x=y;
y=z;
m++;
printf(" the result= is %d",z);
}while(m<n);
getch();
}
Problem 6.5: The numbers in the sequence
1 1 2 3 5 8 13 21……………………..
Are called Fibonacci numbers. Write a program using a for loop to calculate and print the Fibonacci numbers.
Solution :
/*……………………Fibonacci sequence…….……………….*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,n,z,i;
x=0;
y=1;
printf("\n\nInput number:");
scanf("%d",&n);
printf("%d",y);
for(i=1;i<=n;i++)
{
z=x+y;
x=y;
y=z;
printf(" the result= %d",z);
}
getch();
}
Problem 6.6: Write a program to evaluate the following investment equation
V=P(1+r)
And print the tables which would give the value of V for various combination of the following values of P,r and n.
Solution:
/*…………investment equation………………..*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int i,n,p,j;
double v,r,t;
printf("\n\n\n");
clrscr();
r=0.10;
n=10;
p=1000;
t=pow((1+r),i);
// printf("\n\n%.6lf ",t);
printf("\nP R N V\n");
for(i=1;i<=n;i++)
{
if(r<=0.20 && p<=10000)
{
t=pow((1+r),i);
v=p*t;
p=p+1000;
r=r+0.01;
}
printf("%d %lf %d ",p,r,n);
printf("V=% .6lf ",v);
printf("\n");
}
getch();
}
Problem 6.7(a): Write the program to print the following outputs using for loops.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Solution :
/*………...trivues……………*/
#include<stdio.h>
#include<conio.h>
Void main()
{
int i,j,n;
clrscr();
printf("\n\n Input number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
printf(" ");
}
printf("\n");
}
getch();
}
Problem 6.7(b): Write programs to print the following outputs using for loops.
* * * * *
* * * *
* * *
* *
*
Solution:
/*………………………….star…………………….….*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,c;
clrscr();
printf(“Input number:”);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
c=i;
if(c>j)
{
printf(" ");
c++;
}
else
printf("*");
}
printf("\n");
getch();
}
Problem 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. Use for and continue statements.
Solution:
/****************age count into 50 to 60***************/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int count,i,age;
count=0;printf(“Input age of 100 persons:”);
for(i=1;i<=100;i++)
{
scanf("%d",&age);
if(age>=50&&age<=60)
count+=1;
continue;
}
printf("the countable number is:%d",count);
getch();
}
Problem 6.9: We have two function of the type
Y1=exp(-a*x)
Y2=exp(-a*x*x/2)
Plot the graphs of these function for x verifying from 0 to 5.0.
Without using continue statement.
Solution:
/*……………..plotting two function………………..*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr(); printf("\n\n");
int i;
float a,x,y1,y2;
a=0.4;
printf(" Y.....>\n");
printf("0-----------------------------------------------------\n");
for(x=0;x<5;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");
}
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");
getch();
}
Problem 6.10: Write a program to print a table of values of the function
Y=exp(-x)
For x varying from 0.0 to 10.0 in steps of 0.10.
Solution:
/*……………….y=exp(-x)………………*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
float y;
printf("\n\n....................................\n");
printf("x");
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
y=exp(-i);
printf(" %.2f ",y);
}
printf("\n");
}
getch();
}
Problem 6.11: Write a program that will read a positive integer and determine and print its binary equivalent.
Solution:
/* ………………….to binary…………………..*/
#include<stdio.h>
#include<conio.h>
void main()
{
char binary[20],temp[20];
int i,j,num;
clrscr();
i=0;
printf("\n\nEnter value:");
scanf("%d",&num);
while(num>=1)
{
temp[i++]=(num%2)+48;
num=num/2;
}
printf("\n\nthe binary equvelant is:");
for(j=i-1;j>=0;j--)
printf("%c",temp[j]);
getch();
}
Problem6.12: Write a program using for and if statement to display the capital letter S in a grid of 15 rows and 18 columns as shown below.
******************
******************
******************
****
****
****
******************
******************
******************
****
****
****
*******************
*******************
*******************
Solution :
/*……………………S grid…………………..*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int row,col,i,j,k;
row=15;
col=18;
for(i=1;i<=row;i++)
{
if((i<=3)||(i>=7&&i<=9)||(i>=13&&i<=15))
{
for(j=1;j<col;j++)
printf("*");
printf("\n");
}
else if(i>=4&&i<=6)
{
for(j=1;j<=4;j++)
printf("*");
printf("\n");
}
else
{
for(j=1;j<=13;j++)
printf(" ");
for(j=1;j<=4;j++)
printf("*");
printf("\n");
}
}
getch();
}
Problem 6.13:Write a program to compute the value of Euler’s number e, that is used as the base of natural logarithms. Use the following formula.
e=1+1/1!+1/2!+1/3!+…………………………………+1/n!
use a suitable loop construct. The loop must terminate when the difference between two successive values of e is less than 0.00001.
Solution:
/*……………………….Euler’s number……………………..*/
#include<stdio.h>
#include<conio.h>
float fact(float i)
{
float f=1;
int k;
for(k=1;k<=i;k++)
f*=k;
return(f);
}
void main()
{
float e,a,b,d=1,temp;
int i,j,k;
clrscr();
e=1;i=2;b=1;
while(d>=0.00001)
{
temp=fact(i);
a=1/temp;
e=e+a;
d=b-a;
if(d>=0.00001)
b=a;
i++;
}
printf("\n\nthe result = %f",e);
getch();
}
Problem 6.14: Write programs to evaluate the following functions to 0.0001% accuracy.
(a) sinx =x-x3/3!+x5/5!-x7/7!+………………….
Solution:
/*……………….sinx function………………..*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
double fact(double power)
{
double f=1;
int k;
for(k=1;k<=power;k++)
f=f*k;
return f;
}
void main()
{
int i=1;
double x,term,deno,lob,sin,power=3;
clrscr();
scanf("%lf",&x);
term=x;
sin=x;
while(term>=0.0001)
{
lob=pow(x,power);
deno=fact(power);
term=lob/deno;
power+=2;
if(i%2==1)
sin=sin-term;
else
sin=sin+term;
i++
}
printf("the result= %lf",sin);
getch();
}
Problem 6.14: Write programs to evaluate the following functions to 0.0001% accuracy.
(b) cosx = 1-x2/2!+x4/4!-x6/6!+………………….
Solution:
/*……………….cosx function………………..*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
double fact(double power)
{
double f=1;
int k;
for(k=1;k<=power;k++)
f=f*k;
return(f);
}
void main()
{
int i=1;
double x,term,deno,lob,cos,power=2;
clrscr();
scanf("%lf",&x);
term=1.0;
cos=1.0;
while(term>=0.0001)
{
lob=pow(x,power);
deno=fact(power);
term=lob/deno;
power+=2;
if(i%2==1)
cos=cos-term;
else
cos=cos+term;
i++;
}
printf("the result is= %lf",cos);
getch();
}
Problem 6.14(c): Write programs to evaluate the following functions to 0.0001% accuracy.
Sum=1+(1/2)2+(1/3)3+(1/4)4+…………………………………
Solution:
/*…………………..accuracy…………….*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double term,deno,lob,sum;
clrscr();
term=1.0;
sum=1.0; lob=1.0;deno=2.0;
while(term>=0.0001)
{
term=lob/deno;
term=pow(term,deno);
sum+=term;
deno++; printf("%lf",sum);
}
printf("the sum= %lf",sum);
getch();
}
Problem6.15: The present value (popularly known as book value )of an item is given by the relationship
P=c(1-d)n
Where c=original cost
D=rate of depreciation
N=number of years
P=present value after years.
If p is considered the scrap value at the end of useful life of the item, write a program to compute the useful life in years given the original cost, depreciation rate, and the scrap value.
Solution:
/*…………..useful life in years…………….*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n,c;
double d,p,lob,hor;
printf(“Input original cost, rate of depreciation, present value”);
scanf("%d%lf%lf",&c,&d,&p);
lob=log(p/c);
hor=log(1-d);
n=lob/hor;
printf("year=%d",n); getch();
}
Problem 6.16(a): Write a program to print a square 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 S S S S
S S S S S
Solution :
/*……………….square size………………..*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
printf("\n\n");
for(i=0;i<=4;i++)
{
for(j=1;j<=5;j++)
printf(" S");
printf("\n\n");
}
getch();
}
Problem 6.16(b): Write a program to print a square 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
solution :
/*……………….square size………………..*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
n=5; printf("\n\n");
for(i=1;i<=n;i++)
printf("s ");
printf("\n\n");
for(i=1;i<=n-2;i++)
{
printf("s ");
for(j=1;j<=n-2;j++)
printf(" ");
printf("s\n\n");
}
for(i=1;i<=n;i++)
printf("s ");
printf("\n");
getch();
}
Problem 6.18 : Write the program to print all integers that are not divisible by 2 or 3 and lie between 1 and 100. Program should also account the number of such integers and print the result.
Solution:
/*………..count the number into 1-100 which r divisible 2 or 3………………*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,count,sum;
clrscr();
sum=0;
count=0;
for(i=1;i<=100;i++)
{
if(i%2!=0&&i%3!=0)
{
sum+=i;
count++;
printf("%d\n",i);
}
}
printf("the sum of the value is :%d \nthe countable numbers is: %d",sum,count);
getch();
}
Problem 6.19: 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 O S S
S S S S S
S S S S S
Solution:
/*……………………………..square with centre 0…………………………….*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,mid;
clrscr();
printf("\n\n");
scanf("%d",&n);
mid=(int)(n/2);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==mid&&j==mid)
printf("0 ");
else
printf("s ");
}
printf("\n\n");
}
getch();
}
Problem 6.20: Given a set of 10 two-digit integer containing both positive and negative values, write a program using for loop to compute the sum of all positive values and print the sum and the number of values added. The program should use scanf to read the values and terminate when the sum exceeds 999. Do not use goto statement.
Solution:
/*…………………….sum positive number……………………*/
#include<stdio.h>
#include<conio.h>
void main()
{
nt sum,n,i,j=0;
sum=0;
clrscr();
printf("\n\nInput ten number both positive and negative:");
for(i=0;i<10;i++)
{
scanf("%d",&n);
if(n>0)
{
sum+=n; j++;}
if(sum>999)
break;
}
printf("\n\nThe value of positive numbers is:%d\nand the countable number is: %d",sum,j);
getch();
}
good collection of simple progg tqqq :)
ReplyDeletenice post , i enjoyed your post really and want to read your site again.
ReplyDeleteToronto Escort
Hello,Can you help me? I need the solution manual of ANSI.
ReplyDeleteReally helpful for every student. And I always prefer it when I face problem then I visit this side.
ReplyDeleteThanks for such easy to learn logic.
ReplyDelete