Hello everyone! I am taking my first c++ class and we're on our last assigns of the semester. I have to write a program which repeatedly reads an integer between 1 and 1000, prints the following menu and reads the user's choice until the user chooses to quit, like so:
Enter an integer between 1 and 1000: 99
Choose one of the following:
1. Display all Fibonacci numbers up to 99
2. Display all prime numbers up to 99
3. Display all Fibonacci numbers that are also prime numbers up to 99
4. Display 5 randomly generated prime numbers between 1 and 1000
5. Quit.
Enter your choice:
I dont have the exact ideo on how to get through my choices 1 - 4!How to display the fibonaccis until number entered, prime numbers etc
this is what i got so far.....
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int integer, choice, fn;
int rn1 = 1 + rand() % 1000;
srand(unsigned(time(0)));
do
{
cout << "Enter an Integer between 1 and 1000: ";
cin >> integer;
cout << endl;
Please use the code tags around your code, so it's easier to read.
Here are a few hints that I see.
- you'll need to #include <cstdlib> in order to use the srand psudo-random functions.
- A Fibonacci number is one that belongs to the Fibonacci's sequence: 0, 1, 1, 2, 3, 5, 8, 13... http://en.wikipedia.org/wiki/Fibonacci_number
Right now your code prints out all integers up to the given number
- You don't need any break; statements in your if else statements, but this reminds me of the do-while-switch menu style that is so popular with homework assignments. Here is a sample of a simple do-while-switch menu code.
// steven.riedel@gmail.com
#include <iostream>
usingnamespace std;
int main( void ) {
int choise;
do { // does the block of code, before the check
cout << "Simple Menu:" << endl;
cout << "1. Hi!" << endl;
cout << "2. Whatever. " << endl;
cout << "3. Quit." << endl;;
cout << "Please enter a choise : ";
cin >> choise;
switch ( choise ) {
case 1: // if choise == 1
cout << "Ok, I'm doing option #1! " << endl;
// whatever block of code or function calls for option 1.
break; // jumps to the end of the switch
case 2: // if choise == 2
cout << "Ok, now I'm doing option #2! " << endl;
// whatever block of code or function calls for option 2.
break; // jump to the end of the switch
case 3: // if chose == 3, the quit case.
cout << "Thank you for using this menu. " << endl;
break; // jump to the end of the switch
default: // input doesn't match any case.
cout << "Wrong Option! " << endl;
} // switch's end.
} while ( choise != 3 ); // read as "Keep doing everything between my { }
// while "choise" is not equal to 3."
return 0;
}
void fibo(int m)
{
int m,a1,a2,i=2;
if(m<2) {
pirntf("the a1=1 and a2=1 in fibonacci");
i=m+1;// to not to enter in loop
}
num[0]=1; num[1]=1;
for(;i<m;i++) //if i !=m+1 the loop will start
{
a1=1;a2=1;a3=a1+a2;
printf("%d",a3);
a1=a2;a2=a3;
}
}
then in your option1 place function fibo() or if you shouldn't use function place it normally
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int m,a1,a2,i=2;
scanf("%d",&m);//the m is 99 here!
if(m<2) {
pirntf("the a1=1 and a2=1 in fibonacci");
i=m+1;// to not to enter in loop
}
num[0]=1; num[1]=1;
for(;i<m;i++) //if i !=m+1 the loop will start
{
a1=1;a2=1;a3=a1+a2;
printf("%d",a3);
a1=a2;a2=a3;
Thanks 4 the response! I tried it without the "break;" statemts in my "if" stmnts but it takes me back to the "Enter your choice:" option... About the fibonacci nums i'm still a bit confused, if my "integer" variable entered is 99 for example, how do i get the fibo nums to that number?
Then i'll have to get the primes to that same num as well!