write a c++ program for the following question \"Recursion\" An integer. Determi
ID: 3826632 • Letter: W
Question
write a c++ program for the following question
"Recursion"
An integer. Determine whether the number you entered a Palindrome (read equally both from left to right and from right to left).
Problem using recursion.
NB: the solution should be in the form as shown in an example below:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
void func(int *a, int n)
{
for (int i = 0; i < n; i++)
if (a[i] < 0) a[i] = 0;
}
int main()
{
int *a;
int n;
srand(time(NULL));
printf("n= ");
scanf("%d", &n);
a = (int*)malloc(n*sizeof(int));
for (int i = 0; i<n; i++)
a[i] = rand() % 201 -100;
for (int i = 0; i<n; i++)
printf("%3d ", a[i]);
func(a, n);
printf(" ");
for (int i = 0; i<n; i++)
printf("%3d ", a[i]);
getchar(); getchar();
return 0;
}
Explanation / Answer
#include <iostream>
#include <math.h>
using namespace std;
/* Function declarations */
int rev(int num);
int Palindrome(int num);
int main()
{
int num;
cout<<"Enter any number to check it is palindrome or not: ";
cin>>num;
if(Palindrome(num) == 1)
{
cout<<"Entered number is palindrome number. ";
}
else
{
cout<<"Entered number is NOT palindrome number. ";
}
return 0;
}
int Palindrome(int num)
{
if(num == rev(num)) //check number is equal to its reverse or not
{
return 1;
}
return 0;
}
//Recursive function to find reverse of the number
int rev(int num)
{
int value;
//Base condition
if(num==0)
return 0;
value = (int)log10(num);
return ((num%10 * pow(10, value)) + rev(num/10));
}
Output:-
Enter any number to check it is palindrome or not: 121
Entered number is palindrome number.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.