A program to Add numbers divisible by 3 or 5 in C++

Problem - To find the sum of numbers divisible by 3 or 5.

Input-

20

Output-

Numbers divisible by 3 or 5 are given below
3
5
6
9
10
12
15
18
20

The sum of numbers divisible by 3 or 5 is - 98

Program -

#include<iostream>
using namespace std;

int main()
{
    int num;
    cout<<"Enter the number ";
    cin>>num;
    int sum=0;
    cout<<"\nThe numbers divisible by 3 or 5 are given below "<<endl;
    for(int x=0;x<num;x++)
    {
        if(x%3==0)
        {
            cout<<x<<endl;
            sum=sum+x;
        }
        else if(x%5==0)
        {
            cout<<x<<endl;
            sum=sum+x;
        }
    }
    cout<<"\nSum of numbers divisible by 3 or 5 is "<<sum<<endl;

    return 0;
}


Comments

Popular posts from this blog

When only Some specific user name will be greeted in a C++ Program.

C++ Programming Language Easy Course