write the prime no program in c++?

Answers were Sorted based on User's Feedback



write the prime no program in c++?..

Answer / md.irfan(rourkela)

#include<iostream.h>
#include<conio.h>
main()
{
int n,i,f=1;
cout<<"enter any no to check prime";
cin>>n;
for(i=2;i<n;i++)
{
if(n%i==0)
k=2;
}
if(k==2)
cout<<"the no is not prime"<<endl;
else
cout<<"the no is prime";
}
getch();
}

Is This Answer Correct ?    145 Yes 91 No

write the prime no program in c++?..

Answer / swapnil

/*To check whethere Entered no. is PRIME or NOT*/
#include<iostream.h>
#include<conio.h>
main()
{
int num,i;
clrscr();
cout<<"Enter the any no.="<<"\n";
cin>>num;
for(i=2;i<=num;i++)
{
if(num%i==0)
{
break;
}
}
if(i==num)
{
cout<<"The number entered is a
PRIME no.";
}
else
{
cout<<"The number entered is NOT a
PRIME no.";
}
getch();
}

Is This Answer Correct ?    74 Yes 41 No

write the prime no program in c++?..

Answer / indu

#include<stdio.h>
#include<conio.h>
main()
{
int n,s=0,i;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
for(i=2;i<n/2;i++)
{
if(n%i==0)
s++;
}
if(s>=1)
printf("%d is not prime",n);
else
printf("%d is prime",n);
getch();
}
ill execute it .it's cent percent correct.

Is This Answer Correct ?    40 Yes 22 No

write the prime no program in c++?..

Answer / prits

#include <iostream>
using namespace std;

void main()
{
int num;
int flag = 0;
cout<<"enter a number"<<endl;
cin>>num;
for(int i=2;i<num;i++)
{
if((num%i) == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
cout<<"Not Prime"<<endl;
else
cout<<"Prime"<<endl;

}

Is This Answer Correct ?    32 Yes 15 No

write the prime no program in c++?..

Answer / darren chang

bool checkPrime(int input)
{
for(int i=2; i<(input/2); i++)
{
if((input%i)==0)
{
return false;
}
}
return true;
}

Is This Answer Correct ?    14 Yes 7 No

write the prime no program in c++?..

Answer / daljeet singh

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
char prime;
prime='Y';
cout<<"\n enter a number";
cin>>n;
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
prime='N';
break;
}
}
if(n==1)
prime='N';
if(prime=='Y')
cout<<"\n entered number is prime";
else
cout<<"\n not a prime no";
getch();
}

Is This Answer Correct ?    10 Yes 3 No

write the prime no program in c++?..

Answer / rajan maheshwari

#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a,c=0,i=2;
cout<<"Enter A Number = ";
cin>>a;
while(i<=a/2)
{
if(a%i==0)
{c=0;
break;}
else
{i++;
c=1;
}}
if(c==1||a==2||a==3)
cout<<"\nPrime Number";
else
if(a==1)
cout<<"\nNeither Prime Nor Composite";
else
cout<<"\nNot a Prime Number";
getch();
}

Is This Answer Correct ?    3 Yes 0 No

write the prime no program in c++?..

Answer / suresh kumar

#include<iostream.h>
#include<conio.h>
void main()
{
int n,k,i=1;
cout<<"enter any no to check prime";
cin>>n;
for(i=2;i<n;i++)
{
if(n%i==0)
k=2;
}
if(k==2)
cout<<"the no is not prime"<<endl;
else
cout<<"the no is prime";
getch();
}

Is This Answer Correct ?    4 Yes 1 No

write the prime no program in c++?..

Answer / waleed

#include<iostream.h>
#include<conio.h>

int prime(int);

void main()
{
clrscr();
int num,x;
cout<<"Enter the no.=";
cin>>num;
x=prime(num);
if(x==1)
{
cout<<"Number is prime";
}
else
{
cout<<"Number is not prime";
}

getch();
}

int prime(int x)
{
int k;
for(int i=2;i<x;i++)
{
if (x%i==0)
{
k=2;
}
}
if (k==2)
return 0;
else
return 1;


}

Is This Answer Correct ?    2 Yes 0 No

write the prime no program in c++?..

Answer / prateek

I found some good and new ways to write this programe.
Thankx to all. Well even I have also tried to make this
programe. By making use of "while loop";

#include<iostream>
#include<conio.h>

using namespace std;

void main(){

int num,n,i=0,flag=0;;
cout<<"Enter the number";
cin>>num;

n=num/2;
while(i<n)
{
++i;
if(num%i==0 && i!=1)
{
flag=1;
break;
}
else
{
flag;
}
}
if(flag)
{
cout<<"The number is not a prime number";
}
else
{
cout<<"The number is a prime number";
}

getch();


}

Plz notice, that I have divided the number by 2. Suppose
user input 42. The highest divisible value of 42 will be
its half, i.e., 21(21*2=42). So there is no need to check
the loop condition until the value of 'i' reach num. Becoz
it is for sure, that the values more than its half are not
divisible. This will increase the efficiency of the
programe.

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More C++ General Interview Questions

What are member functions used in c++?

0 Answers  


What is cloning?

1 Answers  


What is the fastest c++ compiler?

0 Answers  


What is c++ namespace?

0 Answers  


How to construct muliton object

2 Answers   Symphony, TCS,






Is it possible to have a recursive inline function in c++?

0 Answers  


What are the extraction and insertion operators in c++? Explain with examples.

0 Answers  


What is #include iostream in c++?

0 Answers  


Can we make copy constructor private in c++?

0 Answers  


What do you mean by funtion prototype?

0 Answers  


What is null pointer and void pointer?

0 Answers  


Is it possible to write a c++ template to check for a function's existence?

0 Answers  


Categories