C++ Online Compiler
Example: Sum of Numbers Up To 10 in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Sum of Numbers Up To 10 #include <iostream> using namespace std; int main() { // Step 1: Initialize a variable to store the sum // It's crucial to start with 0 so the first addition is correct. int sum = 0; // Step 2: Use a for loop to iterate from 1 to 10 // The loop variable 'i' will take values 1, 2, ..., 10. for (int i = 1; i <= 10; ++i) { // Step 3: Add the current number 'i' to the sum sum = sum + i; // This can also be written as sum += i; } // Step 4: Print the final sum to the console cout << "The sum of numbers from 1 to 10 is: " << sum << endl; return 0; }
Output
Clear
ADVERTISEMENTS