odd numbers program need help plz
Fri Nov 14 2008, 09:03 pm
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;
}
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
@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.
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 ]
Powered by e107 Forum System