0

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.

4
Contributors
8
Replies
10
Views
7 Years
Discussion Span
Last Post by JamesCherrill
-3

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();
            }
        }
    }
Votes + Comments
Code provided directly
No free cookies here.
Don't just hand out code.
0

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

0

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

0

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

0

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)

0
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();
            }
        }
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.