Life, the Universe, and Everything using C

#include <stdio.h>
int main(void)
{
int n;
scanf("%d",&n);
while(n!=42)
{
printf("%d",n);
}
return 0;
}

What’s wrong with this code?

You are scanning n once and if it is not 42, you are printing the same value infinitely. Put another scanf inside while loop after printf so as the value of n also changes till it is not 42.

Also put a \n in printf


#include <stdio.h>
 int main(void)
{
    int n;
    scanf("%d",&n);
    while(n!=42)
    {
       printf("%d\n",n);
       scanf("%d",&n);
    }
    return 0;
}

1 Like

yes sorry i forgot that to add… :frowning: