The Program in C that prints the sum of n numbers , sum of squares of first n even numbers and sum of the cube of first n odd numbers are given below:
#include <stdio.h>
int main() {
int n, i;
int sum = 0, sum_of_squares = 0, sum_of_cubes = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
sum += i;
if (i % 2 == 0) {
sum_of_squares += i * i;
} else {
sum_of_cubes += i * i * i;
}
}
printf("Sum of first %d numbers: %d\n", n, sum);
printf("Sum of squares of first %d even numbers: %d\n", n, sum_of_squares);
printf("Sum of cubes of first %d odd numbers: %d\n", n, sum_of_cubes);
return 0;
}
Output:
Enter the value of n: 56
Sum of first 56 numbers: 1596
Sum of squares of first 56 even numbers: 30856
Sum of cubes of first 56 odd numbers: 1228528
Pro-Tips💡
This program prompts the user to enter an integer value, then uses a for loop to iterate from 1 to n.
Inside the for loop, it keeps track of three sums: the sum of all numbers from 1 to n, the sum of the squares of the first n even numbers, and the sum of the cubes of the first n odd numbers.
Finally, it prints all three sums.
It uses an if else statement inside the for loop to check whether the current number is even or odd and perform the corresponding computation.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.