Question

In: Computer Science

Question 4: Set P to the largest digit in your 5 digit number. For example, if...

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

Solutions

Expert Solution

#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;
}


Related Solutions

The number 52430 is a 5-digit number whereas 03201 is not a 5-digit number. Determine the...
The number 52430 is a 5-digit number whereas 03201 is not a 5-digit number. Determine the number of 5-digit multiples of 5 that can be created using the digits 0 to 9, if digits may not be repeated
Program 5: The concept of a 5-digit palindromenumber is a 5-digit number that reads the...
Program 5: The concept of a 5-digit palindrome number is a 5-digit number that reads the same from left to right and from right to left. For example, 12121, 45454, and 14741 are valid 5-digit palindrome numbers. Design (pseudocode) and implement (source code) a program (name it FiveDigitPalindrom) that reads a 5-digit number from the user (as integer value, not string) and then mathematically (using division and remainder operations) determines whether the entered number is a 5-digit palindrome or not....
The first significant digit in any number must be​ 1, 2,​ 3, 4,​ 5, 6,​ 7,...
The first significant digit in any number must be​ 1, 2,​ 3, 4,​ 5, 6,​ 7, 8, or 9. It was discovered that first digits do not occur with equal frequency. Probabilities of occurrence to the first digit in a number are shown in the accompanying table. The probability distribution is now known as​ Benford's Law. For​ example, the following distribution represents the first digits in 226 allegedly fraudulent checks written to a bogus company by an employee attempting to...
Randomly select a 7 digit natural number. a) Find the probability that the number has 4...
Randomly select a 7 digit natural number. a) Find the probability that the number has 4 odd digits, 3 even numbers, and no number is repeated. b) Find the probability that the number 2 is present exactly 2 times, the number 3 is present exactly 3 times, the remaining digits are present no more than 1 time.
1. Given a randomly chosen 4-digit number... What is the probability... a. That there are no...
1. Given a randomly chosen 4-digit number... What is the probability... a. That there are no pairs of consecutive digits in the number b. That there are no pairs of consecutive digits in the number, if the last digit and first digit of the number could be classified as consecutive c. Given a randomly chosen six digit number: What is the probability that the number is an odd number if it contains the six digits 7-6-4-4-3-1, and is in a...
A three-digit number is formed from nine numbers (1, 2, 3, 4, 5, 6, 7, 8...
A three-digit number is formed from nine numbers (1, 2, 3, 4, 5, 6, 7, 8 & 9). No number can be repeated. How many different three-digit numbers are possible if 1 and 2 will not be chosen together? Select one: a. 462 b. 336 c. 672 d. 210
1. A three-digit number is formed from nine numbers (1, 2, 3, 4, 5, 6, 7,...
1. A three-digit number is formed from nine numbers (1, 2, 3, 4, 5, 6, 7, 8 & 9). No number can be repeated. How many different three-digit numbers are possible if 1 and 2 will not be chosen together? Select one: A. 672 B. 210 C. 462 D. 336 2. In a recent survey conducted by a professor of UM, 200 students were asked whether or not they have a satisfying experience with the e-learning approach adopted by the...
A three-digit number is formed from nine numbers (1, 2, 3, 4, 5, 6, 7, 8...
A three-digit number is formed from nine numbers (1, 2, 3, 4, 5, 6, 7, 8 & 9). No number can be repeated. How many different three-digit numbers are possible if 1 and 2 will not be chosen together? a. 462 b. 210 c. 672 d. 336
Four numbers are selected without replacement from the set of 1,2,3,4,5,6,7 to form a 4 digit...
Four numbers are selected without replacement from the set of 1,2,3,4,5,6,7 to form a 4 digit number. What is the probability that the number is greater than 5432?
Write a program that does the following: Prompts the user for a 5-digit number. Divides that...
Write a program that does the following: Prompts the user for a 5-digit number. Divides that number into its separate five digits. Adds up the sum of those five digits. Outputs that sum You should test your program with at least the following test cases. (I am to be programming with python) >>> RESTART: /Users/mamp/Desktop/Desktop - Enter a 5-digit number: 12345 15 >>> RESTART: /Users/mamp/Desktop/Desktop - Enter a 5-digit number: 00000 0 >>> RESTART: /Users/mamp/Desktop/Desktop - Enter a 5-digit number:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT