C Examples

WAP in C to print the sum of n numbers , sum of squares of first n even numbers and sum of the cube of first n odd numbers

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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X