Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary
π’ C Program to Print Hollow Continuous Number Square
π Source Code:
#include <stdio.h>
int main()
{
int num, k = 0;
printf("Enter the number:\n");
scanf("%d", &num);
for(int i = 1; i <= num; i++)
{
for(int j = 1; j <= num; j++)
{
if(i == 1 || i == num || j == 1 || j == num)
{
// k increments sequentially only along the borders
printf("%d ", k++);
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
π» Expected Output (Input: 5):
Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
π’ C Program to Print Standard Hollow Binary Row Square
π Source Code (Fixed Specifier):
#include <stdio.h>
int main()
{
int num;
printf("Enter the number:\n");
scanf("%d", &num);
for(int i = 1; i <= num; i++)
{
for(int j = 1; j <= num; j++)
{
if(i == 1 || i == num || j == 1 || j == num)
{
// Fixed: Changed %c to %d to correctly display the binary digits
printf("%d ", i % 2);
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
π» Expected Output (Input: 5):
Enter the number: 5 1 1 1 1 1 0 0 1 1 0 0 1 1 1 1 1
π’ C Program to Print Standard Hollow Binary Column Square
π Source Code (Fixed Specifier):
#include <stdio.h>
int main()
{
int num;
printf("Enter the number:\n");
scanf("%d", &num);
for(int i = 1; i <= num; i++)
{
for(int j = 1; j <= num; j++)
{
if(i == 1 || i == num || j == 1 || j == num)
{
// Fixed: Changed %c to %d to correctly display the binary digits
printf("%d ", j % 2);
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
π» Expected Output (Input: 5):
Enter the number: 5 1 0 1 0 1 1 1 1 1 1 1 1 0 1 0 1
π’ C Program to Print Inverted Hollow Row-Alphabet Square
π Source Code:
#include <stdio.h>
int main()
{
int num;
printf("Enter the number:\n");
scanf("%d", &num);
for(int i = num; i >= 1; i--)
{
for(int j = num; j >= 1; j--)
{
if(i == 1 || i == num || j == 1 || j == num)
{
// Prints character based on the inverted row index (i)
printf("%c ", i + 64);
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
π» Expected Output (Input: 5):
Enter the number: 5 E E E E E D D C C B B A A A A A
π’ C Program to Print Inverted Hollow Alphabet Square Pattern
π Source Code:
#include <stdio.h>
int main()
{
int num;
printf("Enter the number:\n");
scanf("%d", &num);
for(int i = num; i >= 1; i--)
{
for(int j = num; j >= 1; j--)
{
if(i == 1 || i == num || j == 1 || j == num)
{
printf("%c ", j + 64);
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
π» Expected Output (Input: 5):
Enter the number: 5 E D C B A E A E A E A E D C B A
π’ C Program to Print Hollow Star Square Pattern
π Source Code:
#include <stdio.h>
int main()
{
int num;
printf("Enter the number:\n");
scanf("%d", &num);
for(int i = 1; i <= num; i++)
{
for(int j = 1; j <= num; j++)
{
if(i == 1 || i == num || j == 1 || j == num)
{
printf("* ");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
π» Expected Output (Input: 5):
Enter the number: 5 * * * * * * * * * * * * * * * *
π’ C Program to Print Hollow Row-Alphabet Square Pattern
π Source Code:
#include <stdio.h>
int main()
{
int num;
printf("Enter the number:\n");
scanf("%d", &num);
for(int i = 1; i <= num; i++)
{
for(int j = 1; j <= num; j++)
{
if(i == 1 || i == num || j == 1 || j == num)
{
// Prints character based on row index (i)
printf("%c ", i + 64);
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
π» Expected Output (Input: 5):
Enter the number: 5 A A A A A B B C C D D E E E E E
π’ C Program to Print Hollow Alphabet Square Pattern
π Source Code:
#include <stdio.h>
int main()
{
int num;
printf("Enter the number:\n");
scanf("%d", &num);
for(int i = 1; i <= num; i++)
{
for(int j = 1; j <= num; j++)
{
if(i == 1 || i == num || j == 1 || j == num)
{
printf("%c ", j + 64);
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
π» Expected Output (Input: 5):
Enter the number: 5 A B C D E A E A E A E A B C D E
π’ C Program to Print Inverted Hollow Number Square Pattern
π Source Code:
#include <stdio.h>
int main()
{
int num;
printf("Enter the number:\n");
scanf("%d", &num);
for(int i = num; i >= 1; i--)
{
for(int j = num; j >= 1; j--)
{
if(i == 1 || i == num || j == 1 || j == num)
{
printf("%d ", j);
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
π» Expected Output (Input: 5):
Enter the number: 5 5 4 3 2 1 5 1 5 1 5 1 5 4 3 2 1
π’ C Program to Print Hollow Number Square Pattern
π Source Code:
#include <stdio.h>
int main()
{
int num;
printf("Enter the number:\n");
scanf("%d", &num);
for(int i = 1; i <= num; i++)
{
for(int j = 1; j <= num; j++)
{
if(i == 1 || i == num || j == 1 || j == num)
{
printf("%d ", j);
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
π» Expected Output (Input: 5):
Enter the number: 5 1 2 3 4 5 1 5 1 5 1 5 1 2 3 4 5
Comments
Post a Comment