Thread: finding no of digit

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    finding no of digit

    i've wrote the code for finding the num of digits in the function Digits(num)
    the function should return no of digit in the num, but the output of my coding doesn't runs well.instead od returning 5, it returns 4!

    here's my code:
    #include<stdio.h>
    #include<conio.h>
    int Digits(int num);
    void main()
    {
    int num;
    num = 12345;

    clrscr();
    printf("%d",Digits(num));
    getch();
    }
    int Digits(int num)
    {
    int i;

    if(num>i)
    {
    num = num / 10;
    ++i;
    return i;
    }
    else
    return i;



    }

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    int Digits(int num)
    {
    	num = num < 0 ? -num : num;
    	int i = 1; 
    
    	while(num>i)
    	{
    		num = num / 10;
    		++i;
    	}
    	return i;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main()
    main returns an int, anything else is wrong.
    My best code is written with the delete key.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Use either just main( ), or int main( ). NO VOID MAIN!!!!
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error msg instead of the result so far
    By maybabier in forum C Programming
    Replies: 25
    Last Post: 03-20-2009, 02:45 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM
  4. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  5. Roman number converter
    By BalanusBob in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 06:29 AM