E.Balagurusamy ANSI C Programming solution Chapter-04

ANSI C Programming solution Chapter-four
Managing Input and output operation 
Chapter Four(04) contains
01.Reading a Character
02.Writing a Character
03.Formatted Input
04.Formatted Output
Solution of Exercise
A :scanf(“%c %f %d”,city,&price,&year);
-Not Error
B scanf(“%s %d”, city,amount);
-THERE WILL BE A & BEFORE AMOUNT.
C: scanf(“%f %d”,&amount,&year);
-Not Error
D: scanf(\n”%f”, root);
-\N WILL REMAIN INTO DOUBLE QUOTE.
E:scanf(“%c %d %ld”,*code, &count, root);
-IS NOT ALLOWED BEFORE CODE AND &WILL STAY BEFORE ROOT.

(4.7) WHAT WILL BE THE VALUES STORED OF THE VARIABLES YEAR AND CODE WHEN THE DATA 1988,X ?
A: scanf(“%d %c”,&year,&code);
-YEAR STORS 1988 AND CODE STORS X.
B: scanf(“%c %d”,&year,&code);
-YEAR STORS X AND CODE STORS 1988.
C :scnaf(“%d %c”,&code,&year);
-CODE STORS 1988 AND YEAR STORS X.

(4.8) COUNT,PRICE,CITY HAVE VALUES:
     COUNT=1275,
    PRICE=235.74,
    CITY=CAMBRIDGE.
       WHAT WILL BE THE OUTPUT THE STATEMENT ?
A: printf(“%d %f”,count,price);
Output-1275  235.75.

B:printf(“%d %f”,price,count);
Output-36576  790980

C:printf(“%c”,city);
Output-CAMBRIDGE
(4.9)SHOW THE WRONG OF THE OUTPUT STATEMENTS.
A:printf(“%d.7.2%f”,year,amount);
 WRONG=7.2 SHOULD REMAIN AFTER THE %
B:printf(“%-s,%c”\n,city,code);
WRONG=COMMA IS NOT ALLOWED AND \N SHOULD STAY INTO QUOTATION.
C:printf(“%f %d %s”,price,count,city);
-not wrong
(4.10)WHAT VALUES DOSE THE COMPUTER ASSIGN OF THIS INPUT STATEMENTS?
scanf(“%4d %*d”,&year,&code,&count);
          IF DATA KYED IN 19883745
Output-1988.
(4.11) HOW CAN WE USE  getcher()  FUNCTION TO MULTICHARACTER STRINGS?
-BY INCLUDING  SINGLE  QUOTATION  OVER  MULTICHARACTER WE  CAN  USE  getchar()  FUNCTION.
(4.12)HOW CAN WE USE  putchar()  FUNCTION  TO  MULTICHARACTER STRINGS?
-BY INCLUDING  SINGLE  QUOTATION  OVER MULTICHARACTER WE  CAN USE  putchar()  FUNCTION.
(4.13)WHAT IS THE PURPOSE OF SCANF() FUNCTION?
-IF  WE WANT TO TAKE DATA AFTAR RUNNING THE PROGRAMM THEN
  WE USE  scanf()  FUNCTION.
(4.14) DESCRIBE THE PURPOSE OF  COMMONLY  USED  CONVERSION CHARACTERS  IN  A scanf()  FUNCTION ?
-IT INDICATES WHAT TYPES OF DATA WE TAKE AS INPUT.

(4.16)WHAT IS THE PURPOSE OF  printf()  FUNCTION ?
-IT IS USED TO SHOW ANYTHIG ON OUTPUT.

(4.15)WHAT HAPPENS WHEN AN INPUT DATA ITEM CONTAIN ?
A: MORE CHARACTERS THAN SPECIFIED FIELD WIDTH.
-VALUE WILL BE RIGHT-JUSTIFIED.
B: FEWER CHARACTER THAN SPECIFIED FIELD WIDTH.
-VALUE WILL BE LEFT-JUSTEFIED.
(4.17)DESCRIBE THE PURPOSE OF COMMONLY USED CONVERSION CHARACTERS  IN A printf()  FUNCTION ? 
- IT INDICATES WHAT TYPES OF DATA WE WANT TO SHOW ON
OUTPUT.
(4.18) WHAT HAPPENS WHEN AN 0UTPUT DATA ITEM CONTAIN ?
A: MORE CHARACTERS THAN SPECIFIED FIELD WIDTH.
-VALUE WILL BE RIGHT-JUSTIFIED.
B:FEWER CHARACTER THAN SPECIFIED FIELD WIDTH.
-VALUE WILL BE LEFT-JUSTEFIED.

