C Program to Multiply Two Numbers

1. Introduction

Arithmetic is one of the pillars of programming. As we continue our journey into the basics of C programming, today's focus is on multiplication. Through a simple program that multiplies two numbers, we will explore fundamental coding practices such as variable usage, capturing user input, and displaying outputs.

2. Program Overview

Our program's structure involves:

1. Asking the user to provide two numbers.

2. Capturing and storing the numbers.

3. Multiplying the numbers.

4. Displaying the product.

3. Code Program

#include <stdio.h>  // Incorporating the Standard I/O library to utilize input and output functions

int main() {  // The main function, acting as the starting point of our program

    double num1, num2, product;  // Declare three variables of type double: two for the user's input and one for the multiplication result

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

    printf("Enter the second number: ");
    scanf("%lf", &num2);  // Capture and store the second number

    product = num1 * num2;  // Calculate the product of the two numbers

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

    return 0;  // Indicate the program has run successfully
}

Output:

Enter the first number: 5
Enter the second number: 4
The product of 5.00 and 4.00 is: 20.00

4. Step By Step Explanation

1. #include <stdio.h>: This inclusion integrates the standard input/output library, enabling us to use foundational functions such as printf and scanf.

2. int main(): All C programs commence their execution from the main function.

3. double num1, num2, product;: Here, we set aside memory for three double type variables to hold our two numbers and the resulting product.

4. The printf function: Utilized to display messages or prompts to the user.

5. The scanf function: This function captures user input. With %lf, we specify that we're dealing with the double datatype. The ampersand (&) signifies the memory address, indicating where in memory the input value should be saved.

6. product = num1 * num2;: This line carries out the multiplication operation.

7. return 0;: This line is a convention, indicating that the program executed without errors.

Through this basic multiplication program, newcomers to C programming are introduced to user interactions, arithmetic operations, and the structure of a simple program.

Comments