Given an integer, perhaps a long integer, calculate its score as described below
ID: 3801166 • Letter: G
Question
Given an integer, perhaps a long integer, calculate its score as described below. If the integer input happens to be prime then the largest digit in the number will be added to the score otherwise the smallest digit will be subtracted from the score to produce the final result. Example of calculating the score for integer 5328. 1. 5^1 + 3^2 + 2^3 + 8^4 = 4118 2. And since 5328 is NOT PRIME the final result is 4118 - 2 = 4116. Example Execution #1 (see above): Enter your number: 5328 The final result of the input 5328 is: 4116 Example Execution #2 (score is 2620, add 7 because 3067 is prime): Enter your number: 3067 The final result of the input 3067 is: 2627 Example Execution #3: Enter your number: 345634591 The final result of the input 345634591 is: 43130635Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
//#include<math.h>
using namespace std;
//calculating power(number,exponential)
int raiseToPow(int x, int power)
{
int result=1;
for (int i=1; i<=power;i++)
{
result = result*x;
}
return(result);
}
//finding largest digit in given number
int largestDigit(unsigned long long int n)
{
int digit,large=0;//variables
while(n>0)
{
digit=n%10;//finding digit
if(digit>large)//checking condition for large
large=digit;
n=n/10;//dividing number by 10
}
return large;
}
//finding smallest digit in given number
int smallestDigit(unsigned long long int n)
{
int digit,small = 9; //variables
while(n > 0)
{
digit = n % 10; //finding digit
if(digit == 0)
{
small=0; //assigning 0 to small
}
if(digit <= small)
{
small = digit; //assigning digit to small
}
n = n/10; //dividing number by 10
}
return small;
}
//check whether number is prime or not
int checkPrimeNumber(unsigned long long int n)
{
bool flag = false;
for(int i = 2; i <= n/2; ++i)
{
if(n%i == 0)
{
flag = true;
break;
}
}
return flag;
}
/*calculating final result as mentioned in the question.
exmaple : for number 5328
5^1 + 3^2 + 2^3 + 8^4 =4118
And since 5328 in not prime the final result is 4118 -2 = 4116
*/
unsigned long long int calculateScore(unsigned long long int num)
{
unsigned long long int sum = 0,tempNum=num;
int totalDigits =0, temp;
while (tempNum > 0)
{
tempNum = tempNum /10;
totalDigits++;
}
while (totalDigits > 0)
{
temp = num % 10;
sum = sum + (raiseToPow(temp, totalDigits)) ;
num = num / 10;
totalDigits --;
}
return sum ;
}
int main()
{
unsigned long long int number, score, finalResult;
cout << "enter a number: " ;
cin >> number;
score = calculateScore(number);
if(checkPrimeNumber(number) == 0)
{
cout << number << " is a prime number. " ;
finalResult = score + largestDigit(number);
cout << "Calculated result is : " << finalResult;
}
else
{
cout << " is not a prime number. "<< number ;
finalResult = score - smallestDigit(number);
cout << "Calculated result is : " << finalResult;
}
system("pause >nul");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.