Advertisement

printf() and scanf() in C

The printf() and scanf() functions are used for enter and output in c language. each features are in-built library features, defined in stdio.h (header record).

  • printf() function

The printf() function is used for output. It prints the given assertion to the console.

 

The syntax of printf() function is given beneath

Syntax

int number = 10;

printf(“The value of number is: %d\n”, number);

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

  • scanf() function

The scanf() function is used for enter. It reads the enter information from the console.

Syntax

int scanf(const char *format, …);

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 num;

// Prompting the user to enter a number

printf(“Please enter a number: “);

// Reading the input number from the user

scanf(“%d”, &num);

// Calculating the cube of the number

int cube = num * num* num;

// Displaying the result

printf(“The cube of %d is: %d\n”, num, cube);

return 0;

}

Output

Please enter a number: 10

The cube of 10 is: 1000

 

Program to print multiplication of 2 numbers

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

#include <stdio.h>

int main() {

int num1, num2, mult;

// Prompt the user to enter the first number

printf(“Enter the first number: “);

scanf(“%d”, &num1);

// Prompt the user to enter the second number

printf(“Enter the second number: “);

scanf(“%d”, &num2);

// Calculate the product of the two numbers

mult = num1 * num2;

// Display the result

printf(“The Multiplication of %d and %d is: %d\n”, num1, num2, mult);

return 0;

}

Output

Enter the first number: 5

Enter the second number: 10

The Multiplication of 5 and 10 is: 50

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top