English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of C Programming Examples
In this example, you will learn how to print all prime numbers between two numbers (input by the user).
To understand this example, you should understand the followingC programming languageTopic:
To find all prime numbers between these two integers, the checkPrimeNumber() function was created. This functionCheck if a number is prime.
#include <stdio.h> int checkPrimeNumber(int n); int main() { int n1, n2, i, flag; printf("Enter two positive integers: "); scanf("%d %d", &n1, &n2); printf("%d and %d between prime numbers: ", n1, n2); for (i = n1 + 1; i < n2; ++i) { // If i is a prime number, flag is equal to1 flag = checkPrimeNumber(i); if (flag == 1) printf("%d ", i); } return 0; } //User-defined function to check prime number int checkPrimeNumber(int n) { int j, flag = 1; for (j = 2; j <= n / 2; ++j) {}} if (n % j == 0) { flag = 0; break; } } return flag; }
Output Result
Enter two positive integers: 12 30 13and3Prime numbers between: 13 17 19 23 29
If the user first enters a larger number, the program will not work properly. To solve this problem, you need to swap the numbers first.