Problem no. 4.1:Given the string“WORDPROCESSING”,
Write a program to read the string from the terminal and
Display the same in the following format:
(a)WORD PROCESSING
(b)WORD
PROCESSING
(c) W.P.
Solution:
#include <stdio.h>
int main()
{
char s[10], d[11];
printf(“enter the string=”);
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);
return 0;
}
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:
(a)(x+y)/(x-y) (b)(x+y)/2 (c)(x+y)*(x-y)
Solution:
#include<stdio.h>
main()
{
float x,y,a,b,c;
printf(“Enter the value of x & y: “);
scanf(“%f%f”,&x,&y);
if(x-y==0)
printf(“(a)=imagine”);
else
{
a=(x+y)/(x-y);
printf(“(a)=%f”,a);
}
b=(x+y)/2;
c=(x+y)*(x-y);
printf(“(b)=%f (c)=%f”,b,c);
return 0;
}
Problem no. 4.3: Write a program to read the following numbers, round them off to the nearest integers and print out the results in integer form:
35.7 50.21 -23.73 -46.45
Solution: #include<stdio.h>
main()
{
int a,b;
float c;
printf(“Enter the value of a:”);
for(a=1;a<=4;a++)
{
scanf(“%f”,&c);
if(c>=0)
b=c+0.5;
else
b=c-0.5;
printf(“Nearest integer number of %f is=%d\n”,c,(int) b);
}
getch();
}
Problem no.4.5: Write a program to demonstrate the process of multiplication. The program should ask the user to enter two two digit integer and print the product of integers as shown bellow.
45
X 37
7×45 is 315
3x45is 135
Add them 1665
Solution: #include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,p;
printf(“Enter 2 two digits number:”);
scanf(“%d%d”,&a,&b);
printf(” \t %d\n\tx%d\n”,a,b);
printf(“\t——\n”);
p=b/10;
c=b%10;
printf(“%dx%dis %d\n”,c,a,c*a);
printf(“%dx%dis %d\n”,p,a,p*a);
printf(“\t——-\n”);
printf(“Add them %d\n”,a*b);
printf(“\t——-“);
getch();
}
Problem no.4.6: Write a program to read three integers from the keyboard using one scanf statement and output them on one line using:
(a)three printf statements,
(b)only one printf with conversion specifiers and
(c) only one printf without conversion specifiers.
Solution: #include<stdio.h>
main()
{
int x,y,z;
printf(“Enter three integer value of x,y,&z:”);
scanf(“%d%d%d”,&x,&y,&z);
printf(“(a) X=%d,”,x);
printf(“Y=%d,”,y);
printf(“Z=%d\n”,z);
printf(“(b) X=%2d, Y=%2d, Z=%2d\n”,x,y,z);
printf(“(c) X= %d, Y=%d, Z=%d”,x,y,z);
getch();
}
Problem no.4.7: Write a program that prints the value 10.45678 in exponential format with the following specifications:
(a)correct to two decimal place,
(b)correct to four decimal place and
(c)correct to eight decimal place.
Solution: #include <stdio.h>
int main()
{
float a=10.45678;
printf(“%8.2e\n%10.4e\n%10.8e”,a,a,a);
return 0;
}
Problem no.4.8 Write a program to print the value 345.6789 in fixed-point format with the following specifications:
(a)correct to two decimal place,
(b)correct to four decimal place and
(c)correct to zero decimal place.
Solution: #include<stdio.h>
main()
{
float a=345.6789;
printf(“The two decimal place is: %.2f\n”,a);
printf(“The five decimal place is: %.5f\n”,a);
printf(“The zero decimal place is: %.0f”,a);
return 0;
}
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.
(a) ANIL K. GUPTA
(b) A. K. GUPTA
(c) GUPTA A. K.
Solution: #include<stdio.h>
main()
{
char s[6],d[6],c[6];
printf(“Enter the string:”);
scanf(“%5s%5s%5s”,s,d,c);
printf(“(a) %s %.1s. %s\n”,s,d,c);
printf(“(b) %.1s.%.1s.%s\n”,s,d,c);
printf(“(c) %s%.1s.%.1s.\n”,c,s,d);
return 0;
}
Problem no.4.10: Write a program to read and disply the following table of data
Name Code Price
Fan 67831 1234.50
Motor 450 5786.70
The name and code must be left-justified and price must be right-justified .
Solution: #include<stdio.h>
main()
{
int code1,code2;
float price1,price2;
char name1[10],name2[10];
printf(“Enter first name ,code and price :”);
scanf(“%s%d%f”,name1,&code1,&price1);
printf(“Enter second name ,code and price :”);
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();
}