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

Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am
utaletxcyw
Wed Apr 17 2024, 10:21 am
Anthonyvab
Wed Apr 17 2024, 08:48 am
RobertCix
Wed Apr 17 2024, 06:46 am
Astorne
Tue Apr 16 2024, 08:52 pm