|
1 |
| -#include <iostream> |
2 |
| -using namespace std; |
| 1 | +#include <iostream> |
| 2 | +using namespace std; |
3 | 3 |
|
4 |
| -// Maximum number of digits in output |
| 4 | +// Maximum number of digits in output |
5 | 5 | // x^n where 1 <= x, n <= 10000 and overflow may happen
|
6 |
| -#define MAX 100000 |
| 6 | +#define MAX 100000 |
7 | 7 |
|
8 |
| -// This function multiplies x |
9 |
| -// with the number represented by res[]. |
10 |
| -// res_size is size of res[] or |
11 |
| -// number of digits in the number |
12 |
| -// represented by res[]. This function |
13 |
| -// uses simple school mathematics |
14 |
| -// for multiplication. |
15 |
| -// This function may value of res_size |
16 |
| -// and returns the new value of res_size |
17 |
| -int multiply(int x, int res[], int res_size) { |
| 8 | +// This function multiplies x |
| 9 | +// with the number represented by res[]. |
| 10 | +// res_size is size of res[] or |
| 11 | +// number of digits in the number |
| 12 | +// represented by res[]. This function |
| 13 | +// uses simple school mathematics |
| 14 | +// for multiplication. |
| 15 | +// This function may value of res_size |
| 16 | +// and returns the new value of res_size |
| 17 | +int multiply(int x, int res[], int res_size) { |
18 | 18 |
|
19 |
| -// Initialize carry |
20 |
| -int carry = 0; |
| 19 | + // Initialize carry |
| 20 | + int carry = 0; |
21 | 21 |
|
22 |
| -// One by one multiply n with |
23 |
| -// individual digits of res[] |
24 |
| -for (int i = 0; i < res_size; i++) { |
25 |
| - int prod = res[i] * x + carry; |
| 22 | + // One by one multiply n with |
| 23 | + // individual digits of res[] |
| 24 | + for (int i = 0; i < res_size; i++) { |
| 25 | + int prod = res[i] * x + carry; |
26 | 26 |
|
27 |
| - // Store last digit of |
28 |
| - // 'prod' in res[] |
29 |
| - res[i] = prod % 10; |
| 27 | + // Store last digit of |
| 28 | + // 'prod' in res[] |
| 29 | + res[i] = prod % 10; |
30 | 30 |
|
31 |
| - // Put rest in carry |
32 |
| - carry = prod / 10; |
33 |
| -} |
| 31 | + // Put rest in carry |
| 32 | + carry = prod / 10; |
| 33 | + } |
34 | 34 |
|
35 |
| -// Put carry in res and |
36 |
| -// increase result size |
37 |
| -while (carry) { |
38 |
| - res[res_size] = carry % 10; |
39 |
| - carry = carry / 10; |
40 |
| - res_size++; |
41 |
| -} |
42 |
| -return res_size; |
43 |
| -} |
| 35 | + // Put carry in res and |
| 36 | + // increase result size |
| 37 | + while (carry) { |
| 38 | + res[res_size] = carry % 10; |
| 39 | + carry = carry / 10; |
| 40 | + res_size++; |
| 41 | + } |
| 42 | + return res_size; |
| 43 | +} |
44 | 44 |
|
45 |
| -// This function finds |
46 |
| -// power of a number x |
47 |
| -void power(int x, int n) |
48 |
| -{ |
| 45 | +// This function finds |
| 46 | +// power of a number x |
| 47 | +void power(int x, int n) |
| 48 | +{ |
49 | 49 |
|
50 |
| -//printing value "1" for power = 0 |
51 |
| -if(n == 0 ){ |
52 |
| - cout<<"1"; |
53 |
| - return; |
54 |
| -} |
| 50 | + //printing value "1" for power = 0 |
| 51 | + if(n == 0 ){ |
| 52 | + cout<<"1"; |
| 53 | + return; |
| 54 | + } |
55 | 55 |
|
56 | 56 |
|
57 |
| -int res[MAX]; |
58 |
| -int res_size = 0; |
59 |
| -int temp = x; |
| 57 | + int res[MAX]; |
| 58 | + int res_size = 0; |
| 59 | + int temp = x; |
60 | 60 |
|
61 |
| -// Initialize result |
62 |
| -while (temp != 0) { |
63 |
| - res[res_size++] = temp % 10; |
64 |
| - temp = temp / 10; |
65 |
| -} |
| 61 | + // Initialize result |
| 62 | + while (temp != 0) { |
| 63 | + res[res_size++] = temp % 10; |
| 64 | + temp = temp / 10; |
| 65 | + } |
66 | 66 |
|
67 |
| -// Multiply x n times |
68 |
| -// (x^n = x*x*x....n times) |
69 |
| -for (int i = 2; i <= n; i++) |
70 |
| - res_size = multiply(x, res, res_size); |
| 67 | + // Multiply x n times |
| 68 | + // (x^n = x*x*x....n times) |
| 69 | + for (int i = 2; i <= n; i++) |
| 70 | + res_size = multiply(x, res, res_size); |
71 | 71 |
|
72 |
| -cout << x << "^" << n << " = "; |
73 |
| -for (int i = res_size - 1; i >= 0; i--) |
74 |
| - cout << res[i]; |
75 |
| -} |
| 72 | + cout << x << "^" << n << " = "; |
| 73 | + for (int i = res_size - 1; i >= 0; i--) |
| 74 | + cout << res[i]; |
| 75 | +} |
76 | 76 |
|
77 |
| -// Driver program |
78 |
| -int main() { |
79 |
| -int exponent, base; |
80 |
| -printf("Enter base "); |
81 |
| -scanf("%id \n", &base); |
82 |
| -printf("Enter exponent "); |
83 |
| -scanf("%id", &exponent); |
84 |
| -power(base, exponent); |
85 |
| -return 0; |
86 |
| -} |
| 77 | +// Driver program |
| 78 | +int main() { |
| 79 | + int exponent, base; |
| 80 | + printf("Enter base "); |
| 81 | + scanf("%id \n", &base); |
| 82 | + printf("Enter exponent "); |
| 83 | + scanf("%id", &exponent); |
| 84 | + power(base, exponent); |
| 85 | + return 0; |
| 86 | +} |
0 commit comments