Answer to Question #259029 in C++ for Charmaine

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.


1
Expert's answer
2021-10-30T10:36:39-0400

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS