printf() and scanf() in C
The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file).
printf() function
The printf() function is used for output. It prints the given statement to the console.
The syntax of printf() function is given below :
printf("format string", argument_list);
The format string can be %d (integer), %c (character), %s (string), %f (float) etc.
scanf() function
The scanf() function is used for input. It reads the input data from the console.
The syntax of scanf() function for numerical values is given below :
scanf("format string", &argument_list);
The syntax of scanf() function for character values is given below :
scanf("format string", argument_list);
Program to print cube of given number
#include <stdio.h>
int main()
{
int number;
printf("Enter a number : ");
scanf("%d", &number);
printf("Cube of %d is : %d \n", number, number*number*number);
return 0;
}
Output :
Enter a number : 5
Cube of 5 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 %d is : %d \n", number, number*number*number); statement prints the cube of number on the console.
Program to print sum of 2 numbers
#include <stdio.h>
int main()
{
int x, y, result;
printf("Enter first number : ");
scanf("%d", &x);
printf("Enter second number : ");
scanf("%d", &y);
result = x + y;
printf("The sum of %d and % d is : %d \n", x, y, result);
return 0;
}
Output :
Enter second number : 5
The sum of 5 and 5 is : 10
Comments
Post a Comment