What is a Prime Number?
” A Natural number greater than 1 which has only two divisor 1 and itself is called prime number “.
For Example:
5 is prime, because it has only two divisors 1 and itself.
C Code
#include <stdio.h> #include <math.h> #include <conio.h> void main() { int startNum,endNum; int total=0; printf("Enter Number START of Range: "); scanf("%d", startNum); printf("Enter Number END of Range: "); scanf("%d", endNum); for(int i=startNum;i<=endNum;i++) { int count=0; for(int j=2;j<=sqrt(i);j++) { if(i%j==0) { count++; break; } } if(count==0 && i>1) { total++; printf("Prime Number -> %d\n", i); } } printf("Total Prime Number Between %d to %d = %d\n", startNum, endNum,total); getch(); }
C++ Code
#include<iostream> #include<math.h> using namespace std; int main() { int startNum,endNum; int total=0; cout<<"Enter Number START of Range: "; cin>>startNum; cout<<"Enter Number END of Range: "; cin>>endNum; for(int i=startNum;i<=endNum;i++) { int count=0; for(int j=2;j<=sqrt(i);j++) { if(i%j==0) { count++; break; } } if(count==0 && i>1) { total++; cout << "Prime Number -> " << i << endl; } } cout<<"Total Prime Number Between " << startNum << " to " << endNum << " = " << total << endl; return 0; }
Output:
Other Related Search Terms
- Code For Prime Number In C++
- Code For Prime Number In C
- C++ Programs to find Prime number
- C Programs to find Prime number
- Find all Prime numbers between 1 to 100
2 Comments
#include
#include
void main(void){
clrscr();
int flag=0,num;
printf(“Enter range = “);
scanf(“%d”,&num);
for(int i=2;i<=num;i++)
{
for(int j=2;j<=i/2;j++)
{
if(i%j==0){
flag++;break;}
}
if(flag==0){
printf(" %d ",i);
}
flag=0;
}
getch();
}
#include
#include
main ()
{
int up,low,i,j,sum,flag,pnum;
pnum=0;
sum=0;
flag=0;
cout<>low;
cout<>up;
//for inputting the vlues again and again till user input all the values true..
while(lowup)
{
if(low<2)
{
cout<<"THE lower value must be grater then 1\n";
cout<>low;
cout<>up;
}
else
{
cout<<"THe Uper limit must be grater than the lower\n";
cout<>low;
cout<>up;
}
}
cout<<"\n THe prime numbers B/W the Range are: \n\n ";
//findin the prime number…
for(i=low; i<=up; i++)
{
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
flag++;
break;
}
}
if(flag==0)
{
cout<<i<<"\t";
//counting the total prime numbers within the range…
pnum++;
// calculatting the sum of all prime numbers within the range..
sum += i;
}
flag=0;
}
cout<<"\n\n\nTotal number of Prime number B/W the range are: "<<pnum;
cout<<"\n\n\nTHe sum of all Prime numbers B/W the range is: "<<sum;
getche();
}