printf() function

printf(): 

printf is a library function which is used to show on the screen, it is an output function. Through this function the programmer can show some ting in the computer screen.



Syntax: printf(“string”);



Example of some sample program of printf( ):


A     Write a program to show your name in the screen.

CODING:
  1.         # include <stdio.h>
  2.         void main()
  3.            
  4.               printf(“Amit Roy”);
  5.              }
     OUTPUT:

OUTPUT DISCUSSION:  This example is showing how to use the printf function. Here in the example we are use a printf function which is used to show a string or group of characters in the computer screen. The string should write in double invited comma.



B     Write a program to show your name and your parents name.

CODE:
  1.   # include <stdio.h>
  2.   void main()
  3.   {         
  4.         printf(“Amit Roy”);
  5.         printf(“Mr Suraj Roy”);
  6.    }
OUTPUT:

DISCUSSION: This example is showing how to use two printf function in a program. We can use multiple function in a program, but if can see the output then you can find that there is no any space between the Amit Roy and Suraj Roy because we can't create any space in the printf function also.


C. Write a program to show your name and some of your 3 or 4 friends.

CODE:
    1.       # include <stdio.h>
    2.       void main()
    3.       {
    4.             printf(“Jack Oliver Harry James”);
    5.        }

OUTPUT:



DISCUSSION: Here is a another example of printf function where we are showing two name in a single printf function. we can show a long string in a single printf function.

    

 D.  White a program to show the 5 names in vertically (one after another).

CODE:
1.       # include <stdio.h>
2.       Void main()
3.       {
4.             printf(“Jack\nOliver\nHarry\nJames”);
5.       }
OUTPUT:

DISCUSSION: This output is different from the previous because we use a backslash character "\n" which use as end line or enter.


 E.  Write a program to show your name and after pressing enter key show your friend name after clear your name from screen.

CODE:

1.       # include <stdio.h>
2.       # include <conio.h>
3.       Void main()
4.       {
5.              printf(“Sam Johnson”);
6.              getch();
7.              printf(“John”);
8.         }












     DISCUSSION: This program is showing a string "Sam johnson" then stop the execution until press any key and then show another string "John". In the program we use a function getch(), which is mainly used to accept any character from keyboard.


  F. Write a program to show three names with 5 space each.

CODE:
1.       # include <stdio.h>
2.       # include <conio.h>
3.       Void main()
4.       {
5.             printf(“ John\t sham \tJohnson”);
6.         }

        OUTPUT: 



DISCUSSION: In the program we are showing three names with five space or tab space. In the program we use "\t" for 5 space or tab space.


G. Write a program to show the name of any 5 cities, just using one printf() function.

