Multiply without using multiplication operator in C and C++
C Program to multiply positive integers x and y by using addition and subtraction operators only. Means multiply without using multiplication operator. Let’s start.
Algorithm:
- Take the input from the user and store them in x and y.
- Now create a for loop, That will loop from 1 to the y.
- For each iteration, we are going to add x to mul. Mul is variable which is initially 0 and we are adding x to mul. Means we are multiplying the value.
- This process will repeat until we reach the y. Once we reach, Loop will break and Multiplication value is stored in variable mul
- Display the mul value on the console.
Program :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main(void) { int x, y, i, mul=0; printf("Enter x and y values : "); scanf("%d%d",&x,&y); for(i=1;i<=y;i++) { mul = mul + x; } printf("Multiplication is : %d \n",mul); return 0; } |
Output:
It will not work if we enter x greater n y smaller