Skip to content

Instantly share code, notes, and snippets.

@rajdeep26
Last active December 15, 2015 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajdeep26/5211986 to your computer and use it in GitHub Desktop.
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.
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