1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
// Programmieren 1, Praktikum 3, Aufgabe 5
// Sven Eisenhauer
// 17.11.2004
//
// file: main.cpp
//
// purpose: egypt multiplication
//
//
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int multIterative(int,int);
void multRecursive(int,int, int &);
int main()
{
int number1,
number2,
erg;
cout << "1st number: ";
cin >> number1;
cout << "2nd number: ";
cin >> number2;
cout << "Product: " << multIterative(number1,number2) << endl;
multRecursive(number1,number2,erg);
cout << "Product: " << erg << endl;
return 0;
}
int multIterative(int a,int b)
{
int product=0;
while (b>0)
{
if (0 != b%2)
product+=a;
a = a << 1;
b = b >> 1;
}
return product;
}
void multRecursive(int a,int b, int &product)
{
int x,y;
if (b<=1)
product=a;
else
{
x=a<<1;
y=b>>1;
multRecursive(x,y,product);
if (0!=b%2)
product+=a;
}
}
|