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...

C Pattern Program: Diagonal Number Pattern

C Pattern Program: Diagonal Number Pattern

๐Ÿ”ท C Pattern Program: Diagonal Number Pattern

#include <stdio.h>

int main()
{
    int num;
    printf("Enter the number:\n");
    scanf("%d", &num);
    
    for (int i = 0; i < num; i++)
    {
        for (int j = 0; j < num; j++)
        {
            if (j == i)
                printf("%d", i + 1);
            else if (j == num - 1 - i)
                printf("%d", num - i);
            else
                printf(" ");
        }
        printf("\n");
    }

    return 0;
}
  

๐Ÿ“˜ Explanation:

This C program prints a square pattern with two diagonal lines:

๐Ÿ”น On the **main diagonal** (from top-left to bottom-right), it prints i+1 (increasing number).
๐Ÿ”น On the **anti-diagonal** (from top-right to bottom-left), it prints num - i (decreasing number).
๐Ÿ”น All other positions are filled with spaces to maintain the structure.
๐Ÿ”น The result is a visually symmetric pattern based on the input size.

๐Ÿ” Sample Output:

Enter the number:
5
1   5
 2 4 
  3  
 2 4 
1   5
    

๐Ÿท️ Keywords:

C pattern printing, diagonal number pattern, matrix pattern in C, double diagonal number pattern, C loop-based programs

Comments

Popular Posts

๐ŸŒ™