197k views
3 votes
Question 1 This program should declare a large array of integers and should read elements into the array from the keyboard, ending when zero is typed in. The program should then print out (i) all elements of the array that are divisible by 5 (ii) all elements of the array that have exactly two digits and (iii) all elements of the array that end in 7.​

1 Answer

4 votes

Here's an example program in C++ that fulfills the requirements:

______________________________________________________

#include <iostream>

#include <vector>

using namespace std;

int main() {

// Declare a vector to store integers

vector<int> array;

// Read elements into the vector from the keyboard

int num;

while (true) {

cout << "Enter a number (or 0 to exit): ";

cin >> num;

if (num == 0)

break;

array.push_back(num);

}

// Print elements divisible by 5

cout << "Elements divisible by 5:\\";

for (int i : array) {

if (i % 5 == 0)

cout << i << " ";

}

cout << endl;

// Print elements with exactly two digits

cout << "Elements with exactly two digits:\\";

for (int i : array) {

if (i >= 10 && i <= 99)

cout << i << " ";

}

cout << endl;

// Print elements ending in 7

cout << "Elements ending in 7:\\";

for (int i : array) {

if (i % 10 == 7)

cout << i << " ";

}

cout << endl;

return 0;

}

_______________________________________________________

This C++ program declares a vector to store integers and reads elements from the keyboard until the user enters 0. It then iterates through the vector to print the numbers that satisfy the given conditions: divisible by 5, having exactly two digits, and ending in 7.

~~~Harsha~~~

User Alpesh Valaki
by
8.3k points