scanf() function

scanf():

scanf() is a library function which is used to accept data from user through the input device. This is a input function which is used to accept data from user.

syntax: scanf("control strings", variables);


                  EXAMPLE OF SOME scanf() FUNCTION :



  1. Write a program to accept a number and show the number ?
CODING:

  1.         # include <stdio.h>
  2.         void main()
  3.            
  4.              int Number;
  5.              printf(“Enter a number ”);
  6.              scanf("%d",&Number);
  7.              printf("The input number is %d", Number);      
  8.        }

  OUTPUT:




  DISCUSSION:  Here in the program we use scanf () to accept data from the user and we can accept data at run time, after run the program we can accept data from the scanf function.


2. Write a program to accept two number’s and add them and show the result?

CODE:
  1.         # include <stdio.h>
  2.         void main()
  3.            
  4.              int Number1,Number2, Sum;
  5.              printf(“Enter value of Number1:  ”);
  6.              scanf("%d",&Number1);
  7.              printf(“\nEnter value of Number2:  ”);
  8.              scanf("%d",&Number2);
  9.              Sum = Number1 + Number2;
  10.              printf("The Result of Summation is %d", Sum);      
  11.        }
OUTPUT:

DISCUSSION: Here in the program we are producing summation of two variable Number1 and Number2 are integer type variables and a third variable called Sum which holds the result of summation. Value of Number1 is 5 and value of Number2 is 10and the summation of Number1 and Number2 is 15 which is the value of Sum variable.



3. Write a program to accept three integer number and add first two number and subtract the third number from the result?


CODE:

  1.         # include <stdio.h>
  2.         void main()
  3.            
  4.              int Number1,Number2, Number3, Sub;
  5.              printf(“Enter value of Number1:  ”);
  6.              scanf("%d",&Number1);
  7.              printf(“\nEnter value of Number2:  ”);
  8.              scanf("%d",&Number2);
  9.              printf(“\nEnter value of Number3:  ”);
  10.              scanf("%d",&Number3);
  11.              Number1 = Number1 + Number2;
  12.              Sub = Number1 - Number3;
  13.              printf("The Result of Subtraction is %d", Sub);      
  14.        }
OUTPUT:

DISCUSSION: Here in the program we are using 4 variables Number1, Number2, Number3 and last one is Sub, both are integer type of variables. In the program we accept data from user and then perform the operation. At first we accept value of Number1 variable (5) and then accept the Number2 variable (10) and perform the summation of Number1 and Number2 and store the result of summation in Number1 variable(15). After that we accept the value of Number3 variable(7) and then perform the subtraction operation of Number1(15) variable and Number3 (7)variable and store the result of subtraction in the variable Sub(8).

Process:  Number1 = 5
               Number2 = 10
               Number1 = 5 + 10
               Number1 = 15
               Number3 = 7
               Sub = 15 - 7
               Sub = 8


4. Write a programme to accept two integer number and perform multiplication operation?

