Thursday, 19 June 2014

C++ program to print numbers from 1 to n that are divisible by x and indivisible by y


Here is a C++ program to print numbers from 1 to n that are divisible by x and indivisible by y


 SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
int main()
{
    long sum=0,n,y=1,x;
    cout<<"Enter the value of x ";
    cin>>x;
    cout<<"Enter the value of y ";
    cin>>y;
    cout<<"Enter the value of n ";
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        if(i%x==0&&i%y!=0)
        {
            cout<<i<<" ";
            sum+=i;
        }
    }
    cout<<endl<<"Sum "<<sum<<endl;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment