-3

for loop runs infinitely and it prints 1 infinitely when I run the below code.

#include<stdio.h>
#include<conio.h>
void main(){
int i,n;
scanf("%d", &n);
for(i=1;i<=n;i+2){
printf("%d",i);
}
getch();
}

If the input of n= 10

Actual Output:

11111111111111111111111111111111111111111111111111.......

Expected Output:

13579

I want to know why 1 is printed infinitely.

Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59
Deepshika S
  • 500
  • 4
  • 16
  • You might want to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) for that or look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Sep 02 '19 at 08:10
  • 5
    `... i<=n; i+2){` - `i` never changes. – meowgoesthedog Sep 02 '19 at 08:11

1 Answers1

3

i+2 does not change i, you want i += 2 or i = i + 2

mch
  • 9,424
  • 2
  • 28
  • 42