Prime number program in c language
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i = 3, count, c;
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
getch();
}
C program for check number is prime number or not
#include<stdio.h>
#include<conio.h>
void main()
{
int n, c = 2;
printf("Enter a number to check if it is prime\n");
scanf("%d",&n);
for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
printf("%d is prime.\n", n);
getch();
}
C program for prime number using function
#include<stdio.h>
#include<conio.h>
int check_prime(int);
void main()
{
int n, result;
printf("Enter an integer to check whether it is prime or not.\n");
scanf("%d",&n);
result = check_prime(n);
if ( result == 1 )
printf("%d is prime.\n", n);
else
printf("%d is not prime.\n", n);
getch();
}
int check_prime(int a)
{
int x;
for ( x = 2;x <= a - 1; x++ )
{
if ( a%x == 0 )
return 0;
}
if ( x == a )
return 1;
}
No comments:
Post a Comment