repl.it
@justingraham/

Regional 2016 - Generating Fibonacci/Perfect

C++

No description

fork
loading
Files
  • main.cpp
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//NOTE TO GRADER:
//This is just a sample source code. Contestant code may differ.
//****************************************************************
//MAIN
//****************************************************************
// Regional2015FibonacciPerfectNumbers.cpp
//This program displays a menu to generate Fibonacci numbers and perfect numbers.
#include <iostream>
using namespace std;

void generateFibonacciNumbers();
void generatePerfectNumbers();

int main()
{
	int option; // user's entered option will be saved in this variable
	do //do-while loop starts here.that display menu again and again until user select to exit program
	{
		//Displaying Options for the menu
		cout << "***********************************\n";
		cout << " Special Number Generator\n";
		cout << "***********************************\n";
		cout << " 1) Calculate Fibonacci Numbers " << endl;
		cout << " 2) Calculate Perfect Numbers " << endl;
		cout << " 3) Exit Program " << endl;
		//Prompting user to enter an option according to menu
		cout << " Please select an option : ";
		cin >> option; // taking option value as input and saving in variable "option"
		if(option == 1) // Checking if user selected option 1
		{
			//Call Fibonacci Method
			generateFibonacciNumbers();
		}
		else if(option == 2) // Checking if user selected option 2
		{
			//Call Perfect Number Method
			generatePerfectNumbers();
		}
		else if(option == 3) // Checking if user selected option 4
		{
			cout << "Terminating Program" << endl;
		}
		else //if user has entered invalid choice (other than 1,2,3 or 4)
		{
			//Displaying error message
			cout << "Invalid Option entered" << endl;
		}
	}
	
	while(option != 3); //condition of do-while loop
	
	return 0;
	}
	
void generateFibonacciNumbers()
{
	cout << "\n";
	//Calculate and output the first 20 Fibonacci numbers. (Assume it starts with 1).
	int max, current, first = 1, second = 1;
	double next;
	max = 20;
	for (current = 0 ; current < max ; current++)
	{
		if (current <= 1)
		{
			next = 1;
		}
		else
		{
			next = first + second;
			first = second;
			second = next;
		}
	cout << " Fibonacci # " << current + 1 << " is: " << next << endl;
	}
	
	string menu; 
	cout << "\nEnter anything in to continue . . .";
	cin  >> menu;
}

void generatePerfectNumbers()
{
	int i = 1, sum = 0;
	cout << "\n";
	cout << " Here are all the perfect numbers less than 1000.\n";
	//Output all perfect numbers less than 1000.
	for (int num = 1; num < 1000; num++)
	{
		while(i < num)
		{
			//Check for factors of current number, if factor is found, add it tothe sum
			if(num % i == 0)
			{
				sum = sum + i;
			}
			i++;
		}
		if(sum == num)
		{
			cout << " " << num << endl;
		}
		i = 1; //reset i
		sum = 0; //reset sum to check next number
	}
	string menu; 
	cout << "\nEnter anything in to continue . . .";
	cin  >> menu;
}
Fetching token
?