-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from IoannisLazaridis/fizz-buzz-java
Solution for Fizz Buzz problem #3 in Java ☕
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} | ||
} |