C program | Simple C program - Example of a C program that takes two numbers as input adds them and then prints the result

C program:

Here's a simple example of a C program that takes two numbers as input, adds them, and then prints the result:

c
#include <stdio.h>

int main() {
    // Declare variables to store user input and the result
    double num1, num2, sum;

    // Prompt the user to enter the first number
    printf("Enter the first number: ");
    scanf("%lf", &num1);

    // Prompt the user to enter the second number
    printf("Enter the second number: ");
    scanf("%lf", &num2);

    // Calculate the sum of the two numbers
    sum = num1 + num2;

    // Display the result
    printf("The sum of %.2lf and %.2lf is %.2lf\n", num1, num2, sum);

    return 0;
}

Simple  C program - Example of a C program that takes two numbers as input adds them and then prints the result
C programming language

This program uses the `printf` and `scanf` functions from the `<stdio.h>` library to interact with the user. The program declares variables `num1`, `num2`, and `sum` to store the user input and the result of the addition. The program prompts the user to enter two numbers, reads the input using `scanf`, calculates the sum, and then prints the result using `printf`.

ShowHideComments