how can i construct a summation program using a for loop? i.e. the user enters the number 2 and the program executes: 0 + 1 + 2 = 3.
how can i construct a summation program using a for loop? i.e. the user enters the number 2 and the program executes: 0 + 1 + 2 = 3.
How would you do it? What are your ideas?
If you have a loop like the following:
It will print out "0 1 2 3 4 5". Figuring out how to get the program to sum these values is extremely easy at this point.Code:int count = 5; for(int i = 0; i <= count; i++) { printf("%i ", i); }
Apart from if you are using C++ then don't use printf
Printf is totally legal in C++.
http://www.cppreference.com/stdio/index.html
Code:int count = 5; int sum=0; for(int i = 0; i <= count; i++) { sum+=i; printf("%i ", i); if(i==count-1){ printf("+ "); } else{ printf("= "); } } printf("sum: %i",sum);
"The Internet treats censorship as damage and routes around it." - John Gilmore
Just because something is legal doesn't mean you should use it. gets is legal in C. Although there is little reason to discuss it here since it is irrelevant to the topic and the point being made by UMTopSpinC7.