CODE:
1.       # include <stdio.h>
2.       # inclue <conio.h>
3.       Void main()
4.       {
5.                  printf(“New York, Chicago, Mumbai, Los Angeles, San              Francisco");
6.         }

OUTPUT:





DISCUSSION: Here in the program where we are showing the different cities in the world with the help of a single printf function.


H.       Write a program to show your name, age and marks on the screen.

CODE:
1.       # include <stdio.h>
2.       # include <conio.h>
3.      void main()
4.       {
5.              printf(“NAME : John\nAGE : 29 \nMARKS : 98”);
6.         }

              OUTPUT:
       

DISCUSSION: In the program we are showing NAME: John, AGE:29, MARKS:98 one by one vertically with the help of "\n" which is use to create a new line.






I. Write a program to show corresponding ascii for the particular letter.

CODE:
  1. # include <stdio.h>
  2. void main()
  3. {
  4.        char ch;
  5.        ch = 'A';
  6.        printf("The ASCII value of the letter is : %d", ch);
  7. }

OUTPUT:




DISCUSSION: Here in the program where we are showing the ASCII value of "A". "A" is a character so if we use %d then we can get the result.




J. Write a program to show your name in the middle of the screen.



  1. # include <stdio.h>
  2. void main()
  3. {
  4.       printf("\n\n\n\n\n\t\t\t\tSam Johnson");
  5. }
OUTPUT:

DISCUSSION: Here we are showing a string in middle of the window with the help of "\n" and "\t".




K. Write a program to show the summation of two values.

CODE:
  1. # include <stdio.h>
  2. void main()
  3. {
  4.       printf(" summation of 5 and 10 is = %d", 5+10);
  5. }
OUTPUT:

DISCUSSION: Here we are showing addition of 5 and 10 in the printf function. In the program we use%d, and %d is a control string which is used to show an integer number value in a particular area of the printf function were the control string is used.




L. Write a program to show the subtraction of two variables.



CODE:
  1. # include <stdio.h>
  2. void main()
  3. {
  4.       int A,B;
  5.       A = 10;
  6.       B = 5;
  7.       A = A - B;
  8.       printf("subtraction result is %d",A);
  9. }

OUTPUT:

DISCUSSION: Here in the program we are doing subtraction operation of two variable integer type variables A and B. The value of variable A is 10 and B is 5.  We perform A = A - B that means subtract the variable B from A and store the result in the variable A. After perform the value of the variable A is 5.


M. Write a program to show the multiplication of multiple variables.  

  1. # include <stdio.h>
  2. void main()
  3. {
  4.       int A,B,C = 5;
  5.       A = 10;
  6.       B = 5;
  7.       A = A * B * C;
  8.       printf("Multiplication result is %d",A);
  9. }

OUTPUT:


DISCUSSION: Here in the program we are showing multiplication operation of three variables A, B, C and the value of variables are, value of variable A is 10 and the value of variable B is 5 and the value of variable C is 5. We are showing the multiplication operation of A, B, C and the result of multiplication operation is stored in the variable A, so now the value of the variable A is 250.


process:    A = A * B * C;

                  A = 10 * 5 * 5;
                  A = 50 * 5;
                  A = 250 


N. Write a program to show the division of multiple variables.

CODE:
  1. # include <stdio.h>
  2. void main()
  3. {
  4.       int A,B,C;
  5.       A = 10;
  6.       B = 5;
  7.       C = 2;
  8.       A = A / B / C;
  9.       printf("subtraction result is %d",A);
  10. }

OUTPUT:


DISCUSSION: Here in the program we are showing division operation of three variables a, b, c and the value of variables are, value of variable A is 10 and the value of variable B is 5 and the value of variable C is 2. We are showing the division operation of A, B, C and the result of division operation is stored in the variable A, so now the value of the variable a is 1.


process:    A = A / B / C;

                  A = 10 / 5 / 2;
                  A = 2 / 2;
                  A = 1;


O. Write a programme to show triangle structure of '*' with the help only printf()?

  1. # include <stdio.h>
  2. void main()
  3. {
  4.       printf("* * * * * * ");
  5.       printf("\n* * * * *");
  6.       printf("\n* * * *");
  7.       printf("\n* * *");
  8.       printf("\n* *");
  9.       printf("\n*");
  10. }

OUTPUT:


DISCUSSION: In the program we want to give an idea of printf function that we can write or show any thing on the computer screen. If we couldn't use any kind of methods or technique then alos we can able to show any thing on the computer screen but the effort requred is vary high for the programmer.  


P. Write a program to show consecutive 5 names but show one after another by pressing the enter key?

CODE:
  1. # include <stdio.h>
  2. void main()
  3. {
  4.       printf(" Emily"); getch();
  5.       printf(" Jessica"); getch();
  6.       printf(" Jennifer"); getch();]
  7.       printf(" Lucy"); getch();
  8.       printf(" Lilly"); getch();
  9. }

OUTPUT:


DISCUSSION:  Here in the program we are showing five names but the name will shown on the computer screen one after another by pressing any key. One after another the name will showed on the computer screen.


Q. Write a program to show the ASCII value of characters?

  1. # include <stdio.h>
  2. void main()
  3. {
  4.       printf(" ascii value 65 = %d\n ASCII value 53 = %c",65,53);
  5. }



OUTPUT:

DISCUSSION: Here in the program we are showing the ASCII value of characters with the help of the c program. If you see the program the you understand that we are using the control string of integer value instead of character value and using the character control string instead of the character's and we get our required result.












Translate