Last active
December 15, 2015 05:49
-
-
Save rajdeep26/5211986 to your computer and use it in GitHub Desktop.
Write a function which takes 'n' as an argument and prints all the numbers from 1 to 'n', except that: (a) if a number is divisible by 3, it should print 'Hip' instead of the number, (b) if divisible by 5 it should print 'Hop', and (c) if divisible by 3 & 5, it should print 'Whazaa' instead of the number.
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
import java.util.Scanner; | |
public class PrintNumbers | |
{ | |
static Scanner input = new Scanner(System.in); | |
public static void print(int n) | |
{ | |
for(int i=1;i<=n;i++) | |
{ | |
if((i%3 == 0)&& (i%5 == 0)) | |
System.out.println("Whazaa"); | |
else | |
if(i%5 == 0) | |
System.out.println("Hop"); | |
else | |
if(i%3 == 0) | |
System.out.println("Hip"); | |
else | |
System.out.println(i); | |
} | |
} | |
public static void main(String[] args) | |
{ | |
int n; | |
System.out.println("Enter the value of n: "); | |
n = input.nextInt(); | |
print(n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment