Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

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

๐ŸŒ™