Ads

Thursday, May 7, 2020

C Programing Language: printf() function & scanf() function

 printf() function 

The printf() function:The printf() function is used for all output process. It prints the given statement to the console. The syntax of printf () function is that: 

printf("format string",argument_list);


 The format string can be %d (integer), %c (character), %f (float),%lf (double),%s (string) etc.



scanf() function 
The scanf() function:The scanf() function is used for input. It reads the input data from the console. 

scanf("format string",argument_list); 

Program to print cube of given number 
Let's see a simple example of c language that gets input from the user and prints the cube of the given number. 

#include<stdio.h> 
int main()

int number; 
printf("enter a num:"); 
scanf("%d",&num); 
printf("cube of number is:%d ",number*number*number); 
return 0; 
}

Output 
enter a num:5 
cube of number is:125 

The scanf("%d",&number) statement reads integer number from the console and stores the given value in number variable. 

The printf("cube of number is:%d ",number*number*number) statement prints the cube of number on the console. 

Program to print sum of 2 numbers

 Let's see a simple example of input and output in C language that prints addition of 2 numbers. 

#include<stdio.h> 
int main()

int x=0,y=0,result=0; 
printf("first number:"); 
scanf("%d",&x); 
printf("second number:"); 
scanf("%d",&y); 
result=x+y; 
printf("sum of 2 numbers:%d ",result); 
return 0; 


Output 
first number:9 
second number:9 
sum of 2 numbers:18

0 comments:

Post a Comment