Star Program In C Mcq Exercise 3

int main {

int i, j, n = 5;

// Loop through each row

for (i = 1; i <= n; i++) {

// Print asterisks based on row number

if (i < n) {

printf("*");

if (i > 1) {

printf(" ");

}

printf("\n");

} else {

for (j = 1; j <= n; j++) {

printf("*");

}

printf("\n");

}

}

return 0;

}

Example


Explanation:

The correct answer is option (a).

It prints stars at the edges and the base is filled so that a pattern of a hollow pyramid is formed along with a filled base.

2. Which is the output of the following program?

include <stdio.h>

int main {

int i, j, n = 5;

for (i = 1; i <= n; i++)

{

for(j = 1; j < i; j++)

{

printf(" ");

}

for (j = 1; j <= n; j++)

{

printf("*");

}

printf("\n");

}

return 0;

}

Example


Explanation:

The correct answer is option (a).

The first for loop dictates the rows, and the two inner for loops increase the spaces before printing the defined number of stars. In combination, these create a right-shifted rectangle-like figure out of the stars.

3. What is the output of the following code?

include <stdio.h>

int main {

int i,j,n=5;

for(i=n;i>0;i-=1)

{

for(j=1;j<=i;j++)

{

printf("*");

}

for(j=i+2;j<=n;j++)

{

printf(" ");

}

for (channel j = i + 1; channel j <= n; channel j++) {

printf(" ");

}

for (j = 1; j < i; j++) {

printf("*");

}

printf("\n");

}

return 0;

}

Example


Explanation:

The correct answer is option (b).

It prints an output that forms a mirrored decreasing star pattern with spaces in the middle. Starting with n on both sides, the number of stars in between progressively decreases to 1 with increasing spaces.

4. What is the output of the following code?

include <stdio.h>

int main {

int sum = 0;

int n=4;

for (int i = 1; i <= n; i++)

{

for (int j = 1; j <= n; j++)

{

if (j == i || j == n-i + 1)

{

printf("*");

}

else

printf(" ");

}

printf("\n");

}

return 0;

}

Example


Explanation:

The correct answer is option (a).

The condition if (j == i || j == n - i + 1) makes stars on the diagonals of the square, which gives an impression of an 'X'.

5. What is the output of the following code?

include <stdio.h>

int main {

int i, n = 5;

// Loop through each row

for (i = 1; i <= n; i++) {

// Print an asterisk

printf("*");

// Print a space after the asterisk for rows 2 to 5

if (i > 1) {

printf(" *");

}

// Move to the next line

printf("\n");

}

return 0;

}

Example


Explanation:

The correct answer is option (a).

The 'Row' control is determined by the outer loop, while the inner loops manage 'spacing' and the 'stars'. The stars are printed only at the beginning and at the end of the row, which results in the end effect of a pyramid in a hollow form.

Input Required

This code uses input(). Please provide values below: