Indent code

This commit is contained in:
Ashwek Swamy 2019-02-13 19:28:19 +05:30 committed by GitHub
parent 68c93d8efb
commit 62ff5e5d61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,12 +16,12 @@ using namespace std;
// and returns the new value of res_size // and returns the new value of res_size
int multiply(int x, int res[], int res_size) { int multiply(int x, int res[], int res_size) {
// Initialize carry // Initialize carry
int carry = 0; int carry = 0;
// One by one multiply n with // One by one multiply n with
// individual digits of res[] // individual digits of res[]
for (int i = 0; i < res_size; i++) { for (int i = 0; i < res_size; i++) {
int prod = res[i] * x + carry; int prod = res[i] * x + carry;
// Store last digit of // Store last digit of
@ -30,16 +30,16 @@ for (int i = 0; i < res_size; i++) {
// Put rest in carry // Put rest in carry
carry = prod / 10; carry = prod / 10;
} }
// Put carry in res and // Put carry in res and
// increase result size // increase result size
while (carry) { while (carry) {
res[res_size] = carry % 10; res[res_size] = carry % 10;
carry = carry / 10; carry = carry / 10;
res_size++; res_size++;
} }
return res_size; return res_size;
} }
// This function finds // This function finds
@ -47,40 +47,40 @@ return res_size;
void power(int x, int n) void power(int x, int n)
{ {
//printing value "1" for power = 0 //printing value "1" for power = 0
if(n == 0 ){ if(n == 0 ){
cout<<"1"; cout<<"1";
return; return;
} }
int res[MAX]; int res[MAX];
int res_size = 0; int res_size = 0;
int temp = x; int temp = x;
// Initialize result // Initialize result
while (temp != 0) { while (temp != 0) {
res[res_size++] = temp % 10; res[res_size++] = temp % 10;
temp = temp / 10; temp = temp / 10;
} }
// Multiply x n times // Multiply x n times
// (x^n = x*x*x....n times) // (x^n = x*x*x....n times)
for (int i = 2; i <= n; i++) for (int i = 2; i <= n; i++)
res_size = multiply(x, res, res_size); res_size = multiply(x, res, res_size);
cout << x << "^" << n << " = "; cout << x << "^" << n << " = ";
for (int i = res_size - 1; i >= 0; i--) for (int i = res_size - 1; i >= 0; i--)
cout << res[i]; cout << res[i];
} }
// Driver program // Driver program
int main() { int main() {
int exponent, base; int exponent, base;
printf("Enter base "); printf("Enter base ");
scanf("%id \n", &base); scanf("%id \n", &base);
printf("Enter exponent "); printf("Enter exponent ");
scanf("%id", &exponent); scanf("%id", &exponent);
power(base, exponent); power(base, exponent);
return 0; return 0;
} }