Q:

Sum of all the elements in an array divisible by a given number K

belongs to collection: C++ programs on various topics

0

Sum of all the elements in an array divisible by a given number K

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

#include<iostream>

using namespace std;

int main(){
    int n, ele;

    cout<<"Enter the size of the array.\n";
    cin>>n;

    int A[n];
    cout<<"Enter elements in the array\n";
    for(int i =0;i<n;i++){
        cin>>A[i];
    }

    cout<<"Enter the element to be divisible by.\n";
    cin>>ele;

    int sum = 0;
    for(int i =0;i<n;i++){
        if(A[i]%ele==0){
            sum += A[i];
        }
    }

    cout<<"The sum is "<<sum<<endl;

    return 0;    
}

Output

 
Enter the size of the array.
6
Enter elements in the array
2 3 4 5 6 7
Enter the element to be divisible by.
3
The sum is 9

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Find the roots of quadratic equation in C++... >>
<< Create a class with public data members only in C+...