C Chapter 04
C Chapter 04
=NO ERROR.
=NO ERROR.
COUNT=1275,
PRICE=235.74,
CITY=CAMBRIDGE.
OUTPUT=1275 235.75.
OUTPUT=36576 790980
[C] :printf(“%c”,city);
OUTPUT=CAMBRIDGE
[A] :printf(“%d.7.2%f”,year,amount);
[B] :printf(“%-s,%c”\n,city,code);
=NO WRONG.
scanf(“%4d %*d”,&year,&code,&count);
IF DATA KYED IN 19883745
0UTPUT=1988.
OUTPUT.
(a)WORD PROCESSING
(b)WORD
PROCESSING
(c) W.P.
Solution:
#include
#include
void main()
char s[10],d[11];
clrscr();
scanf("%4s%10s",s,d);
printf("(a)%s %s\n",s,d);
printf("(b)%s\n%s\n",s,d);
printf("(c)%.1s.%.1s",s,d);
getch();
Output:
(a) WORDPROCESSING
(b) WORD
PROCESSING
(c) W.P.
Problem no. 4.2: Write a program to read the values of x and y and print the
results of the following expression in one line:
Solution:
#include
#include
void main()
{
float x,y,a,b,c;
clrscr();
scanf("%f%f",&x,&y);
if(x-y==0)
printf("(a)=imagine");
else
a=(x+y)/(x-y);
printf("(a)=%.2f",a);
b=(x+y)/2;
c=(x+y)*(x-y);
getch();
Output:
(a)=7.00
(b)=3.50
(c)=12.00
(a)= imagine
(b)=7.00
(c)=0.00
Problem no. 4.3: Write a program to read the following numbers, round them
off to the nearest integers and print out
Solution:
#include
#include
void main()
int p,i;
float a;
clrscr();
for(i=1;i<=4;i++)
scanf("%f",&a);
if(a>=0)
p=a+0.5;
else
p=a-0.5;
getch();
Output:
Problem no. 4.4:write a program that read 4 floating values in the range, 0.0
to20.0, and prints a horizontal bar chart to represent these values using the
character * as the fill character. For the purpose of the chart, the values may
be rounded off to the nearest integer . For the example , the value 4.36
should be represented as follos,
****
* * * * 4.36
****
Solution:
#include
#include
void main()
float a1,a2,a3,a4;
int x,y,z,t,i;
clrscr();
scanf("%f%f%f%f",&a1,&a2,&a3,&a4);
x=a1+0.5;y=a2+0.5;z=a3+0.5;t=a4+0.5;
for(i=0;i<x;i++)< span=""></x;i++)<>
printf("* ");
printf("%.2f\n",a1);
for(i=0;i<y;i++)< span=""></y;i++)<>
printf("* ");
printf("%.2f\n",a2);
for(i=0;i<z;i++)< span=""></z;i++)<>
printf("* ");
printf("%.2f\n",a3);
for(i=0;i<t;i++)< span=""></t;i++)<>
printf("* ");
printf("%.2f\n",a4);
getch();
Output:
* * * * * 4.85
* * * * 4.36
* * * 3.12
* * * * * 5.47
45
X 37
7x45 is 315
3x45is 135
#include
#include
void main()
int a,b,c,p;
clrscr();
scanf("%d%d",&a,&b);
printf(" \t%4d\n\tx%3d\n",a,b);
printf("\t------\n");
p=b/10;
c=b%10;
printf("%dx%dis%6d\n",c,a,c*a);
printf("%dx%dis%5d\n",p,a,p*a);
printf("\t-------\n");
printf("\t-------");
getch();
Output:
45
X 37
7x45 is 315
3x45is 135
Solution:
#include
#include
void main()
int x,y,z;
clrscr();
scanf("%d%d%d",&x,&y,&z);
printf("(a) X=%d,",x);
printf("Y=%d,",y);
printf("Z=%d\n",z);
printf("(b) X=%3d, Y=%2d, Z=%2d\n",x,y,z);
%d",x,y,z);
getch();
Output:
Solution:
#include
#include
int main(void)
float a=10.45678,x,y,z;
clrscr();
printf("%8.2e\n%10.4e\n%10.8e",a,a,a);
getch();
return 0;
Output:
1.04e+01
1.0456e+01
1.04567804e+01
Solution:
#include
#include
void main()
float a=345.6789;
clrscr();
getch();
Output:
Problem no.4.9: Write a program to read the name ANIL KUMAR GUPTA in
three parts using the scanf statement and to display the same in the
following format using the printf statement.
(b) A. K. GUPTA
(c) GUPTA A. K.
Solution:
#include
#include
void main()
char s[6],d[6],c[6];
clrscr();
printf("(b) %.1s.%.1s.%s\n",s,d,c);
printf("(c) %.1s.%.1s.\n",c,s,d);
getch();
Output:
( b) A. K. GUPTA
(d) GUPTA A. K.
Problem no.4.10: Write a program to read and disply the following table of
data
The name and code must be left-justified and price must be right-justified .
Solution:
#include
#include
void main()
{
int code1,code2;
float price1,price2;
char name1[10],name2[10];
clrscr();
scanf("%s%d%f",name1,&code1,&price1);
scanf("%s%d%f",name2,&code2,&price2);
printf("Name\tCode\tPrice\n");
printf("%-s\t%-d\t%.2f\n",name1,code1,price1);
printf("%-s\t%-d\t%.2f\n",name2,code2,price2);
getch();
Output: