[Solved] Print the following 1 2 3 4 Bus 6 7 8 9 bUs 11 12 13 14 buS [closed]


Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(i=1; i<=n; i++)
{
    if(i%3==2 && i%5==0)
    {
        System.out.println("Bus");
    }
    else if(i%3==1 && i%5==0)
    {
        System.out.println("bUs");
    }
    else if(i%3==0 && i%5==0)
    {
        System.out.println("buS");
    }
    else
    {
        System.out.println(i+" ");
    }
}

System.out.println does not mean it will not go through the next piece of code. You need either an else as above, or a switch statement or a break after the println.

So to explain fully, when i = 5

it goes to the first if statement, prints Bus, checks the second and third if statements and fails both, then it also runs the last println because there is no if statement around it.

solved Print the following 1 2 3 4 Bus 6 7 8 9 bUs 11 12 13 14 buS [closed]