Java code to print an odd triangle of sta?
-
I need to print a triangle of stars using loops in java. The program should ask you for the number of stars in the base line. For example: Print the triangle of stars with a number of 3, it should print: * *** another example: print the triangle with the base line 7 * *** ***** ******* It should only print odd numbers.
-
Answer:
try public class Triangle { public void printTriangle(int base) { for(int i =0;i<base+1;i++) { if(i % 2 == 1) { for(int j=0;j<i;j++) { System.out.print("*"); } System.out.println(); } } } public static void main(String[] args) { Triangle t = new Triangle(); java.util.Scanner sc = new java.util.Scanner(System.in); System.out.println("Enter desired base: "); t.printTriangle(sc.nextInt()); } }
Ali at Yahoo! Answers Visit the source
Related Q & A:
- How to Code Encryption Algorithm in Java?Best solution by code2learn.com
- how to close the applet in java code?Best solution by Stack Overflow
- how to Create a Java Package from MATLAB Code?Best solution by Stack Overflow
- I need help with writing up the Java Code.Best solution by Yahoo! Answers
- How can i go to 16169 elza drive hacienda heights CA from vermont/sta monica station taking the red line metro?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.