array of even numbers

hi guys, my program displays a set of even numbers ranging from 1 to 100 with their square roots, cubes and reciprocals. pls i need a source code for comparison, can someone help with that? thanks.
Last edited on
Here's some help. You can loop through 1-100 iterations with a for loop.

1
2
3
4
for (int i = 1; i <= 100; i++)
{

}


Then you can check if you ran into any even numbers with the conditional if statement and then run your math operations on them.

1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 1; i <= 100; i++)
{
    if (i % 2 == 0) // The '%' symbol is the modulus which divides the number and finds the remainder
    {
        // Run math operations here on the even number (i)
        // Use can use the math library in C++ for square roots
        //
        // Use can find the reciprocal with the following
        // (1 / i) reciprocal
        // Then you can run math operations for cubes here as well and print the data
    }
}
Last edited on
okay....can i get a full source code on that?
Uhm.. I'd rather not spoon feed you. It would be better if you tried it yourself first. I provided what should give you a better understanding on this subject.
okay here is what i have done....but its not calculating the reciprocal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;

int main()
{
    int i;
    int r = 1/i;
    for (i=1; i<=100; i++)
    {
        if (i%2==0)
        {
            cout <<"The even number is "<< i <<"\n\n";
            cout << "The square root of the even number is "<<sqrt(i)<<"\n\n";
            cout << "The reciprocal of the even number is "<< r<< "\n\n";
        }
    }

    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
    int i;
    int r = 1/i;
    for (i=1; i<=100; i++)
    {
        if (i%2==0)
        {
            cout <<"The even number is "<< i <<"\n\n";
            cout << "The square root of the even number is "<<sqrt(i)<<"\n\n";
            cout << "The reciprocal of the even number is "<< r<< "\n\n";
        }
    }


You are assigning r to a unidentified integer which has unexpected results. You could write a function with a argument of a double and return the reciprocal back for example.

1
2
3
4
5
double getReciprocal(double number)
{
    double reciprocal = (1 / number);
    return reciprocal;
}


Then you could just pass it into your if statement like this:
 
cout << "The reciprocal of the even number is " << getReciprocal(i) << "\n\n";


Last edited on
closed account (oGN8b7Xj)
And like this?
1
2
3
4
5
6
7
for (int i (2) ; i < 100 ; i += 2)
{
  cout <<"The even number is "<< i <<"\n\n";
  cout << "The square root of the even number is "<< sqrt((double) i) <<"\n\n";
  cout << "The cube of the even number is "<< i * i * i <<"\n\n";
  cout << "The reciprocal of the even number is "<< 1.0 / (double) i << "\n\n";
}
Last edited on
If @OP has learned about function prototyping, it would be exactly as @Mr Impact mentioned: just make a function for it.

1
2
3
4
5
6
7
8
9
10
11
12
// this allows you to define them in any order without worrying about compiler errors
float squareRoot( float &number );
float cube( float &number );
float reciprocal( float &number );

int main( void )
{
    for( int a = 2; a < 100; a += 2 )
    {
        // call those functions
    }
}
Topic archived. No new replies allowed.