Saturday 1 June 2013

Write A Program To Print Star Right-Triangle

Program To Print star triangle in c++
                                            
  1.        *         2.      *
          **                   **
        ***                   ***
      ****                   ****
    *****                   *****


Program 1

#include<iostream.h>     
#include<conio.h>
main()
{
int n;
cout<<"Enter Number Of Lines You want";
cin>>n;
cout<<endl;
clrscr();
for (int i=1;i<=n;i++)
{
for ( int j=1; j<=n-1; j++ )
{
cout<<" ";
}
for (int k=i;k>=1;k--)
{
cout<<"*";
}
cout<<"\n";
}
 getch();
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h>      :- It is Header File For Defination of functions like :- cout and cin 
Cout  is used to Print the value to output screen
Cin is used to get input entered by user 
----------------------------------------------------------------------------------------------------------

cout<<"\n";

This is known as new line character to print an empty line on output screen 
----------------------------------------------------------------------------------------------------------



LOGIC OF PROGRAM

for (int i=1;i<=n;i++)
{
for (int j=n-i;j>0;j--)
{
cout<<" ";
}
for (int k=i;k>=1;k--)
{
cout<<"*";
}
cout<<"\n";
}

In This program Three loops
LOOP #1 is executed n times It consist of two inner loops .This loop print new line characte after "*" is printed on user screen
Loop #2 is executed in order n-1,n-2,n-3,n-4 times for n lines . In this program this loop prints Single whitespace
LOOP #3 is used to print "*" character in given format.


Share this

0 Comment to "Write A Program To Print Star Right-Triangle"

Post a Comment