C Program to Multiply Two Numbers

C Program to Multiply Two Numbers

Multiplication is a fundamental arithmetic operation in programming. Learning to multiply numbers in C helps beginners understand variables, arithmetic operators, and basic input-output functions. A program to multiply two numbers might seem simple, but it is an excellent way to practice how C programs perform calculations, store results, and display them on the screen. By the end of this guide, you will know how to write a complete C program to multiply two numbers, understand each line of code, and avoid common beginner mistakes.

Multiplying numbers in C also introduces the concept of data types and precision. Depending on whether you are working with whole numbers or decimals, choosing the correct variable type ensures accurate calculations. This tutorial will cover multiplication using integers, floating-point numbers, and fixed values, giving you a solid foundation for more complex arithmetic operations.

Using printf() and scanf() to Multiply Two Numbers

The most common method to multiply numbers is by reading input from the user using scanf() and displaying the result with printf(). Both functions are part of the standard input-output library, which is included using #include <stdio.h>. Here’s a complete example:

#include <stdio.h>

int main() {

    int num1, num2, product;

    printf("Enter the first number: ");
    scanf("%d", &num1);

    printf("Enter the second number: ");
    scanf("%d", &num2);

    product = num1 * num2;
    printf("The product of %d and %d is %d\n", num1, num2, product);

    return 0;

}

In this program, we first include the standard input-output library so that printf() and scanf() can be used. We declare three integer variables: num1, num2, and product. The printf() statements prompt the user to enter two numbers. Using scanf("%d", &num1); reads the first number and stores it in num1. The & symbol passes the memory address of the variable to store the input properly.

The multiplication is performed using the * operator in the line product = num1 * num2;, and the result is stored in the variable product. Finally, printf("The product of %d and %d is %d\n", num1, num2, product); displays the output. This example introduces the use of arithmetic operators and formatted output in C.

Multiplying Floating-Point Numbers

To multiply numbers with decimals, you can use float or double variables. These data types store fractional numbers and allow precise calculations. Here’s an example using double:

#include <stdio.h>

int main() {

    double num1, num2, product;

    printf("Enter the first number: ");
    scanf("%lf", &num1);

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

    product = num1 * num2;
    printf("The product of %.2lf and %.2lf is %.2lf\n", num1, num2, product);

    return 0;

}

In this program, num1, num2, and product are declared as double. The scanf("%lf", &num1); function reads a double value from the user. The printf() function displays the result with two decimal places using %.2lf. Using floating-point variables ensures accurate multiplication for numbers with fractions, which is important for calculations in science, finance, or measurements.

Multiplying Fixed Values

Sometimes, you might want to multiply two numbers directly in the program without taking input. This method is useful for testing or performing simple calculations. Here’s an example:

#include <stdio.h>

int main() {

    int num1 = 10;
    int num2 = 5;
    int product;

    product = num1 * num2;
    printf("The product of %d and %d is %d\n", num1, num2, product);

    return 0;

}

In this code, values are directly assigned to num1 and num2. The multiplication operation and the output are handled in the same way as before. While this method is simple, it does not allow user input, making it less flexible for interactive programs.

Common Beginner Mistakes

When multiplying numbers in C, beginners often encounter these issues:

  • Using uninitialized variables, which can lead to incorrect results.
  • Using the wrong format specifier in scanf() or printf(). For example, %d instead of %lf for double variables can produce unexpected outputs.
  • Overlooking operator precedence when combining multiplication with other arithmetic operations; parentheses can help avoid mistakes.

To prevent these errors, always declare and initialize your variables properly, match format specifiers to variable types, and use parentheses when needed to control operation order.

FAQs

Q1: Can I multiply more than two numbers in C?
Yes, you can multiply multiple numbers by chaining the * operator, like result = num1 * num2 * num3;. For larger sets, loops and arrays can be used.

Q2: What is the difference between int and double for multiplication?
int stores whole numbers, while double can store decimal numbers. Use double for more precise calculations involving fractions.

Q3: Can multiplication be done directly inside printf()?
Yes, you can write printf("Product is %d", num1 * num2); to calculate and display the result without storing it in a variable.

Q4: What happens if I multiply a large integer that exceeds the variable limit?
If the result exceeds the storage limit of the variable type, an overflow occurs, and the value may be incorrect. Use larger data types like long or double if necessary.

Conclusion

Multiplying two numbers in C is a simple yet essential task for beginners. It teaches variables, arithmetic operators, and input-output handling. Whether using integers, floating-point numbers, or fixed values, this program builds a strong foundation for more advanced arithmetic operations. Practicing multiplication programs will help you understand how C performs calculations and prepare you for solving complex problems. Start experimenting with different numbers and see how your programs respond.

References & Additional Resources

  1. Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language. 2nd Edition, Prentice Hall, 1988.
  2. TutorialsPoint: C Arithmetic Operators – Detailed explanation of arithmetic operations.
  3. Wikipedia: C Data Types – Overview of integer and floating-point data types in C.
Scroll to Top