Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 2nd, 2011, 05:56 AM
Registered User
 
Join Date: Oct 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Prime number

task is to idetify if the given K is prime or not.
Prime number is the number that can be divided by 1 and by itself ONLY.
If given number is prime output "YES",otherwise ouput "NO".

Input:
Only one line with n number.(1<=N=<1000)
Output:
Only one line with "YES" or "NO".
Sample input 1:
4

Sample output 1:
NO

Sample input 2:
7

Sample output 2:
YES
Reply With Quote
  #2 (permalink)  
Old October 15th, 2011, 06:17 AM
Registered User
 
Join Date: Sep 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default PRIME NUMBER?

Code:
int main()
{
	int n,flag=0;
	cout<<"Enter any number ";
	cin>>n;
	for(int i=2;i<n;i++)
	{
		if(n%i==0)
		{
			flag=1;
			break;
		}
	}

	if(flag==0 && n>1)
		cout<<"YES";
	else
		cout<<"NO";

	return 0;
}
http://www.cppforschool.com
Reply With Quote
  #3 (permalink)  
Old December 18th, 2011, 09:00 PM
Registered User
 
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Talking Something can be amended

Code:
#include <iostream>
#include <math>

using namespace std;

int main()
{
cout <<"Enter a number:";
int num;
boolean isPrime = true;
cin >>;
for (int i = 2; i <= sqrt(num); i++)
{
if (num % i == 0) 
{
isPrime = false;
break;
}
if (isPrime)
cout <<"YES"<<endl;
else
cout <<"NO<<endl;
return 0;
}
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Prime number program help daddymac1213 Assembly Language 2 January 10th, 2010 07:29 AM
How to add a line number/Row number- need to use it for page breaks naijacoder XSLT 7 September 27th, 2009 09:42 PM
prime numbers code error.. amahja56 C++ Programming 1 January 19th, 2005 01:33 PM
Prime Number Counting. Help!! ajm235 C++ Programming 3 August 27th, 2004 12:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.