1
void print(int num, int digits)
{
    double mod, divi;
    int mod1, divi1;
    mod = pow(10, digits);
    divi = pow(10, digits-1);
    mod1 = (int)mod;
    divi1 = (int)divi;

    if(digits > 0)
    {
        printf("%d\n", ((num%mod1)/divi1));
        digits--;
        print(num, digits);
    }
    else
    {
        return;
    }
}

This function is meant to print the digits from num vertically, however when I run it the only thing it prints are 0's. I know it gets the correct number of digits because it prints the same number of zeroes as there are digits in the number, so something must be wrong with my use of Modulo or Division here, and I can't quite seem to find it. Any help would be appreciated thank you!

The function to get the numbers(maybe the problem is here?):

void getNum()
{   
    int num, digits=0;
    printf("Enter a number to be printed vertically: ");
    scanf("%d", &num);

    if(num < 0)
    {
        digits = 1;
    }
    while(num)
    {
        num = num/10; //Here was the problem. Num was 0 once i passed it to print()
        digits++; //Simply added a new variable to fix it. Thanks all!
    }

    print(num, digits);
}
6
  • Works for me. What are your inputs?
    – NPE
    Oct 13, 2014 at 21:49
  • pow() may return a value just under the expected integer value. Better to round the result round(pow(10,digits)); before truncating to an int. Be sure to first #include <math.h> Oct 13, 2014 at 22:24
  • I would just input random numbers of any length and always get all 0's, the same number as the number of digits.
    – abzib
    Oct 13, 2014 at 22:29
  • Maybe the function I just added is where the error lies. (Getting the numbers)
    – abzib
    Oct 13, 2014 at 22:31
  • Well, looking at it I realize how stupid I am. I set num equal to 0 before passing it to the function. My apologies!
    – abzib
    Oct 13, 2014 at 22:36

4 Answers 4

1

I don't see anything wrong with your code, except that it's much too complicated and unnecessarily uses floating-point maths.

Here is an alternative:

void print(int num, int digits)
{
    if (digits > 0) {
        print(num / 10, digits - 1);
        printf("%d\n", num % 10);
    }
}

The trick is to move the printing after the recursive call. This allows for much simpler logic.

4
  • This has an interesting output when num < 0. But so does OP's. Oct 13, 2014 at 22:26
  • I copy-pasted this into my code and still got all 0's strangely...I've edited the above to add my getNum function, perhaps the problem lies there?
    – abzib
    Oct 13, 2014 at 22:30
  • @abzib: Yes, the problem is there. You modify num until it becomes zero, and then call print on its new zero value.
    – NPE
    Oct 14, 2014 at 5:48
  • @chux-ReinstateMonica, May not important but add this condition in the beginning solve that. if (num<0){ num *=-1; }
    – EsmaeelE
    Jan 13, 2020 at 21:53
0

This is a fix for my problem. I was setting num to 0 before passing it to the function. I simply declared another variable num1 to use in the while loop instead of the actual num which is passed to the function.

Code

void getNum()
{   

    int num, num1, digits=0;
    //get the num
    printf("Enter a number to be printed vertically: ");
    scanf("%d", &num);
    num1 = num;

    //Loop to find how many digits are in the number
    while(num1)
    {
        num1 = num1/10;
        digits++;
    }
    //call function to print
    print(num, digits);
}
0
int print(int num, int digits)
{
    int min = 0;
    if (digits > 0) {
        min = print(num / 10, digits - 1);
        if(num %10)
           printf("%s%d\n", num < 0 && !(min++) ? "-" : "", abs(num % 10));
    }
    return min;
}

int print1(int num, int digits)
{
    int min = 0;
    if (digits > 0) {
        min = print1(num / 10, digits - 1);
        if(num %10)
        {
            printf("%s", num < 0 && !(min++) ? "-\n" : "");
            printf("%u\n", abs(num % 10));
        }
    }
    return min;
}

int main()
{
    print(-1234, 9);
    printf("\n-------------\n\n");
    print1(-23456565, 9);
}
0

By little change on NPE answer I reach to version of code that eliminate digits argument and works for negative numbers.


Code

void print(int num)
{
    if (num < 0){
        num *=-1; 
    }

    if (num > 0) {
        print(num / 10);
        printf("%d\n", num % 10);
    }
}

[Edit1 and 2]: Correct INT_MIN overflow

Thanks for chux for his constructive comments I correct potential INT_MIN negation overflow. Another similar topic by chux .

void print(int num)
{

    #if INT_MIN < -INT_MAX
    ///In two's complements systems.
    ///handle special case

    if ( num == INT_MIN ) {
      //fprintf(stderr, "Houston, we have a problem\n");

      // Maybe return or exit here.
      //~ 2147483647 -> 2147483648
      print(INT_MAX/10);
      print(INT_MIN%10); //portable version ///print(8);
      return;
    }

    if (num < 0){   
            //num =-((unsigned)num);
            num *=-1; 
    }

    #endif  

    if (num > 0) {
        print(num / 10);      
        printf("%d\n",  num%10);
    }

}

Now if we call print()

int number = INT_MIN;// a negative number

print(-120);
printf("\n");
print(-321);

print(number);

Output

1
2
0

3
2
1
Houston, we have a problem

[Edit3]

Change code to work for also print(0).

int zeroSignal=1;

void print(int num)
{

    #if INT_MIN < -INT_MAX
    ///In two's complements systems.
    ///handle special case

    if ( num == INT_MIN ) {
      ///fprintf(stderr, "Houston, we have a problem\n");
      // Maybe return or exit here.
      //~ 2147483647 -> 2147483648
      print(INT_MAX/10);
      print(INT_MIN%10); //portable version ///print(8);
      return;
    }

    if (num < 0){   
            //num =-((unsigned)num);
            num *=-1; 
    }

    #endif  


    if (num==0 && zeroSignal){
        printf("Only zero: %d\n", 0);
    }

    if (num > 0) {
        zeroSignal = 0;
        print(num / 10);
        printf("%d\n",  num%10);
    }

    zeroSignal = 1;      
}

In above code I use global integer zeroSignal to implement a way that works for print(0).

2
  • @chux-ReinstateMonica, OK. I think now it handle - INT_MIN.
    – EsmaeelE
    Jan 16, 2020 at 4:37
  • print(8); assumes INT_MIN == -2147483648. That assumption is not needed nor portable programming. Consider print(INT_MIN%10); and drop the fprintf(stderr, ... Jan 16, 2020 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.