﻿ C Program: Sum of Cubes of even numbers (up to 20)

# C Program: Sum of Cubes of Even Numbers up to 20 using While loop

## C While Loop: Exercise-8 with Solution

Write a C program that calculates and prints the sum of cubes of even numbers up to a specified limit (e.g., 20) using a while loop.

Sample Solution:

C Code:

``````#include <stdio.h>

int main() {
int limit = 20;  // Specify the limit for even numbers
int sum = 0;     // Variable to store the sum of cubes

// Start a while loop to iterate through even numbers up to the limit
while (num <= limit) {
// Calculate the cube of the current even number
int cube = num * num * num;

// Add the cube to the sum
sum += cube;

// Move to the next even number
num += 2;
}

// Print the sum of cubes of even numbers
printf("Sum of cubes of even numbers up to %d: %d\n", limit, sum);

return 0;  // Indicate successful program execution
}
```
```

Sample Output:

```Sum of cubes of even numbers up to 20: 24200
```

Explanation:

Here are key parts of the above code step by step:

• #include <stdio.h>: Include the standard input-output library for functions like "printf()".
• int main() {: Start the main function.
• int limit = 20;: Specify the limit for even numbers.
• int sum = 0;: Initialize a variable to store the sum of cubes.
• while (num <= limit) {: Start a while loop to iterate through even numbers up to the limit.
• int cube = num num num;: Calculate the cube of the current even number.
• sum += cube;: Add the cube to the sum.
• num += 2;: Move to the next even number.
• }: Close the while loop.
• printf("Sum of cubes of even numbers up to %d: %d\n", limit, sum);: Print the sum of cubes of even numbers.
• return 0;: Indicate successful program execution.
• }: Close the main function.

Flowchart:

C Programming Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

-->

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