Skip to content

Commit

Permalink
Merge pull request #47 from IoannisLazaridis/fizz-buzz-java
Browse files Browse the repository at this point in the history
Solution for Fizz Buzz problem #3 in Java ☕
  • Loading branch information
ademclk authored Aug 24, 2022
2 parents 53e4d48 + cf21d41 commit 8486a21
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions challenges/fizz_buzz/FizzBuzz.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Scanner;

public class FizzBuzz {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Requesting from user the (n) number of integers
System.out.println("Enter the number: ");
// Reading the integer from the users input
int n = scanner.nextInt();
System.out.println("The Fizz, Buzz and FizzBuzz numbers are: ");

// Iterating from 1 to n
for (int i = 1; i <= n; i++) {
// If the integer is divisible by both 3 and 5 then we print the integer followed by "FizzBuzz" word
if (i % 3 == 0 && i % 5 == 0) {
System.out.print(i + " -> ");
System.out.println("FizzBuzz");
// Else-If the integer is divisible by 3 then we print the integer followed by "Fizz" word
} else if (i % 3 == 0) {
System.out.print(i + " -> ");
System.out.println("Fizz");
// Else-If the integer is divisible by 5 then we print the integer followed by "Buzz" word
} else if (i % 5 == 0) {
System.out.print(i + " -> ");
System.out.println("Buzz");
}
}
}
}

0 comments on commit 8486a21

Please sign in to comment.