# Thread: How to make a for-loop for an equation

1. ## How to make a for-loop for an equation

Hi, I was wondering if someone can help me with a pretty basic for loop for one of the equation. I've been bashing my head about it for the past 3-4 days and I am nowhere near the exit. It has to be calculated for a number n that has been inputted. I am grateful for the help  2. So... It's a sum of fractions. Each fraction is the sum of the "n" first odd numbers, all divided by the sum of the squares of the "n" first odd numbers...

If you were to brute-force it, you need 1 outer loop and 2 inner loops. But if you do a bit of math wizardry, you can reduce it to a single loop. Food for thought, the sum of all natural numbers from 1 to N is equal to N*(N+1)/2. You can derive similar formulas for the sum of all odd numbers and for the sum of the squares too. 3. I leave the math solution up to you to figure out, but the brute force it dead-easy:
Code:
```for (i = 0; i < N; ++i) {
for (j = 0; j <= i; ++j) {
// Sum up the numerator
// Sum up the denominator
}
// Divide the numerator with the denominator and add the result to S
}``` 4. Code:
```for (i = 1; i < N; i+=2) {
for (j = 1; j <= i; i+=2) {
// Sum up the numerator
// Sum up the denominator
}
// Divide the numerator with the denominator and add the result to S
}```
Made what I hope is a correction to GReaper code.

Edit: Changed "=+" to "+=" and "j = 0" to "j = 1"

Tim S. 5. Heh, the funny thing is that we're both right at the same time. Depending on the implementation, both ways can be used. I still had math spinning in my head after finding the formula for the sum of squares, and I was thinking of odd numbers in terms of natural numbers, like (2*n + 1).  6. I've still been bashing my head to no avail. This is the best I've come up with and it doesn't work. Here's the entire code. Feel free to correct me wherever you like. Being new with all this, I have no damn idea what I am doing and where it's all going wrong. I'd honestly be grateful for the guidance. The other idea was to use if(i%2!=0), but that went nowhere.
Code:
```#include <stdio.h>
#include <stdlib.h>

int main ()
{
int i,j,k=1,n;
float s,p,q;
printf("Insert n=");
scanf("%d",&n);
s=0; p=0;q=0;
for (i=1; i<=n;i+=2){
for (j=1;j<=n;j+=2){
k*=i;

}
q+=k;
p+=i;
}
s+=q/p;
printf("%.2f",s);
return 0;

}```

All I know is that for number 3, the result given should be 8.00, not 1.80 7. Pick better variable names.
Code:
```for ( powers = 1 ; powers <= 10 ; powers += 2 ) {
int numerator = 0;
for ( sum = 1 ; sum <= powers ; sum += 2 ) {
numerator += sum;
}
printf("The numerator for power=%d is %d\n", power, numerator );
}```
Do this for denominator as well.

When you're happy that both are correct, proceed to calculation.

Looking at a mass of i,j,k tells you nothing. Popular pages Recent additions 3-4, days, equation, head, past 