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

Print Prime Numbers Between Two Numbers in C

Print Prime Numbers Between Two Numbers in C

✅ Print Prime Numbers Between Two Numbers in C

#include <stdio.h>

int main() {
    int m, n, i, j, isPrime;

    // Input range from user
    printf("Enter the starting number (m): ");
    scanf("%d", &m);

    printf("Enter the ending number (n): ");
    scanf("%d", &n);

    printf("Prime numbers between %d and %d are:\n", m, n);

    for(i = m; i <= n; i++) {
        if (i < 2) continue;

        isPrime = 1; // Assume prime

        for(j = 2; j <= i / 2; j++) {
            if(i % j == 0) {
                isPrime = 0;
                break;
            }
        }

        if(isPrime) {
            printf("%d ", i);
        }
    }

    printf("\n");
    return 0;
}
  

๐Ÿ“˜ Explanation:

✅ The program reads two integers m and n.
✅ It loops from m to n and checks each number for primality.
✅ For each number, it checks if it’s divisible by any number from 2 to i/2.
✅ If no divisors are found, it's a prime and printed.

๐Ÿงพ Sample Output:

Enter the starting number (m): 10
Enter the ending number (n): 25
Prime numbers between 10 and 25 are:
11 13 17 19 23
  

๐Ÿ”‘ Keywords:

C prime number program, prime between two numbers, C beginner practice, loop logic, prime check algorithm in C

๐Ÿ“Œ Hashtags:

#CProgramming #PrimeNumbers #LoopingInC #BeginnerC #ProgrammingBasics #InterviewPrep

Comments

Popular Posts

๐ŸŒ™