Help please,about user-defined functions

hello I am quit new to C++ I was writing a code that could give out all prime numbers from 0 to 20 here is the code ,but the out was 4370436 I dont know what to do from here.and also I would some more examples on User-defined fuctions.any help will be appreciated.thanks,
#include<iostream>
using namespace std;
void avoideven();
int main()
{
int a;
void avoideven();
cout<<a<endl;
return 0;
}
void avoideven()
{
int a;//---is our prime number
int b;
int result;
for(i=1;i<20;i++)
{
result=a/b;
result=a/a;
if(result==a)//--that prime number is divisible by one
{
a++;
}
else if(result==1)//---prime number is divible by itself
{
a++;
}
cout<<a<<endl;
}

return a;//---to the main function
}
closed account (z05DSL3A)

There are some good tutorials here:
http://www.cplusplus.com/doc/tutorial/

Here is some code to do what you want (hopefully you can work out what it is doing:
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
#include<iostream>
#include <math.h>

using namespace std;

bool isPrime (int num);

int main()
{
    const int maxNumber = 21;

    for(int i = 0; i <= maxNumber; i++)
    {
        if( isPrime(i) )
        {
            cout << i << " is prime" << endl;
        }
    }

    return 0;
}

bool isPrime (int num)
{
    if (num <=1)
        return false;
    else if (num == 2)         
        return true;
    else if (num % 2 == 0)
        return false;
    else
    {
        bool prime = true;
        int divisor = 3;
        double num_d = static_cast<double>(num);
        int upperLimit = static_cast<int>(sqrt(num_d) +1);
        
        while (divisor <= upperLimit)
        {
            if (num % divisor == 0)
                prime = false;
            divisor +=2;
        }
        return prime;
    }
}


___
How to put code into your postings:
http://www.cplusplus.com/forum/articles/1624/
Last edited on
Topic archived. No new replies allowed.