Results 1 to 3 of 3

Thread: Integer length problem

  1. #1
    Join Date
    Nov 2009
    Posts
    359

    Integer length problem

    Hi everyone
    I have a problem in my code, I think this is a simple problem but it's not clicking. I searched for this problem but I could not find the problem. Suppose, consider integer x = 987456, I want to write a program to find the length of integer without using any in-built functions in the library (obviously in this case the result should be 6). Remember it it is an array then its simple. But its an integer datatype. Any help on this. I still think this is a simple problem, so I am expecting a help from you.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Integer length problem

    Hello,
    ya this is a simple program, but no worry's it happens sometimes with every one. Check out the following code.
    Code:
    int p=12345;
    int count = 0;
    while(p !=0 )
    {
     p= p/10; //removes the last digit of the number; or better p /= 10
    count++;
    }
    Now, after the loop is executed "p" will be zero count will count the number of digits in the number. If you want back "p" just use int b=p; // this should be before the loop or p=b; //this should be after the loop.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Integer length problem

    The above program is correct. But still to increase you knowledge I have posted a simple code, just go through it.
    Code:
    int a=12345;
    if(a<0)a=-a;
    int count = 0;
    int remainder, reverse=0; // ----change over here
    while(a !=0 )
    {
    remainder = a%10; // to read the last digit -----change
    a = a/10; //removes the last digit of the number; or better a /= 10
    reverse = (reverse * 10) + remainder; //this statement should come before count++ else the value will be 543210
    count++;
    }
    If you have any more problems in future do post back again. Your problem will be definitely solved.

Similar Threads

  1. Replies: 4
    Last Post: 28-01-2011, 11:57 PM
  2. Java BigDecimal to Integer
    By Anthony12 in forum Software Development
    Replies: 6
    Last Post: 11-08-2010, 05:09 PM
  3. Integer range of CPU
    By Udyan in forum Motherboard Processor & RAM
    Replies: 6
    Last Post: 31-07-2010, 06:07 AM
  4. How to know the length of an integer in C++
    By troop in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 09:32 PM
  5. Verillo's integer and reg?
    By Fason in forum Networking & Security
    Replies: 3
    Last Post: 25-10-2008, 01:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,524,774.12085 seconds with 17 queries