March 17, 2012

How to print a triangle in java

This program shows you how to print out a triangle in java.



package com.sequestration.fuzzylogic.implementation;
import java.util.Scanner;


public class Triangle {
public static void main(String[] args) {

Scanner scanner=new Scanner(System.in);
System.out.println("(Triangle Triangle)\n\n Enter an odd number: ");
int n=scanner.nextInt();
int[] a={78,73,74,69,83,72};
System.out.print("\t coded by :");
for(int i=0;i<=5;i++)
System.out.print((char)a[i]);
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
if((n%2)==1)
{
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
{
if((i==j)||((i+j)==(n+1)))
{
System.out.print(i+" ");
}else
{
System.out.print(" ");
}
}
System.out.println();
}

}
else
System.out.println("\t"+ n+ " is not a odd number \n\t choose "+(n+1)+" or "+(n-1));
}

}



Thanking you....

Facebook comments