In: Computer Science
Question 4: Set P to the largest digit in your 5 digit number. For example, if your 5 digit number is 88412, then P should be set to 8. Calculate and print the value of (P+1) multiplied by itself (P+3) times. As an example, if your 5 digit number is 88412, you should print the result of multiplying 9 with itself 12 times -- this is also known as 9 to the power of 12. HINT: you are free to re-arrange the code any way you see fit HINT: you are free to add more variables if needed. HINT: you are free to use known math functions HINT: alternatively, you are free to use for loops write a c++ code
#include <iostream>
using namespace std;
// Maximum number of digits in
// output
#define MAX 100000
// This function multiplies x
// with the number represented by res[].
// res_size is size of res[] or
// number of digits in the number
// represented by res[]. This function
// uses simple school mathematics
// for multiplication.
// This function may value of res_size
// and returns the new value of res_size
int multiply(int x, int res[], int res_size) {
// Initialize carry
int carry = 0;
// One by one multiply n with
// individual digits of res[]
for (int i = 0; i < res_size; i++) {
int prod = res[i] * x + carry;
// Store last digit of
// 'prod' in res[]
res[i] = prod % 10;
// Put rest in carry
carry = prod / 10;
}
// Put carry in res and
// increase result size
while (carry) {
res[res_size] = carry % 10;
carry = carry / 10;
res_size++;
}
return res_size;
}
// This function finds
// power of a number x
void power(int x, int n)
{
//printing value "1" for power = 0
if (n == 0) {
cout << "1";
return;
}
int res[MAX];
int res_size = 0;
int temp = x;
// Initialize result
while (temp != 0) {
res[res_size++] = temp % 10;
temp = temp / 10;
}
// Multiply x n times
// (x^n = x*x*x....n times)
for (int i = 2; i <= n; i++)
res_size = multiply(x, res,
res_size);
cout << x << "^" << n << "
= ";
for (int i = res_size - 1; i >= 0; i--)
cout << res[i];
}
int findP(int num) {
int max = -1;
int mod;
while (num != 0) {
mod = num % 10;
num = num / 10;
if (max < mod) max = mod;
}
return max;
}
// Driver program
int main() {
int exponent = 100;
int base = 20;
cout << "Enter 5 digit number:
"<<endl;
int num;
cin >> num;
int p = findP(num);
//cout << "Maximum :" << p << endl;
power(p+1, p+3);
return 0;
}