You are here:

C/For Loop

Advertisement


Question
Hi
Using C, I was asked to use For Loop to calculate the answer for the sum of 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10.  Although I know how to display these numbers in sequence using a For Loop, but I could not use For Loop to calculate the sum of these numbers.  Any suggestions?  

Answer
main()
{
  int i;
  int sum = 0;

  for (i = 1; i <= 10; i++) sum = sum + i;

  printf("I = %d\n", i);
}

Hope this is what you are looking for.
I am averse to giving complete code. I usually expect the student to do the coding and if there is problem, help to debug.
But, in this case it was a very easy one and I thought I will help by giving code.
From next time, you have to do the coding and if there is problem, post the code and we will together correct it.

-ssnkumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.