c++ looping

i have two questions on this topic. i have tried to do it but the program will not run as i expected it to be. can anyone help out please. thank you.

the questions are:

1. A prime integer number is one that has exactly two different divisors, namely 1 and the number itself. Write a c++ program that finds and prints all the prime numbers less than 100. (Hint: 1 is a prime number. For each number from 2 to 100, find reaminder = number%n, where n ranges from 2 to sqrt(number). If any remainder equals 0, the number is not a prime number.


2. The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13...; the first two terms are 0 and 1, and each term thereafter is the sum of the two preceding terms-that is, fib(n)=fib(n-1)+fib(n-2). Using this information, write a c++ program that calculates the nth number in the Fibonacci sequence, where the user enters n into the program interactively. For example, if n=6, the program should display a value of 5.


my first question's source code is as follow. could you point out my mistakes please. thanks

#include <iostream>
#include <cmath>
#include <conio.h>
using namespace std;

int main()
{
int num, n, remainder, prime, nonprime;

remainder = num % n;

for(num=2; num<=100; num++)
{

for(n=2; n<=sqrt(num); n++)
{
if(remainder=0)
num=nonprime;
else
num=prime;
}
cout<<prime<<" "<<endl;

}

getche();
return 0;
}




the second questions ource code i have so far is as follow. please help me out with it too.

[i]#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
int n, num;

n=(num-1)+(num-2);

cout<<"Please enter a number: "<<endl;
cin>>num;
cout<<n<<endl;
}

getche();
return 0;
}
1.)
- You are calculating the remainder before you are actually intializing n, and you are only doing it once (you need to do it for each n)
- You are using = instead of == for comparison
- The num = prime; and [/code]num = nonprime;[/code] don't make sense

2.)
- I don't think you really know what you are doing here...read:
http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.