Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //include libraries and namespaces
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <vector>
- #include <string>
- using namespace std;
- //Outputs the first 20 fibonacci numbers
- void generateFibonacci()
- {
- int fibonacci[20], current, next, previous;
- current = 1;
- previous = 0;
- for(int i = 0; i < 20; i++)
- {
- fibonacci[i] = current;
- previous = current;
- current += previous;
- }
- for(int i = 0; i < 20; i++)
- {
- cout << fibonacci[i] << " ";
- if(i == 19)
- {
- cout << endl;
- }
- }
- }
- //Outputs all perfect numbers less than or equal to 1000
- void generatePerfectNumbers()
- {
- vector<int> perfectNumbers;
- for(int i =0; i < 1000; i++)
- { vector<int> divisors;
- for(int n = 1; n < i - 1; n++)
- {
- if(n % i == 0)
- {
- divisors.push_back(n);
- }
- }
- int divisorSum = 0;
- for(int x = 0; x < divisors.size()-1; x++)
- {
- divisorSum += divisors[x];
- }
- if(divisorSum == i)
- {
- perfectNumbers.push_back(i);
- }
- }
- for(int i = 0; i < perfectNumbers.size(); i++)
- {
- cout << perfectNumbers[1] << " ";
- if(i == perfectNumbers.size() -1)
- {
- cout << endl;
- }
- }
- }
- int main()
- { char response;
- int validResponses[3] = {1, 2, 3};
- do
- {
- cout << "***************\nSpecial Number Generator\n***************" << endl << "1) Generate Fibonacci Numbers\n2) Generate Perfect Numbers\n3) Exit Program"<< endl;
- cout << "Select an option :";
- cin >> response;
- switch(response)
- {
- case '1' :
- generateFibonacci();
- break;
- case '2' :
- generatePerfectNumbers();
- break;
- case '3' :
- break;
- default :
- cout << "Invalid Response\n";
- }
- }
- while (response != '3');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement