Friday, May 6, 2016

C programs to display Half-Pyramid/Inverted Half-Pyramid of stars and numbers


1. Write a Program in C to print half pyramid using * as shown in figure below

*
* *
* * *
* * * *
* * * * *

/*program to display the pattern*/
#include
#include
int main()
{
int m,n,num;
printf("Enter the number of rows: ");
scanf("%d",&num);
for(m=1;m<=num;++m)
{
for(n=1;n<=i;++n)
{
printf("*");
}
printf("\n");
}
return 0;
}


2. A Program in C to print inverted half pyramid using * as shown in figure    below

* * * * *
* * * *
* * *
* *
*

#include
#include
int main()
{
int m,n,num;
printf("Enter the number of rows: ");
scanf("%d",&num);
for(m=num ; m>=1 ; - -m)
{
for(n=1 ; n<=m ; ++n)
{
printf("* ");
}
printf("\n");
}
return 0;
}

3. A Program in C to print ithe following pattern.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include
int main()
{
int m,n,rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(m=1;m<=rows;++m)
{
for(n=1;n<=i;++n)
{
printf("%d ",n);
}
printf("\n");
}
return 0;
}

4. A Program in C to print the following pattern.(invert half Pyramid)

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

#include
int main()
{
int m,n,rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(m=rows ; m>=1 ;--m)
{
for(n=1;n<=m;++n)
{
printf("%d ",n);
}
printf("\n");
}
return 0;
}

No comments:

Post a Comment