5.5k views
0 votes
Write a c program to print the sum of cubes of odd numbers between 1 to 100​

User Deroccha
by
4.1k points

2 Answers

0 votes

CODE

#include <stdio.h>

int main() {

// Declare the variable

int sum;

for(int i=1;i<=100;i=i+1) {

// Check if i is an odd number

if(i%2 == 1) {

sum = sum + i*i*i;

}

}

// Print the sum

printf("%d", sum);

return 0;

}

DISPLAY


\Huge\boxed{\sf 12497500}

EXPLANATION

Declare sum as an integer.

Use a for loop, which runs from 1 to 100 and increments by 1 each time.

Use an if statement to check if the remainder is 1 to see if the number is odd.

Print the sum.

Return an integer value.

User Andy Xu
by
3.4k points
4 votes

Answer:

int sum = 0;

for (int i = 1; i < 100; i += 2) {

sum += i * i;

}

printf("The sum of cubes is %d", sum);

/* Prints: The sum of cubes is 166650 */

Step-by-step explanation:

If 1 should be excluded, let the for loop start at 3.

User Lukeck
by
2.8k points