C Program to generate prime numbers up to user given Number.
Description :
This program accepts one integer number as input and generates prime numbers up to that number.
This program is one the program in the series of prime number generation programs, my previous programs are here.
- What is Prime Number and C program to Check given number is Prime or Not
- Check given Number is Prime or not Using Square Root(sqrt) Function.(Efficient way)
- C Program to generate prime numbers between two numbers
- C Program to generate first N prime numbers.
Program :
-
#include<stdio.h>
-
int main()
-
{
-
int i,j,cnt,num;
-
LABEL:
-
printf(“Enter Value :”);
-
scanf(“%d”, &num);
-
/* let’s test given number is above 1
-
* and number must non Negative number
- */
-
if(num < 2)
-
{
-
printf(“Please enter valid Number n “);
-
goto LABEL;
-
}
-
for(i=2; i<= num ; i++)
-
{ // this loop for iterating from 2 to given number
-
cnt = 0;
-
for(j=2; j<=i/2; j++)
-
{ // testing i is prime or not..
-
if(i%j == 0)
-
{
-
cnt++;
-
break;
-
}
-
}
-
if(cnt == 0)
-
{ // i is prime..
-
printf(“%d “,i);
-
}
-
}
-
printf(“n“);
-
return 0;
-
}
Output:
Related programs:
- C program to Calculate percentage of student.
- C Program to check given year is the leap or not.
- C Program to convert Temparatuere.
- C program to understand type conversation.
- Finding Largest of two numbers using the conditional operator in C.
- C program to calculate the simple Interest,
- C program to understand Size of Operator