Program in C to print different types of structure
For printing this type structure in C:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Here as we seen the figure is in row and column form thus we have to declare two variable ( i and j ) for row and column respectively. Here we use for loop statement of c.
/* program to print structure */
#include<stdio.h>
void main()
{
int i,j,n ;
clrscr () ;
printf ("\n Enter number of rows:") ;
scanf ("%d", &n) ;
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
printf ("*") ;
printf (" ") ;
}
printf ("\n") ;
}
getch () ;
}
Output screen ::
Enter number of rows :: 5
output ::
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
For printing this type structure in C:
*
* *
* * *
* * * *
* * * * *
* * * * * *
Here as we seen the figure is in row and column form thus we have to declare two variable ( i and j ) for row and column respectively. Here we use for loop statement of c.
/* program to print structure */
#include<stdio.h>
void main()
{
int i,j,n ;
clrscr () ;
printf ("\n Enter number of rows:") ;
scanf ("%d", &n) ;
for (i=1;i<=n;i++)
{
for (j=1;j<=i;j++)
{
printf ("*") ;
printf (" ") ;
}
printf ("\n") ;
}
getch () ;
}
Both of program have nearly same syntax but there is small change in their column for loop statement in case of first type their condition is for (j=1;j<=n;j++) and in case second we have use for (j=1;j<=i;j++).
Output Screen ::
Enter number of rows :: 5
output ::
*
* *
* * *
* * * *
* * * * *
Comments
Post a Comment
Thank You for your participation..