Solution of  Programming Exercise

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>
#include<conio.h>
  void main()
  {
  float x,y,a,b,c;
  clrscr();
  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)=%.2f",a);
  }
  b=(x+y)/2;
  c=(x+y)*(x-y);
  printf("  (b)=%.2f  (c)=%.2f",b,c);
  getch();
  }
Output:
Enter the value of x &  y:  4   3
(a)=7.00
(b)=3.50
(c)=12.00
Enter the value of x &  y:  7   7
(a)= imagine
(b)=7.00
(c)=0.00

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>
#include<conio.h>
void main()
   {
    char s[10],d[11];
    clrscr();
    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);
getch();
}
Output:
Enter the string:   WORDPROCESSING
         (a)  WORDPROCESSING
         (b)  WORD
       PROCESSING
(c)  W.P.

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>
#include<conio.h>
void main()
   {
   int p,i;
   float a;
   clrscr();
   printf("enter  real number  for get nearest integer number\n");
       for(i=1;i<=4;i++)
       {
       scanf("%f",&a);
       if(a>=0)
            p=a+0.5;
       else
            p=a-0.5;
              printf("\nnearest integer number of  %f is= %d\n",a,(int)p);
       }
   getch();
   }
Output:
enter  real number  for get nearest integer number  35.7
nearest integer number of  35.7 is= 36
enter  real number  for get nearest integer number 50.21
nearest integer number of  50.21 is=50  
enter  real number  for get nearest integer number  -23.73
nearest integer number of   -23.73 is=   -24
enter  real number  for get nearest integer number -46.45
nearest integer number of  -46.45 is=   -46

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 follows,

         *   *   *   *
         *   *   *   *    4.36
         *   *   *   *
Solution:
#include<stdio.h>
#include<conio.h>
  void main()
  {
    float a1,a2,a3,a4;
    int x,y,z,t,i;
    clrscr();
    printf("Enter four float number:");
    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;
    printf("The horizontal bar chard is:\n");
    for(i=0;i<x;i++)
    printf("*  ");
    printf("%.2f\n",a1);
    for(i=0;i<y;i++)
    printf("*  ");
    printf("%.2f\n",a2);
    for(i=0;i<z;i++)
    printf("*  ");
    printf("%.2f\n",a3);
    for(i=0;i<t;i++)
    printf("*  ");
    printf("%.2f\n",a4);
    getch();
   }
Output:
Enter four float number:  4.85   4.36   3.12   5.47
The horizontal bar chard is:
*   *   *    *   *   4.85
*   *   *    *    4.36
*   *   *     3.12
*   *   *    *    *    5.47
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
             7x45 is                     315
            3x45is                     135  
Add them                           1665               

Solution:                                   
#include<stdio.h>
#include<conio.h>
void main()
     {
     int a,b,c,p;
     clrscr();
     printf("Enter 2  two digits number:");
     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("Add them %d\n",a*b);
     printf("\t-------");
     getch();
     }
Output:
                                                 45
                                    X          37
             7x45 is                      315
            3x45is                      135 
Add them                           1665

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>
#include<conio.h>
void main()
  {
     int x,y,z;
    clrscr();
    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=%3d, Y=%2d, Z=%2d\n",x,y,z);
    printf("(c) X= %d, Y=%d, Z=
    %d",x,y,z);
    getch();
 }
Output:
Enter three integer value of x,y,&z:  45   27   89
(a)   X=45,   Y=27,   Z=89
(b)   X=45,   Y=27,   Z=89
(c)   X=45,   Y=27,   Z=89

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>
#include<conio.h>
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

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>
#include<conio.h>
void main()
    {
   float a=345.6789;
   clrscr();
   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);
   getch();
   }
Output:
The two decimal place is: 345.67
The five decimal place is: 345.67889
The two decimal place is: 345

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>
#include<conio.h>
void main()
    {
    int code1,code2;
    float price1,price2;
    char name1[10],name2[10];
    clrscr();
    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();
    }
Output:
Enter first name ,code and price :   Fan              67831             1234.50
Enter second name ,code and price :  Motor           450                 5786.70

 Name             Code              Price
Fan                 67831             1234.50
Motor             450                 5786.70

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>
#include<conio.h>
void main()
   {
    char s[6],d[6],c[6];
    clrscr();
    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);
    getch();
   }
Output:
Enter the string:   ANIL KUMAR GUPTA
(a)    ANIL  K. GUPTA
( b)   A. K. GUPTA
(d)    GUPTA A. K.

ANSI C Programming E.Balagurusamyb ebook solution download now. E.Balagurusamy ANSI C Programming whole e-book solution download.Study chapter four and get knowledge about Writing a Character,Reading a Character, Formatted Output,Formatted Input for higher programming implementation.

Popular Posts