C Program to Print Prime Numbers Between 1 to n using For Loop

Write a Program to Print Prime Numbers Between 1 to n using For Loop

#include <stdio.h>
#include <stdbool.h>

bool isPrime(int n)
{
	if(n == 1 || n ==0)
		return false;


	int i = 2;
	for(i = 2; i < n; i++)
	{
		if(n % i == 0)
		{
			return false;
		}
	}

	return true;
}

int main()
{
	int n, i;

	printf("Enter the number :--> ");
	scanf("%d", &n);

	printf("Prime numbers between 1 to %d :--> ", n);

	for(i = 1; i < n; i++)
	{
		if(isPrime(i))
		{
			printf("%d ",i);
		}
	}
	return 0;
}

Output:

Enter the number :--> 25
The prime numbers are :2 3 5 7 11 13 17 19 23

≪ Previous | Next ≫