How To Print "HAPPY" In A Triangle In C++?

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

Was this solution helpful to you?

Just Added Q & A:

Find solution

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.