Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

So, I have a problem when programming in C++, could you help?

+6 votes
asked Sep 22, 2022 by Allan José De Oliveira Pereira (180 points)
make a program that fills a vector of 5 positions with integers, after that, sweep this vector and show on the screen only those that are divisible by 3, if all are not divisible, show the message "none is divisible"
but i can't find a way to show the message

the code:

#include <iostream>

using namespace std;

int c[5],h,i;

int main()
{
    for(i=0;i<5;i++){
    
    cout<<"Insira o valor desejado: ";
    cin>>c[i];
    }
    for(i=0;i<5;i++) {
      
       if (c[i]%3==0){
          cout<<c[i]<<" " << "\n";
          
       } else {
         cout <<"nenhum  ";
        }
    }
       
    return 0;
}

1 Answer

+1 vote
answered Sep 24, 2022 by Peter Minarik (86,160 points)

Please, see below your code modified on how to print numbers divisible by 3.

Also, your description talks about a vector, but your implementation uses an array. Double-check if this is what you want to do.

#include <iostream>

using namespace std;

const int SIZE = 5;

int c[SIZE], h, i;

int main()
{
    for (i = 0; i < SIZE; i++)
    {
        cout << "Enter a number: ";
        cin >> c[i];
    }
    
    cout << "Numbers divisible by 3:";
    
    for (i = 0; i < SIZE; i++)
    {
        if (c[i] % 3 == 0)
            cout << " " << c[i];
    }
       
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...