What does this code mean? Why do I get particular numbers when running it?
1 2 3 4 5 6 7 8 9 10 11 12
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main(){
int a, s = 0;
cin >> a;
for (int i = 1; i <= a; i++) s += i;
cout << s << endl;
system("pause");
}
This is how descriptive variable names can help. The code below is the same (other than the more descriptive output), but just by reading it you probably have a better idea of what it is doing.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main()
{
int limit, sum = 0;
cin >> limit;
for (int i = 1; i <= limit; i++) sum += i;
cout << "The sum of numbers from 1 to " << limit << " is " << sum << endl;
system("pause");
}