Discussion in "Software" started by    aj1    Nov 14, 2008.
Fri Nov 14 2008, 09:03 pm
#1
question : write a loop that will compute s the sum of the square of the first 100 odd integers.

i wrote following c program but some how its not giving me write naswer.
the compiler is not giving me any error. the answer i get is 22850 wihich is worng.

#include "C:\egnu110\include\hcs12.h"
#include "C:\egnu110\include\delay.c"
#include "c:\egnu110\include\stdio.c"
#include "c:\egnu110\include\convert.c"

const char *msg= "the odd sum = ";
int main()
{
double sum=0;
int j, i, buf[4];
for(i=1,j=1;i<=100;i++,j=j+2)
{
sum+=j*j;
}
newline();
newline();
int2alpha(sum,buf);
puts(msg);
putsr(&buf[0]);

return 0;
}

Sat Nov 15 2008, 04:24 am
#2
@aj1
Oddly the value I get from your code (very slightly adapted) is 1333300.

May be worth trying

sum+=(j*j);

Or (more likely) maybe int2alpha() doesn't handle doubles properly.


[ Edited Sat Nov 15 2008, 04:33 am ]

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

WilliamErync
Sun May 05 2024, 02:35 am
Danielnof
Sat May 04 2024, 11:12 pm
oriminna
Sat May 04 2024, 08:28 pm
scaneraNom
Sat May 04 2024, 02:21 am
gtaletrzua
Fri May 03 2024, 10:55 am
Clydehet
Wed May 01 2024, 06:44 pm
Davidoried
Wed May 01 2024, 06:11 pm
KevinTab
Sun Apr 28 2024, 05:35 am