C Online Compiler
Example: Sum of Integers (While Loop) in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Sum of Integers (While Loop) #include <stdio.h> int main() { // Step 1: Declare and initialize variables int sum = 0; // Variable to store the sum int i = 1; // Loop counter, initialized to 1 // Step 2: Use a while loop to iterate from 1 to 10 // The loop continues as long as i is less than or equal to 10. while (i <= 10) { sum += i; // Add the current value of i to sum i++; // Increment i for the next iteration } // Step 3: Print the final sum printf("The sum of numbers from 1 to 10 is: %d\n", sum); return 0; }
Output
Clear
ADVERTISEMENTS