Use the following C code producing output 1 to write a C code that produces outp
ID: 3528832 • Letter: U
Question
Use the following C code producing output 1 to write a C code that produces output
#include <stdio.h>
intmain()
{
int height, i, j;
char symbol;
printf("enter height:");
scanf("%d", &height);
printf("enter a symbol:");
scanf(" %c", &symbol);
for ( i = 1; i <= height; i++) {
// 1 iteration for each row of the pyramid
for ( j = 1; j <= height - i; j++)
printf(" "); // prints the initial blanks
for ( j = 1; j <= 2*i - 1; j++)
printf("%c", symbol); // prints the sequence of stars
printf(" "); // prints a newline: the row is finished
}
}
Output 1:
enter height:5
enter a symbol:$
$
$$$
$$$$$
$$$$$$$
$$$$$$$$$
Write a program that prints the following output:
please enter a number not greater than 9: 5
1
212
32123
4321234
543212345
Explanation / Answer
int main() {
int height, i, j;
printf( "Please enter a number not greater than 9: ");
scanf( "%d", &height );
for( i=1; i<=height; i++ ) {
for( j=1; j<=(height-i); j++ ){
printf( " " ); /* prints the initial blanks)
}
for( j=i; j>=1; j-- ) {
printf( "%d", j );
}
for( j=2; j<=i; j++ ) {
printf( "%d", j );
}
printf( " " );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.