Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get all the elements of an array that are divisible with 3 and then sum them thogether?

I dont know how to get the code to print out the elements that are divisable with 3 and the print out the sum of those elements , can someone help me do it , thanks for your time!

Code:

    #include <stdio.h>

int sum(int arr[]){
    int n = sizeof(arr) / sizeof(arr[0]);
    int sum = 0;
    for (int y=0;y<n;y++){
        sum += arr[y];
        printf("%d",sum);
    }
}

int main() {
    int F[5] = {1,3,5,9,8};
    int s = 0;
     for (int i=0;i<5;i++){
       if (F[i]%3 == 0) {
           int diviz[] = {F[i]};
           printf("%d\n",diviz[0]);
           sum(diviz);
       }
    }
    return 0;
}

Expected Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

3
9
12

Actual Output:

3
349
910

Idk how to solve this issue

>Solution :

int main() {
    int F[5] = {1,3,5,9,8};
    int s = 0;
     for (int i=0;i<5;i++){
       if (F[i]%3 == 0) {
           s = s + F[i];
           printf("%d",F[i]); 
       }
    }
    printf("%d",s); //print sum total 
    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading