-2
import java.util.Scanner;
class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        System.out.println("Enter 2 numbers : ");
        int m = s.nextInt();
        int b = s.nextInt();
        while(m<=b){
            if(m%2!=0){
                System.out.print(m+" ");
                m+=1;
            }
        }
        s.close();
    }
}

I get an error in a platform called "Terv" The error is "Time Limit Exceeded" I tried running this on VS code and it was running for eternity after I give the input

Intr0vert
  • 1
  • 1
  • What is the input? – flwd May 12 '22 at 13:40
  • 6
    Think about it, it doesn't matter if you enter `m` as odd or even, either on the first (if odd) or the second iteration (if even) `m%2 != 0` will be `false` and `m` will then never be incremented again. Resulting in an inifinite loop. – maloomeister May 12 '22 at 13:44

1 Answers1

0

Lets say you enter m = 5 and b = 10 now you're checking if m is odd, if it is then you're saying print m = 5 and then increment the value of 5. Which means now it's 6. And 6 is even. Outside of that if(m%2!=0) statement you are not modifying m that means it is entering in an infinite loop.

I'm not quite sure what you're trying to do here but to avoid the infinite loop either add an else statement where you handle when m is even or increment by 2 if you want to keep it always odd.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95