Using a nested for loop create pattern 1 and pattern 2 shown below. In function
ID: 3801372 • Letter: U
Question
Using a nested for loop create pattern 1 and pattern 2 shown below. In function main prompt the user for the height (number of rows) and width (number of characters per line) for the pattern to be created. You may use any number for height and width, just as long as the basic pattern is preserved. For pattern 1, make use of an if structure to instruct the program what to print on the various lines and columns. Also use a nested for loop to create pattern 2 using the same height and width as in pattern 1. Pattern 1: Pattern 2:Explanation / Answer
pattern 1:
#include <stdio.h>
int main()
{
int i, j,k,x,r,s;
printf("Enter number of rows: ");
scanf("%d",&x);
for(r=0;r<x+2;r++)
{
printf("=");
}
printf(" ");
for(i=0; i< x; i++)
{
printf("*");
for(j=1;j<= x;j++)
{
printf(" ");
}
printf("*");
printf(" ");
}
for(s=0;s<x+2;s++)
{
printf("=");
}
printf(" ");
return 0;
}
OUTPUT:
akolli@smarupureddy:~/python_programs$ gcc -o ex exampl1.c
akolli@smarupureddy:~/python_programs$ ./ex
Enter number of rows: 5
=======
* *
* *
* *
* *
* *
=======
Pattern 2:
#include <stdio.h>
int main()
{
int i, j,k,x;
printf("Enter number of rows: ");
scanf("%d",&x);
for(i=0; i< x; i++)
{
for(j=1;j<= x-i-1;j++){
printf(" ");}
for(k=0; k<=i ;k++ )
{
printf("*");
}
printf(" ");
}
return 0;
}
output:
akolli@smarupureddy:~/python_programs$ gcc -o ex exampl1.c
akolli@smarupureddy:~/python_programs$ ./ex
Enter number of rows: 5
*
**
***
****
*****
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.