check whether a given number is palindrome number or not

Thread Starter

Parth786

Joined Jun 19, 2017
642
Hi
I'm looking for a way to determine whether a number is palindromic or not. I can say by looking whether a number is palindromic or not.

These are palindromic : 101, 303,13031, 1230321
These are not palindromic : 102, 302, 13032,3213341

I can tell by looking given number whether a number is palindromic or not. But I do not understand what logic works behind it even I'm not able to make Flowchart. How to check check whether a given number is palindrome number or not

Note : My focus is not on the program right now. I want to understand basic logic's of palindromic number
 

WBahn

Joined Mar 31, 2012
30,045
There are several ways. Part of it depends how the number is represented. If it is represented as a string, then you just do what you do by eye -- walk in from both sides and see if the digits match until you get to the middle. If it is represented as an integer in program, then you need to get at the individual digits and then you can do the same thing. There are several ways of addressing that smaller problem.

Another approach is to us a stack data structure and push the digits on one at a time. Then pop it back off, which give you access to the digits in reverse order. Again, from there there are several approaches.

So try solving simpler problems to build up the tools you need to solve the bigger problem.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
So try solving simpler problems to build up the tools you need to solve the bigger problem.
If user enter any number then I want to find out whether a number is palindromic or not. Suppose user entered 12121 How to know it with help flow chart?
 

Picbuster

Joined Dec 2, 2013
1,047
As WBahn stated; read the first compare with the last do the same for second and last-1 and so on until reached 1/2 string(array in C) length.
if they not the same no palindromic string.
Remember a single character is also palindromic.
String/array size produce an odd or even number.
This must be enough input to draw a flow chart.

Picbuster
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
This must be enough input to draw a flow chart.
Picbuster
I understand that the first I have to declare variable's and I have to write print statement that will print the message and after that the I have to write scanf statement that will take number from user after that I have to check number go for further step if number is not zero

Code:
#include <stdio.h>

int main(void)
{
    int Number;

    printf("Enter an integer: ");

    scanf("%d", &Number);

    while( Number != 0 )
    {
       // code//
    }
}
 

spinnaker

Joined Oct 29, 2009
7,830
Wow this brings back memories, I think it was assignment #1 or 2 in my computer science class about a million years ago.

A simple google search is all that is needed.

https://www.google.com/search?clien...1j0i131i46k1j46i131k1j0i10i67k1.0.cR9a__SyoNo

This is a typical assignment in many programming classes. There are probably thousands of internet articles that discuss this problem.

Part of being an engineering is learning to use the tools that are right in front of you. Internet search is one of those tools.
 

WBahn

Joined Mar 31, 2012
30,045
I understand that the first I have to declare variable's and I have to write print statement that will print the message and after that the I have to write scanf statement that will take number from user after that I have to check number go for further step if number is not zero

Code:
#include <stdio.h>

int main(void)
{
    int Number;

    printf("Enter an integer: ");

    scanf("%d", &Number);

    while( Number != 0 )
    {
       // code//
    }
}
Again, break it down into simpler problems.

Let's say that I gave you the following function:

int getDigit(unsigned n, unsigned k);

This function takes an unsigned integer and returns the value of the kth digit from the right with the 1's digit being digit 0.

So getDigit(12345, 0) returns 5 while getDigit(12345, 4) returns 1.

If there is no kth digit, then the function returns -1. So getDigit(12345, 5) returns -1.

With this function available to you, can you develop a flowchart to solve the palindrome problem?
 

spinnaker

Joined Oct 29, 2009
7,830
I am sorry but really I didn't understand. I understand there is function where you are passing two value n and k.

This isn't rocket science. It is so simple.
1. Turn the number into a string (character array).

2. get length of string.

3. Create two counters. One set to zero. One set to length of string -1.

4. Have a loop for length of string.

5. Compare character at buffer[startcounter] to buffer[endcounter].

6. If characters are not equal then abort loop.

7. Characters equal increment startcounter decrement endcounter and continue loop till length of string.


Remember that you need to account for the fact that C arrays start at zero.
 

WBahn

Joined Mar 31, 2012
30,045
I am sorry but really I didn't understand. I understand there is function where you are passing two value n and k.
It is very simple. You are given a function that returns the value of a specific digit in a number. If your number is 637485 and you want to know the value of the digit in the hundreds place, then you want to know the value of the digit in place #2 because the units digit is place #0, the tens digit is in place #1, and the hundreds digit is in place #2.

So if you have

int number = 637485;
int digitValue;
int digitPlace = 2;

and then you execute

digitValue = getDigit(number, digitPlace);

and then

printf("In the number %i, the value of the digit in place #%i is %i.\n");

it will print

In the number 637485, the value of the digit in place #2 is 4.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
It is very simple. .
This is not mine But I understand this program a little bit. Can you help me with this example
Code:
#include <stdio.h>
int main(void)
{
    int n, reversedInteger  =  0,  remainder,  originalInteger;

    printf("Enter an integer: ");

    scanf("%d",  &n);

    originalInteger  =  n;

    // reversed integer is stored in variable

    while( n != 0 )
    {
        remainder  =  n%10;

        reversedInteger  =  reversedInteger*10 + remainder;

        n  /=  10;
    }

    // palindrome if orignalInteger and reversedInteger are equal

    if (originalInteger  ==  reversedInteger)

        printf("%d is a palindrome.",  originalInteger);

    else

        printf("%d is not a palindrome.", originalInteger);
  
    return 0;
}
 

WBahn

Joined Mar 31, 2012
30,045
How can I help you when I have no idea what it is about this program you don't understand?

Take out a piece of paper and walk through the program line by line performing the operations by hand. Pick a number such as 456 to work with the first time and then pick 828 for the second time.

You should be able to see what is happening and how it helps answer the question of whether it is a palindrome.
 

WBahn

Joined Mar 31, 2012
30,045
Interesting way to do this problem. Not real obvious at first. I would have converted to a string. a lot more straight forward.
When I was teaching CS-1, this is the way I showed them to do it, but the goal wasn't to have them find palindromes, it was to have them work with number fundamentals leading into number representation. The palindrome problem was just a vehicle.
 

John_2016

Joined Nov 23, 2016
55
Hi Parth786

in MATLAB

let n be the input number

n=10701

then the following returns 1 only when n is palindrome

nstr=num2str(n);
prod(nstr([1:1:floor(numel(nstr/2))])==nstr([floor(numel(nstr/2)):-1:1]))


as function, in file is_palindrome.m and in same folder as calling script:

function ret=is_palindrome(n)
nstr=num2str(n);
ret=prod(nstr([1:1:floor(numel(nstr/2))])==nstr([floor(numel(nstr/2)):-1:1]))
end


John BG

MOD EDIT: Removed e-mail address.
 
Last edited by a moderator:

MrAl

Joined Jun 17, 2014
11,461
Hi,

It's also interesting that in string form, if we reverse the string and then subtract each digit from each other and all results are zero, it passes the 'test'.

There are some catches here though. One is if the user enters a number that the system has trouble handling. If the number is too large for example it may truncate the digits before it gets turned into a string.
Probably best to have the user enter it as a string rather than an actual number.
 
Top