This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 10th, 2011  |  syntax: None  |  size: 3.77 KB  |  views: 845  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //goldemerald's prime numbers program
  2. //i do not know how to use binary or new line stuff :P
  3.  
  4. // Required if your program does any I/O
  5. #include <iostream>                
  6. #include <iomanip>
  7. #include <string>
  8. #include <cstring>
  9. // Required for ANSI C++ 1998 standard.
  10. using namespace std;
  11.  
  12. void decision();
  13. void myNumber();
  14. void morePrimeNumbers(int &count);
  15. void reset(int &count);
  16.  
  17. int main()
  18. {
  19.     cout << "goldemerald's prime number program" << endl;
  20.     decision();
  21.     system("pause");
  22.     return 0;
  23. }
  24.  
  25. void decision()
  26. {
  27.     char decide;
  28.     int count = 1;
  29.  
  30.     cout << "What would you like to do: " << endl;
  31.     cout << "See if a number is a prime number, press 'A'; See if numbers are prime in a row, press 'B'; Reset seen prime numbers to 0, press 'C'; Or press 'Q' to quit: ";
  32.     cin >> decide;
  33.     while (decide != 'Q')
  34.     {
  35.         if (decide == 'A')
  36.             myNumber();
  37.         if (decide == 'B')
  38.             morePrimeNumbers(count);
  39.         if (decide == 'C')
  40.             reset(count);
  41.         cout << "See if a number is a prime number, press 'A'; See if numbers are prime in a row, press 'B'; Reset seen prime numbers to 0, press 'C'; Or press 'Q' to quit: ";
  42.     }
  43. }
  44.  
  45. void myNumber()
  46. {
  47.     int number;
  48.     int increase = 1;
  49.     double x;
  50.     string answer;
  51.     answer = "yes";
  52.  
  53.     cout << "What is the number you want to check if it's a prime number?" << endl;
  54.     cin >> number;
  55.  
  56.     while (increase < number)
  57.     {
  58.         //ensures that 1 is not used for number or increase
  59.         x = number/increase;
  60.         //sees if the number has a remainder, if no, the number is not prime
  61.         if ((number%increase) == 0 && x != 1 && x != number)
  62.             answer = "no";
  63.         //tells the user their number is not prime
  64.         if (answer == "no")
  65.         {
  66.             cout << answer << " it is not a prime number!" << endl;
  67.             return;
  68.         }
  69.         //increases the divisor by 1 to see if the next integer increased will have a remainder
  70.         increase++;
  71.     }
  72.     //if the number always has a remainder, it tells the user it's a prime number
  73.     cout << answer << " it is a factor!" << endl;
  74. }
  75. void morePrimeNumbers(int &count)
  76. {
  77.     int x;
  78.     int y;
  79.     int increase = 1;
  80.     //j is a binary set to false, but i dont know how to use those :P
  81.     int j = 0;
  82.     int input;
  83.  
  84.     cout << "How many (more) prime numbers would you like to see? ";
  85.     cin >> input;
  86.  
  87.     //sets x to be the number of factors to be seen to the users input plus, starting from 1 or the last place left off
  88.     x=count + input;
  89.     cout << "Here are the next " << input << " prime numbers:" << endl;
  90.     //a loop to show the numbers from start to the input number
  91.     while (count < x)
  92.     {
  93.         //the increasing number that will be dividing the next number(count) to see if there is a remainder
  94.         while (increase < count && j != 1)
  95.         {
  96.             y = count/increase;
  97.             //if the number can be divided, it tells the user
  98.             if (count%increase == 0 && y != 1 && y != count)
  99.             {
  100.                 cout << count << "is not a factor!" << endl;
  101.                 //sets the binary number to true for later use
  102.                 j = 1;
  103.             }
  104.             //increases the divisor to see if the next number up can divide it
  105.             increase++;
  106.         }
  107.         //resets the divisor
  108.         increase = 1;
  109.  
  110.         //if the binary number is false, tell the user the number is a prime number
  111.         if (j==0)
  112.             cout << count << "is a factor!" << endl;
  113.         //resets the binary number to false
  114.         j=0;
  115.         //now we move up to the next number
  116.         count++;
  117.     }
  118. }
  119. void reset(int &count)
  120. {
  121.     count = 1;
  122.     cout << "It has been reset to 1!" << endl;
  123. }
  124.  
clone this paste RAW Paste Data