14,828,654 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!)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 260 Richard Deeming 176 Patrice T 130 CPallini 90 Dave Kreskowiak 80
 OriginalGriff 1,961 Richard MacCutchan 855 CPallini 620 Dave Kreskowiak 525 Richard Deeming 488

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900