Multiply and divide in C programming

In this C programming article we will learn how to multiply and divide using C programming language. Multiplication and division can done in the same way we have done addition and subtraction in our previous article.

Let’s see some program to learn about how we can multiply and divide in C programming.

General multiplication program in C

#include <stdio.h>
int main(){
  int a, b, mul;
  printf("Enter first number : ");
  scanf("%d", &a);
  printf("Enter second number : ");
  scanf("%d", &b);

  mul = a * b;
  printf("\nMultiplication result is = %d\n", mul);
  return 0;
}

Output of this multiplication program:

Multiply using loop in C

We will use the concept of addition for multiply any integer by another integer.

We know that,  3 * 4 = 12.

Again ( 4 + 4 + 4 ) = 12  ( 3 times 4 )

Or ( 3 + 3 + 3 + 3 ) = 12  ( 4 times 3)

So, if we add the first integer for second integer’s times or add the second integer for first integer’s times then we will get the result of multiplication.

// multiply using loop, multiply and divide by C
#include <stdio.h>
int main(){
  int a, b, mul = 0;
  printf("Enter first number : ");
  scanf("%d", &a);
  printf("Enter second number : ");
  scanf("%d", &b);

  for(int i = 1; i <= a; i++){
    mul+=b;    // this means mul = mul + b
  }
  printf("\nResult is = %d\n", mul);
return 0;
}

Output of this multiply program:

multiplication program in c, division program

Division program in C

#include <stdio.h>
int main(){
  int p, q;
  printf("Enter first number here : ");
  scanf("%d", &p);
  printf("Enter second number here : ");
  scanf("%d", &q);

  float divide = (float)p / q;    // making the integer p to float
  printf("%.3f\n", divide);
return 0;
}

Output of division program:

division program in c, multiplication program

Result and reminder of division program in C

#include <stdio.h>
int main(){
  int firstNumber, secondNumber, divide, reminder;
  printf("Enter first number : ");
  scanf("%d", &firstNumber);
  printf("Enter second number : ");
  scanf("%d", &secondNumber);

  divide = firstNumber / secondNumber;
  reminder = firstNumber % secondNumber;

  printf("\nResult is = %d\n", divide);
  printf("\nReminder is = %d\n", reminder);
return 0;
}

Output of this program:

multiplication and division program in c

Previous pageOdd and even program

Next pageCheck vowel or constant