Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

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

๐ŸŒ™