int main
{
int i, j, n = 5;
//It prints the first part of the pattern( 5 rows of 5 stars)
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("*");
}
printf("\n");
}
// Print the second part of the pattern (right-aligned stars)
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
printf(" ");
}
printf("*\n");
}
return 0;
}
Explanation:
The correct answer is option (d).
The program prints stars from 1 on both the right and left sides and in the middle, it has spaces up to n. After that, it prints stars again, but in the reverse order, again up to n, so it looks like an asterisk with a gap in the middle.
2. What is the output of the following C programming code?
include <stdio.h>
int main {
int i, j;
int n = 5;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Explanation:
The correct answer is option (b).
The if-statement inside the inner ?For loop?, if (j >= n - i + 1), ensures stars are printed starting from the bottom right of the pyramid and moving up for each new row. This results in the formation of the right-aligned triangle, as shown in the next figure.
3. Which of the following code snippets will print a diamond star pattern?
include<stdio.h>
int main {
int n = 5;
int i, j, space;
// Upper part of the diamond
space = n - 1;
for (i = 1; i <= n; i++) {
// Print spaces
for (j = 1; j <= space; j++) {
printf(" ");
}
// Print asterisks
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
// Move to the next line
printf("\n");
// Decrease space for next row
space--;
}
// Lower part of the diamond
space = 1;
for (i = 1; i <= n - 1; i++) {
// Print spaces
for (j = 1; j <= space; j++) {
printf(" ");
}
// Print asterisks
for (j = 1; j <= 2 * (n - i) - 1; j++) {
printf("*");
}
// Move to the next line
printf("\n");
// Increase space for next row
space++;
}
return 0;
}
include <stdio.h>
int main {
int n = 5;
int i, j;
// Print upper part of the pattern
for (i = 1; i <= n; i++) {
// Print asterisks followed by spaces
for (j = 1; j <= i; j++) {
printf("*");
}
printf(" "); // Print a space after each line
}
// Print lower part of the pattern
for (i = n - 1; i >= 1; i--) {
// Print asterisks followed by spaces
for (j = 1; j <= i; j++) {
printf("*");
}
printf(" "); // Print a space after each line
}
printf("\n"); // Move to the next line after the pattern is printed
return 0;
}
include<stdio.h>
int main
{
int i, j, n=5;
for(i=1;i<=n;i++)
{
for(j=n;j<=i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("* ");
printf(" ");
}
return 0;
}
include<stdio.h>
int main
{
int i, j, n=5;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("*");
printf(" ");
}
return 0;
}
Explanation:
The correct answer is option (a).
Option A is divided into two parts: the first forms the upper half of the diamond, and the second forms the lower half. The space variable controls the spaces before stars so that the pattern formed remains at the center.
4. What is the output of the following code?
include<stdio.h>
int main
{
int i, j, n=4;
for(i=1;i<=n;i++)
{
for(j=1;j<n;j++)
printf(" ");
for (j = 1; j <= (2*i-1); j++)
printf("*");
printf(" ");
}
return 0;
}
Explanation:
The correct answer is option (a).
The given code snippet prints a pyramid pattern of stars. The first nested loop prints the required spaces and the second one prints stars to make them centered.
5. What is wrong with the following code snippet to print an inverted pyramid of stars?
include<stdio.h>
int main
{
int i, j, n=5;
for(i=n;i>=1;i--)
{
for(j=i;j<n;j++)
printf(" ");
for (j = 1; j <= (2*i-1); j++)
printf("*");
printf("\n");
}
return 0;
}
- The condition for the outer loop should be i≤n.
- The condition for the inner loop should be j>n.
- The inner loop for spaces should be j > n-i.
- No error.
Explanation:
The correct answer is option (d).
This code snippet does not contain an error. It correctly prints an inverted pyramid of stars. The outer loop decreases the number of rows. The first inner loop manages the spaces to align the stars at the center. The second inner loop prints the stars.