5. Random Palindrome: Write a function that takes integer N as a parameter. This
ID: 3729211 • Letter: 5
Question
5. Random Palindrome: Write a function that takes integer N as a parameter. This function then generates N random digits (numbers between 0 and 9) using the computer time as the seed of the random number generator. The probability that a generated digit is even is 80% whereas the probability that a generated digit is odd is 20%. The function then concatenates the N randomly created digits to form one number. This function then checks if this number is a palindrome or not. (Hint: find the reverse of that number) Finally, the function prints out the number and says whether it is a palindrome or not. Examplel: Please enter the length of a number: 6 The concatenation of the 6 randomly generated numbers is: 258852 The number is a palindrome! Example2: Please enter the length of a number: 5 The concatenation of the 5 randomly generated numbers is: 86824 The number is a NOT a palindrome!Explanation / Answer
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void program()
{
srand(time(0));
int n,num=0,val;
cout<<"Please enter the length of a number: ";
cin>>n;
for(int i = 0; i<n; i++){
val=rand()%10;
num= num*10+ val;
}
cout<<"The concatenation of the "<<n<<" randomly generated number is :"<<num<<endl;
int digit,rev,temp=num;
while (num != 0)
{
digit=num%10;
rev=(rev*10)+digit;
num=num/10;
}
if (n == rev)
cout<<"The number is a palindrome!";
else
cout<<"The number is NOT a palindrome!";
}
int main()
{
program();
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.