Written by Sai Sameera Thota on January 11, 2013 in C > Java > Programming

C and Java Program to Print Right aligned Triangle

GET ALERTS:

Get our Latest updates delivered to your mailbox!

Simple Java program to print a right aligned triangle using asterisks (*). The program uses two for loops. The outer loop is used for iteration and the inner loop is used to print the asterisks.

The program takes the number of rows as input and displays the output depending on the number of rows. For example, if 4(number of rows) is the input, output will be as follows

*
**
***
****

Print Right aligned/angled Triangle using Java

import java.io.*;
class right_aligned
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("\nEnter the number of rows : ");
		int n = Integer.parseInt(br.readLine());
		for (int i=0;i<n;i++)
		{
			for(int j=0;j<=i;j++)
			{
				System.out.print(" *");
			}
			System.out.println();
		}
	}
}

Print Right aligned/Angled Triangle using C

#include<conio.h>
#include<stdio.h>

void main()
{
	int n, i, j;
	clrscr();
	printf("\nEnter Number of Rows:");
	scanf("%d",&n);

	for(i=0;i<=n;i++)
	{
		for(j=1;j<=i;j++)
		{
			printf("*\t");
		}
		printf("\n");
	}

	getch();
}

Output:
right-angled-triangle-using-c-java

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: