Question

In: Computer Science

A palindromic number is a number that remains the same when its digits are reversed. For...

A palindromic number is a number that remains the same when its digits are reversed. For examples, 1, 11, 99, 121 are palindromic. Write a program that takes one array of 10 positive integers as input and output all the palindromic numbers in this array. We assume that each input number is valid, i.e., a positive integer.

Hint: You can consider the following steps to check whether a number (e.g., 121) is palindromic • Store this number to one variable, e.g., temp = 121. • Calculate the value of the number when all the digits in temp are reversed, e.g., extracting each digit in temp from the right-hand side to left-hand side, and calculating the value at the same time. • Check whether the original value equals to this calculated value.

write it in c++

Solutions

Expert Solution

Answer

here we can use 10 elements of integer array for perform the task, Here we take each value in the array and stored into a temporary variable then we can reverse that integer in best method. finally reversed integer matched with corresponding array value, if it is true then it is a palidrom. so here is the code for above problem.


// C++ code for finding palidromes in a given array of elements;
#include<bits/stdc++.h> 
using namespace std;  
int main() 
{ 
    //sample input array. which contain 10 elements.
    int arr[] = {121,56,434,1,2,121,10,12,131,178}; 
    //set n as total number of elements.
    int n = 10;
    //temp is used to store temporary value of each item in array
    int temp;
    // d hold the reminder at the time of loop.
    int d;
    // rev holds the reversed value of integer.
    int rev;
    for (int i = 0; i <n; i++)  
    { 
         temp=arr[i];
         rev = 0; 
        //very important part in this, reverse an integer.
         while (temp > 0)  
         { 
                d = temp % 10; 
                rev = rev * 10 + d; 
                temp = temp / 10; 
                
         } 
         // checking the reversed integer is same as the array value. then it is palidrome.
         if(arr[i]==rev)
         {
                 cout<<rev<<"\n";
         }
    }
    return 0; 
} 

output

You can change the values of the array as you need. So here the porgram pop out the palidromes only. Any doubt please comment

Thanks in advance


Related Solutions

In a two digit number the sum of the digits is 9. Also, when 27 is subtracted from the number the digits are reversed. Find the number?
In a two digit number the sum of the digits is 9. Also, when 27 is subtracted from the number the digits are reversed. Find the number?
Determine a two digit number whose value is equal to eight times the sum of its digits and when 45 is subracted from the number, the digits are reversed?
Determine a two digit number whose value is equal to eight times the sum of its digits and when 45 is subracted from the number, the digits are reversed?
A Palindromic number is one that reads the same backwards and forwards. Write a MATLAB function...
A Palindromic number is one that reads the same backwards and forwards. Write a MATLAB function (call it palin.m) that takes as input a positive integer, and returns 1 (true) if it is palindromic, 0 (false) if it is not.  
Find a three digit integer in base five that has the order of its digits reversed...
Find a three digit integer in base five that has the order of its digits reversed when multiplied by 2.
19. Under sum-of-the-years’-digits depreciation . . . a. the book value remains the same each year....
19. Under sum-of-the-years’-digits depreciation . . . a. the book value remains the same each year. b. the depreciation rate changes each year. c. the denominator of the SYD fraction changes each year. d. all of the above. 20. For assets acquired during the year, the sum-of-the-years’-digits method requires that the same depreciation rate be used . . . a. for the remaining months of the year of acquisition, then again in the final year of the asset’s estimated life...
A number is a palindromic prime if it is a prime number as well as a...
A number is a palindromic prime if it is a prime number as well as a palindromic number (ie. it is the same number when the digits are reversed). For example, 10301 is a palindromic prime. Write a Python program to ask the user how many palindromic primes they would like to compute, and output the values with a maximum of 10 values per line. Your program should include the following functions: isPrime(number) - returns True or False isPalindrome(number) -...
(Programming Language: Python) It's trivial that the value of a number remains the same no matter...
(Programming Language: Python) It's trivial that the value of a number remains the same no matter how many zeros precede it. However, adding the zeros at the end of the number increases the value * 10. Jasmine is tired of seeing \001/100" on her tests (well yes, no one really writes 001, but Jasmine's teacher finds it funny to do so). So, she managed to login to her teacher's computer and now wants to design a function that can move...
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from...
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from the product of two 2-digit numbers is 9,009 = 91 × 99. The largest palindrome made from the product of two 3-digit numbers is 906,609 = 913 × 993. The largest palindrome made from the product of two 4-digit numbers is 99,000,099 = 9,901 × 9,999. 1. Write a function IN JAVASCRIPT to find the largest palindrome made from the product of two 7-digit...
When the price of gasoline rises by 40 percent, but your salary remains the same, it...
When the price of gasoline rises by 40 percent, but your salary remains the same, it is an example of a decline in your: standard of living purchasing power GDP index cost of living index gross economic income
An Armstrong number is a number that is the sum of its own digits, each raised...
An Armstrong number is a number that is the sum of its own digits, each raised to the power of the number of its digits. For example, 153 is considered an Armstrong number because 13 + 53 + 33 = 153. Write a VBA program that lists all 3-digit Armstrong numbers within a specified range of 3-digit positive integers. Your sub should do the following: 1) Using two input boxes, ask the user to input two 3-digit positive integers (a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT