Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

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

Popular Posts

πŸŒ™