Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

write the prime no program in c++?

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


Please Help Members By Posting Answers For Below Questions

What is a base class?

1081


What is c++ good for?

1032


Snake Game: This is normal snake game which you can find in most of the mobiles. You can develop it in Java, C/C++, C# or what ever language you know.

2977


Explain abstraction.

1064


What is singleton class in c++?

1015


Which operations are permitted on pointers?

964


What are put and get pointers?

1083


Is c++ the best programming language?

987


Will a C compiler always compile C++ code a) Yes b) No c) Only optimized compilers

1020


write a porgram in c++ that reads an integer and print the biggest digit in the number

2215


What is data hiding c++?

1014


How does class accomplish data hiding in c++?

1139


Difference between overloaded functions and overridden functions

1026


Explain what are mutator methods in c++?

1088


Define copy constructor.

1074