14,836,169 members
1.00/5 (1 vote)
See more:
Sample ip
16

Sample op
1 2 3 4 Bus 6 7 8 9 bUs 11 12 13 14 buS 16

What I have tried:

Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=1; i<=n; i++)
{
if(i%5==0)
{
System.out.print("bus");
}
System.out.print(i+" ");
}
Posted
Updated 30-Jan-20 4:06am

## Solution 2

You need to differentiate `bus` then, capitalizing a distinct letter at each occurrence. Tip: the following assertions are valid:
```when i % 3 == 2, Bus should be printed
when i % 3 == 1, bUs should be printed
when i % 3 == 0, buS should be printed```

I think this exercise is here to make you realize what the modulo operator can be used for.
phil.o 31-Jan-20 8:24am

Nothing prevents you from combining both constraints.
phil.o 31-Jan-20 8:27am

Yes.

## Solution 1

You print the number regardless of whether you printed "bus" or not.
You need to put the print of the number in an `else` clause so it only prints when you haven't printed "bus".

(And you probably want to print "Bus " instead of "bus" anyway!)