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 Separate Even and Odd Numbers from an Array

๐Ÿ”ข C Program to Separate Even and Odd Numbers from an Array

#include<stdio.h>
int main()
{
    int num;
    printf("Enter the size of the array:\n");
    scanf("%d",&num);
    if(num<=0)
    {
        printf("!invalid array size:\n");
        return 1;
    }
    int a[num],even[num],odd[num];
    int evencount=0,oddcount=0;
    printf("Enter %d Elements in an array:\n",num);
    for(int i=0;i<num;i++)
    {
        scanf("%d",&a[i]);
        
        if(a[i]%2==0)
        {
            even[evencount++]=a[i];
        }
        else
        {
            odd[oddcount++]=a[i];
        }
    }
    printf("Even numbers are:\n");
    for(int i=0;i<evencount;i++)
    {
        printf("%d ",even[i]);
    }
    printf("\nOdd numbers are:");
    for(int i=0;i<oddcount;i++)
    {
        printf("%d ",odd[i]);
    }
}
  

๐Ÿ“ Explanation:

This C program reads n integers into an array and separates them into two arrays: one for even numbers and another for odd numbers. It then displays them separately.

๐Ÿ’ก Sample Output:

Enter the size of the array:
6
Enter 6 Elements in an array:
1 2 3 4 5 6
Even numbers are:
2 4 6 
Odd numbers are:
1 3 5
  

๐Ÿ” Keywords:

even odd separation C program, split even and odd, C array even odd, separate numbers C, odd even logic in C

Comments

Popular Posts

๐ŸŒ™