Sunday 3 April 2011

Write a program in C / C++ that calculate the summation of consecutive positive odd integers from 1 to n

Here is the code

#include using namespace std; int main() { int n; int sum = 0; int number; cout << "Enter the value for n: "; cin >> n; for (number=1; number<=(2*n); number+2) { sum = sum + number; } cout << "The sum of "; cout << n; cout << " is "; cout << sum << endl; return 0; }

No comments:

Post a Comment