Created
March 21, 2013 06:46
-
-
Save dhuma1981/5211139 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.*; | |
class VacationLabsHackathaon | |
{ | |
public static void main(String args[]) | |
{ | |
Scanner sc=new Scanner(System.in); | |
int n; | |
System.out.println("Enter a number : "); | |
n=sc.nextInt(); | |
ProblemFunctionClass pfc=new ProblemFunctionClass(); | |
pfc.problemFunction(n); | |
} | |
} | |
class ProblemFunctionClass | |
{ | |
public void problemFunction(int n) | |
{ | |
System.out.println("Answer:"); | |
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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment