Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder primefalcon's Avatar
    Join Date
    Aug 2008
    Location
    /home/primefalcon/
    Posts
    678
    Thanks
    8
    Thanked 39 Times in 39 Posts

    C++ keep keeping 4 digits

    Hi there I'm trying to put together a program that can test to see if a number is the same forwards and backwards in a 4 digit number....

    and I'm coming across one little hiccup how do you keep 0's in a number before a non-zero number for example

    stop it from turning 0001 into just 1 in a variable

    Is there a way to do this?
    Last edited by primefalcon; 11-12-2008 at 02:28 AM.

  • #2
    Regular Coder primefalcon's Avatar
    Join Date
    Aug 2008
    Location
    /home/primefalcon/
    Posts
    678
    Thanks
    8
    Thanked 39 Times in 39 Posts
    Nvm I worked out another way to do it here's the source for anyone who's interested

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    cout << "\n\n\n";
    
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int count = 0;
    while(a <= 9)
    {
    	while(b <= 9)
    	{
    		while(c <= 9)
    		{
    			while(d <= 9)
    			{
    			
    if (a == d && b == c)
    {
    count++;
    cout << count << "->" << a << b << c << d << "\n";
    }
    
    			d++;
    			}
    		d = 0;
    		c++;
    		}
    	c = 0;
    	b++;
    	}
    b = 0;
    a++;
    }
    
    cout << "\n\n\n";
    return 0;
    }
    Last edited by primefalcon; 11-12-2008 at 11:22 PM.

  • #3
    New Coder
    Join Date
    Nov 2008
    Location
    Australia
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    good peice of code, however there is a simpler way to achieve what you want

    include <math.h> and that gives you access to the
    Code:
      isPallendrome
    which should do what you want

  • #4
    Regular Coder primefalcon's Avatar
    Join Date
    Aug 2008
    Location
    /home/primefalcon/
    Posts
    678
    Thanks
    8
    Thanked 39 Times in 39 Posts
    still though the problem I had was keeping like 3 0's in say 0001 without the way I did it, c++ kept stripping the 0's from the variable, and I wasn't sure how to overcome that


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •