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