User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 374,572 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,817 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Jan 2nd, 2008
Views: 1,842
This program takes a parameter, which is how many prime numbers you need. However if you want to make a program which checks whether a number is prime or not you can delete everything in the main function and put this line instead:

cout << isPrime (argv [1]) ? "It's a prime." : "It's not a prime."

Hope you find this useful, if it works correct .
Last edited : Jan 5th, 2008.
cplusplus Syntax
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. bool isPrime (float number)
  7. {
  8. if (number == 1.0f) {
  9. return true;
  10. }
  11. for (float i=2.0f; i<number; i+=1.0f) {
  12. if (((number/i) - (int)(number/i)) == 0)
  13. return false;
  14. }
  15. return true;
  16. }
  17.  
  18. int main (int argc, char** argv)
  19. {
  20. cout << "Hello its me the prime number god!, I will give you " << argv [1] << " Prime Numbers like you requested.\n";
  21. int nums =0;
  22. while (nums != atoi (argv [1])){
  23. static int i =0;
  24. if (isPrime (i)){
  25. cout << "The number : " << nums << " is : " << i << endl;
  26. i++;
  27. nums++;
  28. }else
  29. i++;
  30. }
  31. }
Comments (Newest First)
cscgal | The Queen of DaniWeb | Jan 5th, 2008
Moved
darkscript | Newbie Poster | Jan 2nd, 2008
Sorry everybody, I chose C syntax instead of C++.
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:53 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC