-1

what's wrong with this code? It's supposed to get a digit from me, then show all the numbers between 100 & 1000000 containing that digit...

#include <stdio.h>
int main () {
int n,m;
puts("Enter your digit:\n");
scanf("%d\n", n);
int j=100;
while (j<=1000000) {
  m=10;
  if (j%m==n) {printf("%d\n",j);}
    while (j/m>=1) {
    if ((j/m)%10==n) {printf("%d\n",j);}
    m=m*10;}
  j+=1;}
return 0;
}
Morgan
  • 1
  • 2
  • Did you try stepping through your code with a debugger? If yes, where exactly does the code do something different than you would expect it to? (If no, I strongly recommend you to familiarize yourself with the debugger of your development environment. In your software development career, this will be the single most important tool to find bugs in your code.) – Heinzi Oct 22 '22 at 19:47
  • About `scanf("%d\n", n);` please see [What is the effect of trailing white space in a scanf() format string?](https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string) It should be `scanf("%d", n);` Otherwise it will wait for you to enter some non-whitespace. – Weather Vane Oct 22 '22 at 19:51
  • I'm not quite sure what you're trying to do, but is `m=m*110` deliberate? I'm sort of expecting `m=m*10`. But there are neater ways. – Persixty Oct 22 '22 at 19:51
  • I run the program and enter a digit but don't get any anwers. – Morgan Oct 22 '22 at 19:54
  • There is another error that the compiler should tell you about which I just noticed: `scanf("%d", &n);` Add the &. – Weather Vane Oct 22 '22 at 19:57
  • 1
    To be clear: `scanf("%d\n", n);` should be `scanf("%d", &n);` – Weather Vane Oct 22 '22 at 19:59
  • 2
    I had that scanf part wrong...changed it to scanf("%d",&n) and it worked! thank you so much!!! – Morgan Oct 22 '22 at 20:03
  • "I had that scanf part wrong" is practically a tautology. `scanf` is a terrible tool for beginners to use. (It is a terrible tool in general, but especially if you are just learning the language.) Get parameters like this from the command line (argv), not from the input stream. – William Pursell Oct 28 '22 at 12:43
  • `while (j/m>=1)` do you even know what it'll behave? It's equivalent to `while (j >= m)` for positive values but less readable and much slower. Integer division in C doesn't produce a floating-point value – phuclv Oct 28 '22 at 14:05

2 Answers2

0

When using scanf use the '&' sign after the comma. The passes a pointer to the variable so scanf can store the value.
It is wise to avoid using '\n' inside scanf; so it should look like this:

scanf("%d", &n);
James Risner
  • 5,451
  • 11
  • 25
  • 47
Samwel001
  • 1
  • 2
0

Try using this code and see how it works

#include <stdio.h>
#include <math.h>
int main () {
    int n,m;
    puts("Enter your digit:\n");
    scanf("%d", &n);
    for(int i = 1;(pow(10,i)*n)<1000000;i++)
    {
        for(int j = 0;j<(pow(10, i));j++)
        printf("%lf\t\t", ((pow(10, i)*n))+j);
        printf("\n");
    }
    return 0;
}
Samwel001
  • 1
  • 2