Sunday, 8 September 2013

print star triangle

Program 1 

C++ LANGUAGE

#include<iostream.h>     
#include<conio.h>
void main()
{
int n;
clrscr();
cout<<"Enter Number Of Lines You want";
cin>>n;
cout<<endl;
for (int i=0; i<n; i++)
{
for (int j=i; j>=0; j--)
{
cout<<"*";
}
cout<<"\n";
}
 getch();
}

C LANGUAGE

  • #include<conio.h>
    void main()
    {
    clrscr();

    for(int i=0;i<5;i++)
    {
    for(int j=i;j>=0;j--)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }

OUTPUT :- 



Program 2

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=0;i<5;i++)
{
for(int j=i;j>=0;j--)
{
printf("%d",i+1);
}
printf("\n");
}
getch();

}


OUTPUT :-



PROGRAM 3


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int k=5;
for(int i=0;i<5;i++)
{
for(int j=i;j>=0;j--)
{
printf("%d",k);

}
k--;
printf("\n");
}
getch();
}


Program 4

#include<stdio.h>
#include<conio.h>
void main()
{
int k=5;
for(int i=0;i<5;i++)
{
for(int j=i;j>=0;j--)
{
printf("%d",k); 
}
k--;
printf("\n");
}
getch();
}

EXPLANATION 



1. At first k=5 and j loop runs one time as j=i ; j>=0
so it prints 5
now k--; so k=4

2. Secondly k=4 j loop runs two time as j=i (i is 1) ; j>=0
so it prints 44
Now k--; so k=3

3. thirdly k=3 j loop runs three times j=i (i is 2) ; j>=0 
so it prints 333
Now k--; so k=2 

4. fourthly k=2 j loop runs four times j=i (i is 3) ; j>=0 
so it prints 444
Now k--; so k=1

5. Fifthly k=1 j loop runs five times j=i (i is 4) ; j>=0 
so it prints 5555
Now k--; so k=0  


Share this

0 Comment to "print star triangle "

Post a Comment