CODING:



  1. # include <stdio.h>
  2. #include <conio.h>
  3. int main()
  4. {
  5.         int A1, B1;
    1.         printf("Enter A1 and B1 value: ");
    2.         scanf("%d %d", &A1,&B1);
    3.         A1 = A1 * B1;
    4.         printf("\n The result of multiplication is %d", A1);
    5.         getch();
    6.  }
    OUTPUT:


    DISCUSSION: Here is a simple program where we show a multiplication of two value which is given by the user.

    5. Write a program to accept 5 integers number, add first two numbers then subtract third to forth and add both summation result and subtraction result and divide the result with the fifth number?
     Result = ((1st + 2nd)+(3rd – 4th)) / 5th
                             
                                                      INTEGER TYPE VARIABLE
    CODE: 


    1. # include <stdio.h>
    2. # include <conio.h>
    3. int main()
    4. {
    5.         int N1,N2,N3,N4,N5,Result;
    6.         printf("Enter value of 1st variable :");
    7.         scanf("%d", & N1);
    8.         printf("Enter value of 2nd variable:");
    9.         scanf("%d", & N2);
    10.         printf("Enter value of 3rd variable:");
    11.         scanf("%d", & N3);
    12.         printf("Enter value of 4th variable:");
    13.         scanf("%d", & N4);
    14.         printf("Enter value of 5nd variable:");
    15.         scanf("%d", & N5);
    16.         Result = ((N1 + N2) + (N3 + N4)) / N5;
    17.         printf("\n The result is = %d", Result);
    18.         return 0;
    19. }

    OUTPUT:



    DISCUSSION: In this program we are showing a mathematical operation at first the user input 5 integer numbers and perform a operation. Result = ((N1 + N2) + (N3 + N4)) / N5.
    PROCESS:
    Result = ((N1 + N2) + (N3 + N4)) / N5
    Result = ((5 + 6) + (10 + 20)) / 8
    Result = (11 + 30) / 8
    Result = 41 / 8
    Result = 5   *Is it an error

    The actual result is 5.125 but in the program we get just 5 and this is not an error because Result is an integer type variable where we can able to store only integer type number that means we can not store any floating point number. So the result we get only 5. The solution of this problem is to use the float data type.

                                                 FLOAT/ REAL TYPE VARIABLE


    CODE: 


    1. # include <stdio.h>
    2. # include <conio.h>
    3. int main()
    4. {
    5.         float N1,N2,N3,N4,N5,Result;
    6.         printf("Enter value of 1st variable :");
    7.         scanf("%f", & N1);
    8.         printf("Enter value of 2nd variable:");
    9.         scanf("%f", & N2);
    10.         printf("Enter value of 3rd variable:");
    11.         scanf("%f", & N3);
    12.         printf("Enter value of 4th variable:");
    13.         scanf("%f", & N4);
    14.         printf("Enter value of 5nd variable:");
    15.         scanf("%f", & N5);
    16.         Result = ((N1 + N2) + (N3 + N4)) / N5;
    17.         printf("\n The result is = %f", Result);
    18.         return 0;
    19. }
    OUTPUT:


    DISCUSSION: In the program we are using float type or real type variable and we get proper result because the range of the float or real type variable is 4 byte and can able to store floating point numbers.




    6. Write a program to accept 5 and divide the number from 2 ?

    CODE:

    1. # include <stdio.h>
    2. # include <conio.h>
    3. int main()
    4. {
    5.        printf(" The result is : %d", 5/2);
    6.        return 0;
    7. }
    OUTPUT:


    DISCUSSION: The actual result of this operation is 2.5 but here we get 2 because in the program we use the control string "%d" and both the value are integer value, so we get the result is also a integer value.
                                                    REAL / FLOAT 
    1. # include <stdio.h>
    2. # include <conio.h>
    3. int main()
    4. {
    5.        printf(" The result is %f", 5/2.0);
    6.        return 0;
    7. }


    OUTPUT:

    DISCUSSION: In the program we a perfect result as we are using the control string "%f" which can able to store floating point number.



    7. Write a program to accept a letter and the output we get the corresponding ASCII value?

    CODE: 


    1. # include <stdio.h>
    2. # include <conio.h>
    3. int main()
    4. {
    5.         char ch;
    6.         printf("Enter a character : ");
    7.         scanf("%d",& ch);
    8.         printf(" ASCII value of the character is : %d", ch);
    9.         return 0;
    10. }

    OUTPUT:

    DISCUSSION: Here in the program we are showing the ASCII value of a given character. This is a simple program where we accept a character and at the time of showing we represent the character we represent with %d control string.


    8. Write a program to accept a integer and a real and a character value and show the value?

    CODE:


    1. # include <stdio.h>
    2. # include <conio.h>
    3. int main()
    4. {
    5.          int in;
    6.          float fl;
    7.          char ch;
    8.          printf("Enter integer value: ");
    9.          scanf("%d", & in);
    10.          printf("Enter real value: ");
    11.          scanf("%f", &fl);
    12.          printf("Enter character value: ");
    13.          scanf("%d", &ch);
    14.          printf(\n The given value is %d  %f   %c",in,fl,ch);
    15.          return 0;
    16. }

    OUTPUT:

    DISCUSSION: In the program we are using three variables with the different datatypes "in" as integer, "fl" as float and "ch" as char. After run the program enter the value and after that we show them on the screen.


    9. Write a program to accept a capital letter and show the  lower case of the same letter.

    CODE:


    1. # include <stdio.h>
    2. # include <conio.h>
    3. int main()
    4. {
    5.        char ch;
    6.        printf("Enter a letter ");
    7.        scanf("%c", & ch);
    8.        printf("The result is : %c", ch+32);
    9.        return 0;
    10. }
    OUTPUT:


    DISCUSSION: In the program the user input a capital letter and get a small letter. The ASCII value of capital letter is start from 65 and the ASCII value of small letter is start from 97, so the difference between them is 32.


    10. Write a program to accept a small letter and show the capital letter?

    CODE:

    1. # include <stdio.h>
    2. # include <conio.h>
    3. int main()
    4. {
    5.        char ch;
    6.        printf("Enter a letter ");
    7.        scanf("%c", & ch);
    8.        printf("The result is : %c", ch-32);
    9.        return 0;
    OUTPUT:


    DISCUSSION: In the program the user input a small letter and get a capital letter. The ASCII value of capital letter is start from 65 and the ASCII value of small letter is start from 97, so the difference between them is 32.

    11. Write a program to accept a letter and show the next letter of the given letter?

    CODE:

    1. # include <stdio.h>
    2. # include <conio.h>
    3. int main()
    4. {
    5.        char ch;
    6.        printf("Enter a letter ");
    7.        scanf("%c", & ch);
    8.        printf("The result is : %c", ch+1);
    9.        return 0;
    OUTPUT:

    DISCUSSION:  In the program we are showing that how can we find a letter with the ASCII value because in the program we enter 'A' and we know that the next letter is 'B', simply we add 1 with the given character and we get the result. When we add 1 with the given letter then the addition is perform with the ASCII value of the given letter.


    12. Write a program to accept a letter and show the previous letter of the given letter?

    CODE:

    1. # include <stdio.h>
    2. # include <conio.h>
    3. int main()
    4. {
    5.        char ch;
    6.        printf("Enter a letter ");
    7.        scanf("%c", & ch);
    8.        printf("The result is : %c", ch-1);
    9.        return 0;
    OUTPUT:

    DISCUSSION:In the program we are showing that how can we find a letter with the ASCII value because in the program we enter 'C' and we know that the previous letter is 'B', simply we subtract 1  from the given character and we get the result. When we subtract 1 from the given letter then the operation is perform with the ASCII value of the given letter.











    Translate