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 Program to Reverse an Array

 

๐Ÿ” C Program to Reverse an Array

#include<stdio.h>
int main()
{
    int num, a[100];
    printf("Enter size of the array:\n");
    scanf("%d", &num);
    if(num <= 0)
    {
        printf("!invalid Array Size:\n");
        return 1;
    }
    printf("Enter %d elements:\n", num);
    for(int i = 0; i < num; i++)
    {
        scanf("%d", &a[i]);
    }
    printf("\nBefore reverse:\n");
    for(int i = 0; i < num; i++)
    {
        printf("%d ", a[i]);
    }
    printf("\nAfter reverse:\n");
    for(int i = num - 1; i >= 0; i--)
    {
        printf("%d ", a[i]);
    }
}
  

๐Ÿ“ Explanation:

This program takes an array of integers from the user, displays the original array, and then prints the elements in reverse order. It uses a loop to first store the input values, and then another loop from the end of the array to the beginning for reverse display.

๐Ÿ’ก Sample Output:

Enter size of the array:
5
Enter 5 elements:
10 20 30 40 50

Before reverse:
10 20 30 40 50
After reverse:
50 40 30 20 10
  

๐Ÿ” Keywords:

Reverse array in C, C program for reverse, loop reverse logic, array operations in C, beginner array program

Comments

Popular Posts

๐ŸŒ™