0

I have a question: I’m supposed to build a program where when I enter an integer below a hundred, all numbers smaller than said integer and containing the digit “3″ appear on the screen (etc, if I enter 14, the numbers “3, 13″ should appear).

However, there’s something wrong with my code, please help! Thank you!

The code:

#include <stdio.h>

int main(int argc, const char * argv [])
{
    int wholenumber;
    printf("百以内の整数を入力してください\n");
    scanf_s("%d", &wholenumber);

    while(0 <wholenumber)
        {
            wholenumber--;

        while(0 < wholenumber){
        wholenumber = 3 %10;
        wholenumber = 3 /10;

         if (wholenumber == 3);
         {printf("%3d",wholenumber);}
    }
    }
    return 0;
}
Ingo
  • 36,037
  • 5
  • 53
  • 100

6 Answers6

1

I have no idea what your intention was with this code:

 wholenumber = 3 % 10;
 wholenumber = 3 / 10;

First line sets the variable to 3, the second to 0.. which forces the program to exit from the loop.

perreal
  • 94,503
  • 21
  • 155
  • 181
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • It does? I was hoping to be able to check if an the number three was in the integer wholenumber with that (I was looking at this http://stackoverflow.com/questions/4977456/how-to-check-if-a-int-var-contains-a-specific-number page) – user2537878 Jul 01 '13 at 06:23
1

If x is an integer between 0 and 99, the following will check whether either of its digits is a 3:

if (x / 10 == 3 || x % 10 == 3) {
   ...
}

I leave the rest as an exercise for the reader.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Code:

#include<stdio.h>

int main()
{
    int i,num;
    int t1,t2;             // Variable to store data temporary
    printf("\nEnter the number : \n");
    scanf("%d",&num);

    for(i=0;i<num;i++)
    {
     t1= i/10;
     t2= i%10;

     if((t1==3) || (t2 ==3))   //check if number has 3 in it
       printf(" %d", i);

    }
return 0;
}

This is code which is required.Thanks to @amulous for pointing out mistake. Note: It is assumed that entered number is less than 100 as required by user who asked question.

mvv1277
  • 246
  • 2
  • 4
0

You can write this more efficiently, and this will work for any upper limit:

#include <stdio.h>

int main(int argc, const char * argv [])
{
    int wholenumber;
    printf("百以内の整数を入力してください\n");
    scanf("%d", &wholenumber);
    for (int tens = 1; tens < wholenumber; tens *= 10) {
        for (int i = 3 * tens; i < wholenumber; i+= 10*tens) {
            printf("%3d\n", i);
        }
    }
    return 0;
}
perreal
  • 94,503
  • 21
  • 155
  • 181
0
#include <stdio.h>

int contain3(int n){
    if(n == 0) return 0;
    return (n % 10 == 3) ? 1 : contain3(n/10);
}

int main(void){
    int i, wholenumber;
    printf("百以内の整数を入力してください\n");
    scanf_s("%d", &wholenumber);

    for(i=3;i<wholenumber;++i)
        if(contain3(i))
            printf("%3d", i);

    return 0;
}

#include <stdio.h>
#include <limits.h>

int main(void){
    int i, wholenumber;
    int data[]={3,13,23,30,31,32,33,34,35,36,37,38,39,43,53,63,73,83,93,INT_MAX};
    printf("百以内の整数を入力してください\n");
    scanf_s("%d", &wholenumber);

    for(i=0;data[i]<wholenumber;++i)
        printf("%3d", data[i]);

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

OP solution is conceptionallly close but needs changes.

  1. The while() loop test destroys wholenumber. The test for '3' containment should use a copy of wholenumber

  2. the syntax of wholenumber = 3 /10; should be wholenumber_test /= 10;

  3. other syntax errors.


void whole(int wholenumber, int digit) {
  while (0 < wholenumber) {
    int test = wholenumber;  // form test value
    while (0 < test) {
      if ((test%10) == digit) {
        printf("%3d\n", wholenumber);
        break;  // no need to test other digits
      }
      test /= 10;  // move onto next digit
    }
    wholenumber--;
  }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256