Question #52415

Write a C++ program that takes an array of integers a and its length n as
parameters and return back the number of pairs of elements that are multiple of each
other.

Expert's answer

#include <iostream>
using namespace std;
int main()
{
    int n, a[100], c=0;
    cout<<"write down the number of elements n = ";
    cin>>n;
    cout<<"A["<<n<<"]:";
    for (int i=0; i<n; i++)
        cin>>a[i];
    cout<<endl;
    cout<<"A = {";
    for (int i=0; i<n; i++)
    {
        cout<<" "<<a[i];
        for (int j=i+1; j<n; j++)
        {
            if (a[j]%a[i]==0)
                c++;
        }
    }
    cout<<"the number of pairs is "<<c;
}


http://www.AssignmentExpert.com/

LATEST TUTORIALS
APPROVED BY CLIENTS