Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Write a C program to print all the prime numbers between 1 to 50.

Here’s a C program to print all prime numbers from 1 to 50:

#include <stdio.h>

int main() {
    int num, i, isPrime;

    printf("Prime numbers between 1 and 50 are:\n");

    // loop through numbers 1 to 50
    for(num=2; num<=50; num++) {
        isPrime = 1; // assume num is prime

        // check if num is divisible by any number from 2 to num-1
        for(i=2; i<num; i++) {
            if(num % i == 0) {
                isPrime = 0; // num is not prime
                break;
            }
        }

        // if num is prime, print it
        if(isPrime == 1) {
            printf("%d ", num);
        }
    }

    return 0;
}

Output:

Prime numbers between 1 and 50 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 

In this program, we use two loops. The outer loop goes from 2 to 50 and the inner loop checks if each number is prime or not by dividing it by all numbers from 2 to num-1. If num is not divisible by any of these numbers, then it is prime and we print it.