Question #259029

Write a program to find the sum of all natural numbers in a given range [m,n]

which are divisible by number k. The values of all three parameters mnk,

should be read from the keyboard at the beginning of the program.


Expert's answer

SOLUTION CODE FOR THE ABOVE QUESTION


SAMPLE PROGRAM OUTPUT




SOLUTION CODE


#include<iostream>
using namespace std;
int main()
{
    //prompt the user to enter the values of m, n and check
	//Declare the three variables
	int m;
	int n;
	int k;
	cout<<"\tEnter the value of m(the lower range limit): ";
	cin>>m;
	cout<<"\nEnter the value of n(the higher range limit): ";
	cin>>n;
	cout<<"\nEnter the value of k(number to check divisibility): ";
	cin>>k;
     // declare a variable to store the sum and initialize it to 0
	int sum = 0;
	for (int i = m; i <= n; i++)
	{
	  	// If the number is divisible by k
		// then add it to sum
		if (i % k == 0)
		{
		    sum += i;
		}
			  
	}


	// print what is in the sum 
	cout<<"\nThe sum of the numbers between "<<m<<" and "<<n<<" divisible by "<<k<<" = "<<sum<<endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS