Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
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

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.
 
Share this answer
 
Comments
phil.o 31-Jan-20 8:24am    
Nothing prevents you from combining both constraints.
phil.o 31-Jan-20 8:27am    
Yes.
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!)
 
Share this answer
 

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



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