how can i print a triangle in java.Its shape is like this
A
B C
D E F
G H I J
K L M N O
i have to use ascii code for printing alphabets.

Recommended Answers

All 8 Replies

Member Avatar for sravan953

Try this, it should work:

class print_ascii
{
static void ascii()
{
    for(int j=1;j<=26;j++)
        {
            int n=65;
            for(int i=1;i<=j;i++)
                {
                    char a=(char)n;
                    System.out.print(a+" ");
                    n++;
                }
             System.out.println();
            }
        }
    }
commented: Don't just hand out code. -2
commented: No free cookies here. -4
commented: Code provided directly +0

i am new in programming plz tell me,will i have to give a value on runtime??

C:\jdk1.3\bin>javac print.java

C:\jdk1.3\bin>java print
Exception in thread "main" java.lang.NoSuchMethodError: main
.This was given by the program

thanks i solved this problem.But i have to print till "J" only what should i do.

what should i do i have to print till "J" only

Just before you print each letter, check to see if its greater than 'J', if it is, you can exit your program immediately (exit is a method in the System class)

class print_ascii
{
static void ascii()
{
    for(int j=1;j<=26;j++)
        {
            int n=65;
            for(int i=1;i<=j;i++)
                {
                    char a=(char)n;
                    System.out.print(a+" ");
                    n++;
                }
             System.out.println();
            }
        }
    }

Yes, and... ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.