Take the number of rows and columns as input from the user ( length and breadth of the parallelogram) and store it in two different variables. (‘r’ and ‘c’ in this case)
Run a loop ‘r’ number of times to iterate through all the rows. From i=0 to i<r. The loop should be structured as for(i=0 ; i<r ; i++)
Run a nested loop ‘i’ times to print the spaces before the parallelogram. From j=0 to j<i. The loop should be structured as for(j=0 ; j<i ; j++).
Inside this nested loop print whitesapce to print the trailing spaces before the parallelogram.
Run another nested loop ‘c’ times to iterate through each column of a row.
Inside this loop print star to print a start in each column of a row.
Inside the main loop move to the next line by printing a new line.
CODE IN C:
#include<stdio.h>
int main()
{
int i,j,r,c; //declaring integers i,j for loops and r,c for number of rows and columns
printf("Enter the number of rows\n"); //asking user for number of rows
scanf("%d",&r); //saving number of rows in a variable r
printf("Enter the number of columns\n"); //asking user for number of columns
scanf("%d",&c); //saving number of columns in a variable c
for(i=0;i<r;i++) //main loop for controlling number of rows
{
for(j=0;j<i;j++) //nested loop for trailing spaces
{
printf(" "); //printing white space
}
for(j=0;j<c;j++) //nested loop for printing stars row wise
{
printf("*"); //printing star
}
printf("\n"); //new line to move to the next line after a row
}
}
Login/Signup to comment