Use C only not C++ and write a little describtion to each step Using a nested fo
ID: 3803863 • Letter: U
Question
Use C only not C++
and write a little describtion to each step
Using a nested for loop create patterns 1, 2, and 3 shown below. In function main prompt the user for the height (number of rows) and width (number of characters per line) for pattern 1 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 nested for loops to create patterns 2 and 3 using the same height as in pattern 1.Explanation / Answer
//header file for standard input output
#include <stdio.h>
#include <math.h>
//driver main function
int main()
{
int row, col, spaces, height, width;
printf("Enter the height: ");//input height from the user
scanf("%d",&height);
printf("Enter the width: ");//input width from the user
scanf("%d",&width);
printf(" ");
//for loop to print pattern 1
for(row = 1; row <= height; row++){//loop for height
if(row == 1 || row == height)//if its the first row or last
for(col =1 ; col <= width; col++)
{
printf("=");//prints "="
}
else{
printf("*");
for(spaces =1 ; spaces <= width-2; spaces++)
{
printf(" ");
}
printf("*");
}
printf(" ");
}
printf(" ");
if(height+1 == width){//prints pattern only if height+1 == width
//for loops to print pattern 2
for(row = 1; row <= height/2; row++){//loop for height
for(col = 1; col <= row; col++){//loop for width
printf("*");
}
printf(" ");
}
for(row = height/2; row >= 1; row--){//loop for height
for(col = 1; col <= row; col++){//loop for width
printf("*");
}
printf(" ");
}
}
else{
printf("Cannot print Pattern 2 with height %d and width %d. ",height,width);//showing error message if height+1 is not equals to width
}
return 0;
}
HOPE THIS HELPS,
FEEL FREE TO RATE AND COMMENT
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.