Sure, here's a simple C program that takes two numbers as input from the user, calculates their sum, and then prints the result:
#include <stdio.h>
int main() {
// Declare variables to store the two numbers
int num1, num2;
// Prompt the user to enter the first number
printf("Enter the first number: ");
// Read the first number from the user
scanf("%d", &num1);
// Prompt the user to enter the second number
printf("Enter the second number: ");
// Read the second number from the user
scanf("%d", &num2);
// Calculate the sum of the two numbers
int sum = num1 + num2;
// Print the result
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}
In this example
stdio.h
header file to use standard input/output functions.main()
function, we declare two variables num1
and num2
to store the two numbers.printf()
.scanf()
and store it in the variable num1
.num2
.sum
.printf()
.When you run this program, it will prompt you to enter two numbers, calculate their sum, and then print the result. For example:
Enter the first number: 10
Enter the second number: 20
The sum of 10 and 20 is 30
This is a basic example to demonstrate how to take input from the user, perform a calculation, and print the result in C.