C Program For Multiplication Of Two Numbers | Multiply Effortlessly

C Program For Multiplication Of Two Numbers | Multiply Effortlessly

Embark on a fascinating journey into the world of programming magic with a simple yet powerful incantation – the “C program for multiplication of two numbers.” Picture this program as your digital math assistant, capable of swiftly multiplying any two numbers you throw its way! It’s like having a handy calculator, but with a touch of coding wizardry.

Just imagine typing in your numbers, and voilà – the program performs its enchantment, revealing the product. Join the adventure of creating this marvelous spell, where lines of code weave together to bring the mathematical realm to life!

Algorithm:

Start
  Declare variables num1, num2, and result of type float.
  Input the first number (num1).
  Input the second number (num2).
  Multiply num1 and num2, store the result in the variable result.
  Display the result.
End

Pseudocode:

Start
  Declare num1, num2, result as float
  
  Input num1
  Input num2
  
  result = num1 * num2
  
  Display result

How do you multiply two numbers in C?

The ‘*‘ operator can be used to add two numbers in C. Check below example

#include <stdio.h>

int main() {
    int num1, num2, result;

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

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

    // Multiplication
    result = num1 * num2;

    // Display Output
    printf("The product of %d and %d == %d\n", num1, num2, result);

    return 0;
}

Time complexity of the arithmetic operation (num1 * num2) is O(1) since it is a fundamental operation with a constant execution time.

More Examples on C Program For Multiplication Of Two Numbers

Example 1 : C Program to Multiply Two Floating-Point Numbers:

#include <stdio.h>

int main() {
    float num1, num2, result;

    // Input
    printf("Enter the first number: ");
    scanf("%f", &num1);

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

    // Multiplication
    result = num1 * num2;

    // Display Output
    printf("The product of %.2f and %.2f == %.2f\n", num1, num2, result);

    return 0;
}

The time complexity of the multiplication operation is O(1) for floating-point multiplication

Example 2 : C Program for Multiplication of Two Binary Numbers:

#include <stdio.h>

long long multiplyBinary(int binary1, int binary2) {
    int i = 0, remainder = 0, sum[20];
    long long binaryProduct = 0;

    while (binary1 != 0 || binary2 != 0) {
        sum[i++] = (binary1 % 10 + binary2 % 10 + remainder) % 2;
        remainder = (binary1 % 10 + binary2 % 10 + remainder) / 2;
        binary1 = binary1 / 10;
        binary2 = binary2 / 10;
    }

    if (remainder != 0)
        sum[i++] = remainder;


    --i;

    while (i >= 0)
        binaryProduct = binaryProduct * 10 + sum[i--];

    return binaryProduct;
}

int main() {
    int binary1, binary2;

    // Input binary numbers
    printf("Enter the first binary number: ");
    scanf("%d", &binary1);

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

    // Multiply binary numbers
    long long result = multiplyBinary(binary1, binary2);

    // Display Output
    printf("Product of %d and %d in binary: %lld\n", binary1, binary2, result);

    return 0;
}

The amount of time needed to do binary multiplication is O(log(n)), where n denotes the maximum number of digits in the given binary numbers.

Example 3 : C Program for Multiplication of Two Numbers Using Functions

#include <stdio.h>

// Function to multiply two numbers
float multiply(float num1, float num2) {
    return num1 * num2;
}

int main() {
    float num1, num2;

    // Input
    printf("Enter the first number: ");
    scanf("%f", &num1);

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

    // Multiplication using function
    float result = multiply(num1, num2);

    // Display  Output
    printf("The product of %.2f and %.2f == %.2f\n", num1, num2, result);

    return 0;
}

To generate a multiplication table read.

Conclusion:

We covered the algorithm and provided pseudocode for a C program that multiply two integers in this article.

We implemented three separate programs: one that multiplied two floating-point numbers, one that multiplied two binary numbers, and one that used multiplication functions.

The guide also includes output and complexity analysis for a comprehensive understanding of the programs. These programs showcase the flexibility and versatility of C in handling different types of numbers and mathematical operations.

FAQ

How to multiply two numbers?

The ‘*’ operator in C can be used to multiply two numbers. For example, if you multiply num1 and num2, you get result = num1 * num2, which is then saved in the variable result.

What is the product of two numbers?

The product of two numbers is the result got by multiplying them together. In C programming, you can find the product of two numbers using the multiplication operator ‘*’.

How to add two numbers in C program example?

How to add two numbers in C program example?

In a C program, the ‘+’ operator can be used to add two numbers. Here’s one example.

#include <stdio.h>

int main() {
    int num1, num2, sum;

    // Input
    printf(“Enter the first number: “);
    scanf(“%d”, &num1);

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

    // Addition
    sum = num1 + num2;

    // Output
    printf(“The sum of %d and %d == %d\n”, num1, num2, sum);

    return 0;
}

What is float in C programming?

‘float’ is a data type in C code that stands for floating-point numbers. It stores a real number (one with a decimal point). float num = 3.14; creates a float variable called “num” and assigns the value 3.14 to it.

What is the divide symbol in C?

The ‘/’ sign divides things in C. For division tasks between two numbers, it is used as the division operator. If you use result = num1 / num2;, it will divide num1 by num2 and keep the result in the variable result.

Leave a Reply

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