My problem consists of 3 functions that output yes or no if a number is a palind
ID: 3720520 • Letter: M
Question
My problem consists of 3 functions that output yes or no if a number is a palindrome, near prime, or a nice number. The direction below apply the fucntions to a score which I am unable to do.
You will be given a list of numbers. Each number is assigned a score. Its score is determined as follows:
A number’s base score is the value of the number itself.If the number is a palindrome it scores is doubled.
If the number is a prime its score is doubled.
If the number is a nice number its score is tripled.
Consider how to determine the score of a number 89. It is not a palindrome and but it is nice and near prime:
Base score: 89
Score after palindrome check: 89 (remains same as not a palindrome) Score after prime check: 188 (doubled as 89 is prime)
Score after nice check: 534 (tripled as 89 is nice)
Consider the number 101:
It is a palindrome, it is prime but it is not a nice number:
Base score: 101
Score after palindrome check: 202 (doubled as it’s a palindrome)Score after prime check: 404 (doubled as 101 a prime)
Score after nice check: 404 (not tripled as 101 is not nice)
Given a list of numbers entered by the user output the list of numbers in sorted order (ascending) of their scores. Also include the score achieved with each number separated by a comma after the number.
For example if the user enters 89
101
You have to output
101,404 89, 534
Explanation / Answer
PLEASE REFER BELOW CODE
#include<iostream>
using namespace std;
//checking given number is prime
bool isprime(int num)
{
bool flag = true;
for(int i = 2; i <= num / 2; i++)
{
if(num % i == 0)
{
flag = false;
break;
}
}
return flag;
}
//checking given number is prime
bool ispal(int num)
{
int n,d,rev=0;
n=num;
do
{
d=n%10;
rev=(rev*10)+d;
n=n/10;
}while(n!=0);
if(num==rev)
return true;
return false;
}
//check given number is nice
//I dont have code as you have mentioned you have that code. Just paste that code here
bool isnice(int num)
{
bool flag = false;
return flag;
}
int main()
{
int num_arr[10] = {0}; //user input
int base_score[20] = {0}; //for final output
int temp;
cout<<"Enter 0 to terminate "<<endl;
int index = 0;
//taking user input I am assuming user enters upto 10 elements or 0 to terminate code
while(1)
{
cout<<"Enter Number : ";
cin>>temp;
if(temp == 0)
break;
num_arr[index] = temp;
index++;
}
for(int i = 0; i < index; i++)
{
base_score[2*i] = num_arr[i]; //copying original array element to base score
if(ispal(num_arr[i])) //if number is palindrone then double base score
base_score[(2*i) + 1] += (2 * num_arr[i]);
if(isprime(num_arr[i])) //if number is prime then double base score
base_score[(2*i) + 1] += (2 * num_arr[i]);
if(isnice(num_arr[i])) //if number is nice then tripled base score
base_score[(2*i) + 1] += (3 * num_arr[i]);
}
//printing base score array
for(int i = 0; i < (2*index); i++)
{
cout<<base_score[i]<<",";
}
return 0;
}
CURRENTLY I DONT HAVE CODE TO CHECK NUMBER IS NICE. JUST PASTE THAT CODE AND RUN.
PLEASE LET ME KNOW IF YOU FACE ANY DIFFICULTY IN COMMENT SECTION.
I COMPILED CODE SUCCESSFULLY AND IT'S RUNNING FINE
Enter 0 to terminate
Enter Number : 101
Enter Number : 89
Enter Number : 0
101,404,89,178,
Process returned 0 (0x0) execution time : 4.857 s
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.