write the prime no program in c++?
Answers were Sorted based on User's Feedback
Answer / tuhin pal chowdhury
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
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;
getch();
}
Is This Answer Correct ? | 1 Yes | 2 No |
Answer / ashwin kanjariya
I accept the last function since it has less time complexity than all other functions........
Is This Answer Correct ? | 4 Yes | 7 No |
Answer / garima gupta
#include<iostream.h>
#include<conio.h>
#include<math.h>
class prime
{
private:
int i,j,j;
public:
void readn();
void display();
};
void prime::readn()
{
clrscr();
cout<<"enter no. of prime nos you want to display:"<<endl;
cin>>n;
}
void prime::display()
{
int flag;
for(i=2;i<n;i++)
{
flag=0;
for(j=2;j<=sqrt(i);j++)
{
if(i%j==0)
{
flag=1;break;
}
else(flag==0)
{
cout<<i<<" ";
}
}
}
}
void main()
{
prime p;
p.readn();
p.display();
getch();
}
Is This Answer Correct ? | 10 Yes | 18 No |
Answer / adnan sheikh
#include<iostream.h>
#include<conio.h>
main()
{
int i,j,num;
cout<<"enter nay number";
cin>>num;
j=static_cast<int>(num/2);
for(int i=2;i<=j;i++)
{
if(!(num%i))
break;
}
if(i==(j+1))
cout<<"number is prime";
else
cout<<"number is not prime";
getch();
}
Is This Answer Correct ? | 8 Yes | 19 No |
Answer / pramod k sharma
#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 ? | 14 Yes | 32 No |
Can you sort a set c++?
class HasStatic { static int I; }; Referring to the sample code above, what is the appropriate method of defining the member variable "I", and assigning it the value 10, outside of the class declaration? a) HasStatic I = 10; b) int static I = 10; c) static I(10); d) static I = 10; e) int HasStatic::I = 10;
What is implicit conversion/coercion in c++?
why and when we can declar member fuction as a private in the class?
What are vectors used for in c++?
Distinguish between new and malloc and delete and free().
Explain what happens when a pointer is deleted twice?
Differences between private, protected and public and give examples.
What is setiosflags c++?
Why is c++ a mid-level programming language?
What are the advantages of using friend classes?
What do you mean by global variables?