Hello,
Here I have a question that how to print the sum (answer) of the following series using loops in C:
12+22+32+42+52+62+......+n2
"n" is the given range of series by the user(input).
Hello,
Here I have a question that how to print the sum (answer) of the following series using loops in C:
12+22+32+42+52+62+......+n2
"n" is the given range of series by the user(input).
Last edited by shansajid; 01-02-2013 at 06:14 AM. Reason: Missed something
Are you aware of the for loop?
Here is an example.
This will give the sum ofCode:int i; int sum = 0; for(i = 0 ; i < 50 ; i++) { sum += i*5; } printf("sum is %d\n", sum);
5*1 + 5*2 + ... + n*5
I think I already said too much
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
I'm still trying to figure out the pattern. The fifth term is bugging me.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
oh sorry i missed. edited....
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
std10093, Im trying this code that you shared but its output is wrong. Whats the matter with this code?
Code:#include <stdio.h> #include <conio.h> void main() { clrscr(); int a,n,sum; printf("\nEnter the range: "); scanf("%d", &n); for(a=0; a<=n; a++) { sum += a*a; } printf("%d", sum); getch(); }
Perhaps you should initialise sum to 0 to begin with.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Take again a look in my example. There are two things that are different
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
your code shows that 5 is multiplied in all elements of a series. but i replaces usually 5 by the current element which is going to multiplied.
yes sum=0, now its running correct
Good move!
But, as Salem already said, I have initialized a variable... Also my for loop is a little different
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Thankyou std10093 and Salem for your help
Works for me - post your evidence.
Code:$ cat foo.c #include <stdio.h> int main() { int a,n,sum = 0; printf("\nEnter the range: "); scanf("%d", &n); for(a=0; a<=n; a++) { sum += a*a; } printf("%d\n", sum); return 0; } $ gcc foo.c $ ./a.out Enter the range: 5 55 $
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.