Write a short program that prints each number from 1 to n on a new line.
- For each multiple of 3, print “Multiple of 3” instead of the number.
- For each multiple of 5, print “Multiple of 5” instead of the number.
- For numbers which are multiples of both 3 and 5, print “Multiple of 3. Multiple of 5.” instead of the number.
Input : 15 Output : 1 2 Multiple of 3. 4 Multiple of 5. Multiple of 3. 7 8 Multiple of 3. Multiple of 5. 11 Multiple of 3. 13 14 Multiple of 3. Multiple of 5.
The idea is iterate from 1 to n and keep track of multiples of 3 and 5 by adding 3 and 5 to current multiple. If current number matches with a multiple, we update our output accordingly.
1 2 Multiple of 3. 4 Multiple of 5. Multiple of 3. 7 8 Multiple of 3. Multiple of 5. 11 Multiple of 3. 13 14 Multiple of 3. Multiple of 5. 16 17 Multiple of 3. 19 Multiple of 5.
This article is contributed by Nimish Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Decimal to Binary using recursion and without using power operator
- Operator Overloading '<<' and '>>' operator in a linked list class
- Java Program To Find Largest Between Three Numbers Using Ternary Operator
- Multiply a number by 15 without using * and / operators
- Find maximum and minimum element in binary tree without using recursion or stack or queue
- Difference between concat() and + operator in Java
- Swap two Strings without using third user defined variable in Java
- Execute main() multiple times without using any other function or condition or recursion in Java
- How to create a Circular image view in Android without using any library?
- Floor square root without using sqrt() function : Recursive
- Breadth First Search without using Queue
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Print Hello World Without using a Semicolon in Java
- Java Program to Print a Semicolon Without Using Semicolon
- Java Program to Print any Statement without Using the Main Method
- new operator vs newInstance() method in Java
- instanceof operator vs isInstance() method in Java
- new operator in Java
- Java Ternary Operator Puzzle
- Double colon (::) operator in Java