Calculate Sum of First N Natural Numbers
C++
Easy
4 views
Problem Description
If N = 5, find sum 1+2+3+4+5 = 15
Real Life: Like adding page numbers from page 1 to page N.
Step-by-Step Logic:
1. Take value of N
2. Start sum = 0
3. Add numbers from 1 to N one by one
4. Print final sum
Official Solution
void control_q4_sum_n_numbers() {
int n = 10;
int sum = 0;
for(int i = 1; i <= n; i++) {
sum = sum + i;
}
cout << "Sum of first " << n << " numbers: " << sum << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!