Wednesday, January 8, 2014

Write the program to generate following triangle. 1 1 3 1 3 5 1 3 5 7

Program:

  Write the program to generate following triangle.
  1
  1 3
  1 3 5
  1 3 5 7

Code:

package com.ranga;

class TriangleDemo
{
    public static void main (String[] args)
    {
        int temp = 0;
        for(int i=0; i< 4; i++) {
            for(int j = 0; j<=i; j++) {
                if(j == 0) {
                  temp = j + 1;
                } else {
                  temp = temp + 2;   
                }
                System.out.print(temp+" ");
            }
            System.out.println();
        }
    }
}

Output:

1 
1 3
1 3 5
1 3 5 7
 
You can also see live example in ideone.
http://ideone.com/ie8lmD 

Related Posts:

  • Write a Program to find the longest palindrome in a given StringWrite a Program to find the longest palindrome in a given StringProgram: Output:String: HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDELongest Palindrome: 12345678987654321… Read More
  • Important C written Test Programs... P { margin-bottom: 0.08in; direction: ltr; color: rgb(0, 0, 0); line-height: 115%; widows: 2; orphans: 2; }P.western { font-family: "Calibri",sans-serif; font-size: 11pt; }P.cjk { font-family: "Calibri",sans-serif; f… Read More
  • AJAX Drop down Example with DatabaseThe following are the steps for creating a dropdown list by using ajax with database.1. Create Dynamic Web Project : Open File -> New -> Other... -> Web -> Dynamic Web Project to create a dynamic Web pro… Read More
  • Singleton Design Pattern Example Program in JavaDefinition: The singleton pattern is one of the simplest design patterns: it involves only one class which is responsible to instantiate itself, to make sure it creates not more than one instance; in the same time it provides… Read More
  • Spring MVC Hello World ExampleIn this post, we are going to see how to implement Spring MVC HelloWorld example.Step 1:The initial step is to create Dynamic Web Project in eclipse. To create a new Dynamic project in Eclipse IDE select File -> Dynamic We… Read More

0 comments: