Advertisement
Guest User

BPA C++ Competition Practice

a guest
Mar 9th, 2023
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | Source Code | 0 0
  1. //include libraries and namespaces
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <vector>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. //Outputs the first 20 fibonacci numbers
  12. void generateFibonacci()
  13. {
  14.     int fibonacci[20], current, next, previous;
  15.     current = 1;
  16.     previous = 0;
  17.     for(int i = 0; i < 20; i++)
  18.     {  
  19.         fibonacci[i] = current;
  20.         previous = current;
  21.         current += previous;
  22.     }
  23.     for(int i = 0; i < 20; i++)
  24.     {
  25.         cout << fibonacci[i] << " ";
  26.         if(i == 19)
  27.         {
  28.             cout << endl;
  29.         }
  30.     }
  31.  
  32.    
  33. }
  34. //Outputs all perfect numbers less than or equal to 1000
  35. void generatePerfectNumbers()
  36. {
  37.     vector<int> perfectNumbers;
  38.     for(int i =0; i < 1000; i++)
  39.     {   vector<int> divisors;
  40.         for(int n = 1; n < i - 1; n++)
  41.         {
  42.             if(n % i == 0)
  43.             {
  44.                 divisors.push_back(n);
  45.             }
  46.         }
  47.         int divisorSum = 0;
  48.         for(int x = 0; x < divisors.size()-1; x++)
  49.         {
  50.             divisorSum += divisors[x];
  51.         }
  52.         if(divisorSum == i)
  53.         {
  54.             perfectNumbers.push_back(i);
  55.         }
  56.     }
  57.     for(int i = 0; i < perfectNumbers.size(); i++)
  58.     {
  59.         cout << perfectNumbers[1] << " ";
  60.         if(i == perfectNumbers.size() -1)
  61.         {
  62.             cout << endl;
  63.         }
  64.     }
  65. }
  66.  
  67. int main()
  68. {   char response;
  69.     int validResponses[3] = {1, 2, 3};
  70.     do
  71.     {
  72.         cout << "***************\nSpecial Number Generator\n***************" << endl << "1) Generate Fibonacci Numbers\n2) Generate Perfect Numbers\n3) Exit Program"<< endl;
  73.        
  74.        
  75.         cout << "Select an option :";
  76.         cin >> response;
  77.         switch(response)
  78.         {
  79.             case '1' :
  80.                 generateFibonacci();
  81.                 break;
  82.            
  83.             case '2' :
  84.                 generatePerfectNumbers();
  85.                 break;
  86.            
  87.             case '3' :
  88.                 break;
  89.            
  90.             default :
  91.                 cout << "Invalid Response\n";
  92.            
  93.         }
  94.        
  95.     }
  96.     while (response != '3');
  97.    
  98.     return 0;
  99.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement