C Examples

WAP in C to find the sum,average of values appearing at the positions divisible by 3 in the given sequence of n values

The Program in C that finds the sum,average of values appearing at the positions divisible by 3 in the given sequence of n values is given below:

#include <stdio.h>

int main() {
    int n, i, sum = 0, count = 0;
    double average;
    printf("Enter the number of elements in the sequence: ");
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        int x;
        printf("Enter the next element of the sequence: ");
        scanf("%d", &x);
        if ((i+1) % 3 == 0) {
            sum += x;
            count++;
        }
    }

    if (count != 0) {
        average = (double) sum / count;
        printf("Sum of the values appearing at positions divisible by 3: %d\n", sum);
        printf("Average of the values appearing at positions divisible by 3: %lf\n", average);
    } else {
        printf("No value found at positions divisible by 3\n");
    }
    return 0;
}

Output:

Enter the number of elements in the sequence: 7
Enter the next element of the sequence: 12
Enter the next element of the sequence: 56
Enter the next element of the sequence: 33
Enter the next element of the sequence: 57
Enter the next element of the sequence: 78
Enter the next element of the sequence: 11
Enter the next element of the sequence: 10
Sum of the values appearing at positions divisible by 3: 44
Average of the values appearing at positions divisible by 3: 22.000000

Pro-Tips💡

This program prompts the user to enter the number of elements in a sequence, then uses a for loop to iterate through the sequence.

Inside the for loop, it reads each element, check if its position is divisible by 3 and if yes, adds that value to the sum and increases the count.

After the loop it checks if count is zero, if yes it means no value found at positions divisible by 3, otherwise it calculates the average by dividing the sum with the count of values at positions divisible by 3 and prints the sum and average.

It is worth noting that in this program I am using i+1 as position, as the positions in array starts from 0 in C.

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