Given an array A of N integers and two integers X and Y, find the number of integers in the array that are both less than or equal to X and divisible by Y.
#include<iostream>
#include<string>
using namespace std;
int main(){
int size;
int X;
int Y;
int counter=0;
int * numbers;
//Create a dynamic array of user defined size.
cout<<"Enter the size of the array: ";
cin>>size;
numbers=new int[size];
//Now take input in array from user.
for(int i=0;i<size;i++){
cout<<"Enter the number "<<(i+1)<<": ";
cin>>numbers[i];
}
cout<<"Enter X: ";
cin>>X;
cout<<"Enter Y: ";
cin>>Y;
for (int i = 0; i < size; i++) {
if (numbers[i] <= X && numbers[i] % Y == 0) {
counter++;
}
}
cout<<"The number of integers in the array that are both less than or equal to X and divisible by Y: "<<counter<<"\n";
delete[] numbers;
cin>>size;
return 0;
}
Comments
Leave a comment