Anúncio
Problem- You are stranded in the Amazon rain forests with a robot who.docx
Problem- You are stranded in the Amazon rain forests with a robot who.docx
Próximos SlideShares
Python in detailsPython in details
Carregando em ... 3
1 de 2
Anúncio

Mais conteúdo relacionado

Mais de rtodd884(20)

Anúncio

Problem- You are stranded in the Amazon rain forests with a robot who.docx

  1. Problem: You are stranded in the Amazon rain forests with a robot who calls himself FizzBuzz. The robot is pretty cool because it does all the cleaning for you, but it has an annoying habit. It gives you random numbers and expects you to say “Fizz― if the number is divisible by 3, or “Buzz” if the number is divisible by 5, or “Fizz Buzz” if the number is divisible by both 3 and 5. You humor the robot for a while, but it’s getting stressful, and things are coming to a point where your relationship with the robot may suffer if you do not automate these interactions with him. In the program below, the robot will give you a number in the variable ‘num’. You have to print the appropriate value (“Fizz”, “Buzz”, “Fizz Buzz”, “―) to the system console. What we expect: If the value in ‘num’ is 3 we expect your program to print “Fizz―, ditto for 6, and 9. If the value in ‘num’ is 5, we expect your program to print “Buzz―, ditto for 10 and 20. However, if the value in ‘num’ is 15, we expect you to print “Fizz Buzz―, ditto for 30. Finally, if the value in ‘num’ is 7, we expect you to print “― an empty string, and ditto for 8, 11, 16, etc. Hint: You can use the % operator to get the remainder from a division operation Learning outcome: After doing this exercise, you should have learned how to: Use the % (modulus operator) Compare two values using one of the comparison operators in Java Make a decision in your code using an if...else if... else branch statement ------------------------------------------------ import java.util.Scanner; public class FizzBuzz { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("Enter the number:"); int num = scanner.nextInt(); ///{ //write your code here //start //end ///} }
  2. } Solution for(int i=1;i<=n;i++) { if(i%5==0) System.out.println("Buzz"); else if(i%3==0) System.out.println("Fizz"); else if((i%3==0)&&(i%5==0)) System.out.println("FizzBuzz"); else System.out.println(i); } } }
Anúncio