• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Finding the length of an integer with only if and switch

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

kayson

Member
Joined
Jan 5, 2005
So my friend is doing a C programming class, and he has to make a program that will take an integer input and output the number of digits it has. The catch: he's supposed to only use if or switch statements (and other general stuff).

Here's the assignment: http://www.ece.arizona.edu/~ece175/assignments/assignment04.pdf

It would be so easy to do if there wasnt that restriction. As of right now, all I can think of is using a switch and then doing a bunch of intervals, but that would take forever. Any ideas?
 
He has to use IF or SWITCH. This was the easy part he was talking about. I am unfamiliar with C but have done C++ and I wonder if you can type cast the integer into a char array and then IF..THEN the ubound of the array.
 
Yea can't use while loops...He hasn't learned about arrays yet either so that wont work.
 
Well, an int at max will have number*10^4 digits because the maxed signed value for an int is 32000 something

Start your count off at 1 (it'll always have at least one digit, accounts for the 'number'.) Take the int, divide by 10. If it's not 0, increase the count. Do that 4 times and you should have your answer.

Or you could just chain together, works if it's a positive int

int count = 1;
if( 10 <= num && num < 100 )
count = 2;
else if (100 <= num && num < 1000 )
count = 3;
else if....
 
An integer can go to 32,780. This means there are 5 digits total. Also this means you need to limit the user's input to less then this amount.
 
A typical C integer can go to 2 147 483 648.

if( num != 0)
{
num/=10;
count ++;
}
if( num != 0)
{
num/=10;
count ++;
}
if( num != 0)
{
num/=10;
count ++;
}
if( num != 0)
{
num/=10;
count ++;
}
if( num != 0)
{
num/=10;
count ++;
}
if( num != 0)
{
num/=10;
count ++;
}
if( num != 0)
{
num/=10;
count ++;
}
if( num != 0)
{
num/=10;
count ++;
}
if( num != 0)
{
num/=10;
count ++;
}
if( num != 0)
{
num/=10;
count ++;
}
 
Im so glad there are ppl out there like you guys I hate programing I would rather shoot my self in the foot then program
/end thread jack
 
An int is traditionally from -32767 to +32768 (15 numerical bits + 1 sign bit). unsigned int assumes nonnegative. There is no hard and fast rule except that short <= int <= long. It is common for all 3 now to be 32 bit. Also common is 16 bit shorts, 32 bit ints, and 64 bit longs. In the old days ints were often 16 bit, and shorts could even be 8 bit (1 byte), back when memory was much more rare.

Similarly, floats are often now the same as doubles, but they are never more precise. Sometimes they are less.
 
There's an impressive way to do it with recursion, but judging by the class's website, they haven't covered recursion yet (and won't for several weeks). But keep this solution in mind in case it comes up again at that time:

Code:
int countdecimaldigits(int value)
{
   if (value == 0)
      return 0;
   return countdecimaldigits(value/10)+1;
}

int main()
{
   int num = 0;
   scanf("%d", &num);
   int digits = countdecimaldigits(num);
   printf("%d\n", num);
   return 0;
}

ETA: This type of question ("do something without using looping constructs") is often used as a technical interview question when hiring programmers. Iterative algorithms can often be easily converted into tail-recursive algorithms.
 
Who needs loops :D?

Code:
//Digits.c
....Integer input

int _digits=getDigits(thatNumber);

int getDigits(signed int number)
{
if(number<0)
number=-number;   // If its negative, make it positive

if( number > 0 999 999 999)  // Assuming its signed. 
return 10;
if( number > 0 099 999 999)
return 9;
if( number > 0 009 999 999)
return 8;
if( number > 0 000 999 999)
return 7;
if( number > 0 000 099 999)
return 6;
if( number > 0 000 009 999)
return 5;
if( number > 0 000 000 999)
return 4;
if( number > 0 000 000 099)
return 3;
if( number > 0 000 000 009)
return 2;
if( number > 0 000 000 000)
return 1;
else
it is 0;
}

OR:

Code:
int number;
int digits=0;  //ASSUMING 0 IS 0

Check_Statement_Label:
if( int(number/=10)!=0)  
{digits++; goto Check_Statement_Label;}

last code might have a bug.
 
Where does the assignment say anything about decimal digits in part c?

Code:
cout << sizeof(int);

:D

JigPu
 
Back