0

it's me again. I deleted my previous question because it was very poorly asked and I didn't even include any code (i'm new at this site, and new at C). So I need to write a program that prints out the digits smaller than 5 out of a given number, and the number of the digits.

For example: 5427891 should be 421 - 3

The assignment also states that i need to print the numbers smaller than 5 in a recursive function, using void.

This is what I've written so far

#include<stdio.h>
void countNum(int n){
  //no idea how to start here       
}

int main()
{
  int num, count = 0;

  scanf("%d", &num);
  while(num != 0){
      num /= 10;           
      ++count;
  }
  printf(" - %d\n", count);


  }

I've written the main function that counts the number of digits, the idea is that i'll assign (not sure i'm using the right word here) the num integer to CountNum to count the number of digits in the result. However, this is where I got stuck. I don't know how to extract and print the digits <5 in my void function. Any tips?

Edit:

I've tried a different method (without using void and starting all over again), but now i get the digits I need, except in reverse. For example, instead of printing out 1324 i get 4231.

Here is the code

#include <stdio.h>
int rec(int num){
    if (num==0) {
       return 0;

              }
    int dg=0;
    if(num%10<5){
        printf("%d", num%10);
    dg++;
    }

    return rec(num/10);
}
int main(){
int n;
    scanf("%d", &n);
    int i,a;
    for(i=0;i<n;i++)
    {
    scanf("%d", &a);
     rec(a);
        printf(" \n");
    }



return 0;
}

Why is this happening and how should I fix it?

2
  • 1
    either check the returned value from scanf() and/or pre-initialize 'num' to 0; Dec 22, 2014 at 23:49
  • What would you like countNum(0) to do? Print "0" or nothing. Dec 25, 2014 at 22:22

4 Answers 4

2

There is nothing in your question that specifies the digits being input are part of an actual int. Rather, its just a sequence of chars that happen to (hopefully) be somewhere in { 0..9 } and in so being, represent some non-bounded number.

That said, you can send as many digit-chars as you like to the following, be it one or a million, makes no difference. As soon as a non-digit or EOF from stdin is encountered, the algorithm will unwind and accumulate the total you seek.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int countDigitsLessThanFive()
{
    int c = fgetc(stdin);
    if (c == EOF || !isdigit((unsigned char)c))
        return 0;

    if (c < '5')
    {
        fputc(c, stdout);
        return 1 + countDigitsLessThanFive();
    }
    return countDigitsLessThanFive();
}

int main()
{
    printf(" - %d\n", countDigitsLessThanFive());
    return EXIT_SUCCESS;
}

Sample Input/Output

1239872462934800192830823978492387428012983
1232423400123023423420123 - 25

12398724629348001928308239784923874280129831239872462934800192830823978492387428012983
12324234001230234234201231232423400123023423420123 - 50

I somewhat suspect this is not what you're looking for, but I'll leave it here long enough to have you take a peek before dropping it. This algorithm is fairly pointless for a useful demonstration of recursion, to be honest, but at least demonstrates recursion none-the-less.

1
  • this code is very clever; however, it needs to also inform the user of the executable what the user is expected to input and how to exit the executable. Dec 23, 2014 at 0:17
2

Modified to print values from most significant to least.

Use the remainder operator %.

"The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined" C11dr §6.5.5

On each recursion, find the least significant digit and test it. then divide the number by 10 and recurse if needed. Print this value, if any, after the recursive call.

static int PrintSmallDigit_r(int num) {
  int count = 0;
  int digit = abs(num % 10);
  num /= 10;
  if (num) {
    count = PrintSmallDigit_r(num);
  }
  if (digit < 5) {
    count++;
    putc(digit + '0', stdout);
  }
  return count;
}

void PrintSmallDigits(int num) {
  printf(" - %d\n", PrintSmallDigit_r(num));
}

int main(void) {
  PrintSmallDigits(5427891);
  PrintSmallDigits(-5427891);
  PrintSmallDigits(0);
  return 0;
}

Output
421 - 3
421 - 3
0 - 1

Notes:
This approach works for 0 and negative numbers.

3
  • Not clear what should happen if a digit is repeated. Assumed to repeat the display and count. Dec 22, 2014 at 23:39
  • His assignment requires "a recursive function" (although recursion does seem pointless for this task).
    – Dmitri
    Dec 22, 2014 at 23:40
  • @Dmitri Thank-you for highlighting that requirement. Dec 22, 2014 at 23:55
1

First of all, what you wrote is not a recursion. The idea is that the function will call itself with the less number of digits every time until it'll check them all.

Here is a snippet which might help you to understand the idea:

int countNum(int val)
{
  if(!val) return 0;
  return countNum(val/10) + ((val % 10) < 5);
}
-1
void countNum(int n, int *c){
    if(n != 0){
        int num = n % 10;
        countNum(n / 10, c);
        if(num < 5){
            printf("%d", num);
            ++*c;
        }
    }
}

int main(){
    int num, count = 0;

    scanf("%d", &num);
    countNum(num, &count);
    printf(" - %d\n", count);

    return 0;
}

for UPDATE

int rec(int num){
    if (num==0) {
        return 0;
    }
    int dg;
    dg = rec(num/10);//The order in which you call. 
    if(num%10<5){
        printf("%d", num%10);
        dg++;
    }

    return dg;
}

int main(){
    int n;
    scanf("%d", &n);

    int i,a;
    for(i=0;i<n;i++){
        scanf("%d", &a);
        printf(" - %d\n", rec(a));
    }

    return 0;
}

Your Answer

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