The issue in the nested for loop for variable j. You are using the condition i <= num instead of j <= num. This causes the inner loop to behave incorrectly, leading to unexpected output.
the corrected version:
#include <stdio.h>
int main()
{
int num, i, j;
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
for (j = 1; j <= num; j++) // Corrected condition here
{
if (i == 1 j == 1 i == num || j == num)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}