Answer Posted / 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 View All Answers
Why do we need c++?
How one would use switch in a program?
What is a breakpoint?
Why #include is used?
What is &x in c++?
Write a program using shift_half( ) function to shift the elements of first half array to second half and vice versa.
Which command properly allocates memory a) char *a=new char[20]; b) char a=new char[20]; c) char a=new char(20.0);
Why should you learn c++?
What is endl c++?
What is a static element?
What is the type of 'this' pointer?
What is the importance of mutable keyword?
A mXn matrix is given and rows and column are sorted as shown below.Write a function that search a desired entered no in the matrix .with minimum complexity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
What are the general quetions are in DEna bank manager IT/System interviews?
Write down the equivalent pointer expression for referring the same element a[i][j][k][l]?