Thread: seperating numbers

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1

    Unhappy seperating numbers

    I am to determine if a number is a palindrome.

    How do i go about seperating a 7 digit number to see if it is a palindrome.

    I can only use basic if/else statements and other basic knowledge.

    How do i seperate a 7 digit number into single digits to compare?
    Last edited by fireguy; 09-12-2002 at 11:00 PM.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    1) find out the number of digits your number has.
    2) seperate your number into all the digits
    3) check each number with its opposite
    4) if every number matches a pattern, say so

    this is fairly easy to implement.

  3. #3
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    Have a search around for how to use modulus. It will show you how to split the number up.
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Its easier to turn the number into a char array....

    Code:
    #include <iostream>
    #include <cstdio>
    using std::cout;
    using std::endl;
    
    bool isPal(int y){	
    	int a,b,c;
    	char buff[20];	
    
    	b = sprintf(buff,"%d",y);//get char array
    	c = b / 2;//times to loop. If odd, then carry is lost
    
    	for(a = 0,--b;c;a++,b--,c--){
    		if(buff[a] == buff[b])
    			continue;//Same.....good, go again
    		else{//1 difference, so no pallindrome
    			cout << buff << " is not a palindrome" << endl;
    			return false;
    		}
    	}
    	cout << buff << " is a palindrome" << endl;
    	return true;
    }
    
    int main()
    {
    	isPal(2552);//Yes
    
    	isPal(2556);//No
    
    	isPal(78987);//Yes
    
    	isPal(56874);//No
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM
  5. Seperating Main And Fractional Numbers
    By Robert_Ingleby in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2001, 07:18 AM