0% found this document useful (0 votes)
64 views

C Program To Display Prime Numbers Between Two Intervals

This C program takes in two numbers as input, representing a range of numbers. It then prints out all the prime numbers within that range. It uses a while loop to iterate from the low to high number, and inside each loop it tests if the current number is prime using a for loop to check for factors from 2 to the number/2, setting a flag if any factors are found. Prime numbers are printed out.

Uploaded by

Aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

C Program To Display Prime Numbers Between Two Intervals

This C program takes in two numbers as input, representing a range of numbers. It then prints out all the prime numbers within that range. It uses a while loop to iterate from the low to high number, and inside each loop it tests if the current number is prime using a for loop to check for factors from 2 to the number/2, setting a flag if any factors are found. Prime numbers are printed out.

Uploaded by

Aman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>

int main() {
int low, high, i, flag;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Prime numbers between %d and %d are: ", low, high);

// iteration until low is not equal to high


while (low < high) {
flag = 0;

// ignore numbers less than 2


if (low <= 1) {
++low;
continue;
}

// if low is a non-prime number, flag will be 1


for (i = 2; i <= low / 2; ++i) {

if (low % i == 0) {
flag = 1;
break;
}
}

if (flag == 0)
printf("%d ", low);

// to check prime for the next number


// increase low by 1
++low;
}

return 0;
}
Output

Enter two numbers(intervals): 20


50
Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47

You might also like