Saturday 6 January 2018

Inputing Integer Number in C Language | best site to learn c programming | c programming codes




Inputing Integer Number
basic c programs for beginners

 The field specification for reading an integer number is:%wd
  the percent sign(%) indicates that a conversion specification follows. W is an integer number,that specifies the field width of the number to be read and d known as data type character, indicates that the number to be read i in 
integer mode.


Example:      scanf("%2d %5d",&num1,&num2);
data line:       60        13456
 the value 60 is assigned to num1 and 13456 to num2.Input data item must be separated by spaces,tab or new line.Punctuation mark do not count a separator.Various input format ting options for reading integers are experimented with in the program.


Program:  
        /*Reading integer number*/
        main()
        {
         int a,b,c,m,n;
         int i,j,k;
         printf("enter three integer numbers \n");
         scanf("%d%*d%d",&a,&b,&c);
         printf("%d%d%d\n\n",a,b,c);
         printf("enter two 4-integer numbers \n");
         scanf("%d%4d",&m,&n);
         printf("%d%d\n\n",m,n);
         printf("enter two integer numbers \n");
         scanf("%d%*d%d",&a,&m);
         printf("%d%d%d\n\n",a,m);
         printf("enter a nine digit numbers \n");
         scanf("%3d%4d%3d",&i,&j,&k);
         printf("%d%d%d\n\n",i,j,k);
         printf("enter two three digits \n");
         scanf("%d%*d%d",&m,&n);
         printf("%d%d%d\n\n",m,n);
        }

Output:           enter  three  numbers
                  1   2   3
                  1   3  -3577
                  enter  two 4-digit numbers
                  2345          6417
                  23  45
                  enter two integers
                   44        66
                   4321     44     
                  enter  a nine digit number
                   123456789
                   66  1234 567
                  enter two three digit numbers
                   123   456
                   89    123


Inputting Real Number

Unlike integer numbers,the field width of real numbers s not to be specified and therefore scanf reads real numbers using the simple specifications %f for both the notations,namely decimal point and exponential notation.
            Example:      scanf(%f%f%f",&x,&y,&z);
            input data 
                      475.89       43.21e-1         678
will assign the value 475.89 to x,4.321 to y and 678.0 to z. The input field specifications may be separated by an arbitrary blank spaces. If the  number to be read is of double type ,then the pecification should be lf% instead
of %f. A number may be skipped using %*f.

Program:         
         /*Reading of real numbers */
         main()  
         {
          float x,y;
          double  p,q;
          printf("values of x and y:");
          scanf("%f\n%e",&x,&y);
          printf("\n");
          printf("x=%f \n y=%f\n\n",x,y);
          printf("values of p and q:");
          scanf("%lf%lf",&p,&q);
          printf("\n p=%lf \n q=%e",p,q);
          printf("\n\n p=%12lf \n p=%12e",p,q);
         }        

Output:   values of x and y:12.3456 17.5e-2
           x=12.345600
           y=0.175000
          values of p and q: 4.14285178  18.5678901234567890
                         p=4.14285178 
                         q=1.856789012346e+00

   

Inputting Character Strings

Single  character can be  read from the  terminal using the getchar function .The same can be achieved  using the scanf function also. In addition,a function can input strings contain ing more than one character. Following are the specifications for reading character strings: %ws   or  %wc

Program:  /* Reading of strings using %wc and %ws */ 

             main()
             {
              int num;
              char name1[15],name2[15],name3[15];
              printf("enter serial number and name one \n");
              scanf("%d %15c",&num,&name1);
              printf("%d %15s\n\n",num,name1);
              printf("enter serial number and name two \n");
              scanf("%d %s",&num,&name2);
              printf("%d %15s\n\n",num,name2);
              printf("enter serial number and name three \n");
              scanf("%d %15s",&num,&name3);
              printf("%d %15s\n\n",num,name3);
             } 

Output:         enter serial number and name one
                 1          123456789012345
                 1          123456789012345r
                enter serial number and name two
                 2       New York
                 2       New          
                enter serial number and name three
                 2        York
                enter serial number and name one
                 1      123456789012
                 1      123456789012  r
                enter serial number and name two
                 2        New-York
                 2        New-York
                enter serial number and name three
                 3        London

In the above program,the specification %s terminates reading at the encounter of a blank space.Therefore,name2 has  read only the first part of " New York" and the second part is automatically assigned to name3.However,during the second run,the string "New York" is correctly to name2.











0 comments:

Post a Comment