Rahul Khatri
Rahul Khatri

Reputation: 287

Wrong output in printing values using for loop in C

I wrote a simple C program to print all the multiples of 3 but there is some error during runtime my code is:

#include <stdio.h>

void main(void) {
    int i, x;
    for(i = 1; i < 1000; i++) {
        x = i % 3;
        if(x == 0) {
            printf("%d\n", i);
        }
     }
}

the problem is that if I enter numbers greater than 891 till 1000 in the loop the output is starting from 6 instead of 3 and if i write the code as above than the output is starting from 114. for the values less than or equal to 891 it is showing the correct output.

Upvotes: 0

Views: 222

Answers (1)

Reut Sharabani
Reut Sharabani

Reputation: 31339

Make sure you can view all output:

reuts@reuts-K53SD:~/ccccc$ cat mmph.c && gcc mmph.c
#include<stdio.h>
main(){

    int i,x;
    for(i=1;i<1000;i++)
    {
        x=i%3;

        if(x==0){
            printf("%d\n",i);

        }
    }
}
reuts@reuts-K53SD:~/ccccc$ ./a.out | egrep "^3$|999"
3
999

As you can see, this works. Your output is probably truncated.

Upvotes: 1

Related Questions