Sunday, 31 January 2016

Write a C program that calculates product of two numbers using repeated addition i.e. without using the * operator.

Leave a Comment
Solution:- 

     #include <iostream>
#include <conio>

int main(
{
   int x, y;
   cin >> x >> y;  // Let x=A, y=B
   int p = 0;// Invariant: A*B = p + x*y

 while (y > 0) 
{
   p = p + x; 
   y = y – 1;
}

cout << p << endl;
}
getche();

If You Enjoyed This, Take 5 Seconds To Share It

0 Questions: