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);
}
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
abzib
  • 11
  • 3

4 Answers4

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • This has an interesting output when `num < 0`. But so does OP's. – chux - Reinstate Monica Oct 13 '14 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 '14 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 '14 at 05:48
  • @chux-ReinstateMonica, May not important but add this condition in the beginning solve that. `if (num<0){ num *=-1; }` – EsmaeelE Jan 13 '20 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);
}
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
abzib
  • 11
  • 3
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).

EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
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___________
  • 60,014
  • 4
  • 34
  • 74