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

Sazrzhk
Sun Oct 06 2024, 01:34 am
Sazrjgz
Sun Oct 06 2024, 01:23 am
Dnrthvf
Sat Oct 05 2024, 08:39 pm
Dnrthwo
Sat Oct 05 2024, 08:27 pm
Mofredka
Sat Oct 05 2024, 03:43 pm
ErnestClagS
Sat Oct 05 2024, 06:44 am
Arisha4Et
Fri Oct 04 2024, 05:53 pm
LancenuT
Fri Oct 04 2024, 02:15 pm