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.
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
}
}
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.
#include <iostream>
#include <cmath>
#include <stdio.h>
usingnamespace 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;
}
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.
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";
}
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
}
}