1. Write a program to add all integers n in the range of N1 and N2 that are mult
ID: 3630055 • Letter: 1
Question
1. Write a program to add all integers n in the range of N1 and N2 that are multiples of “M”. Save the program as a file named “myadd.cpp” and upload the file “myadd.cpp”.• If N1 =12, N2 =19 and M=3, the program should add all multiples of 3 between 12 and 19, and get the result of 12+15+18=45. The program should display the message “The sum of multiples of 3 in the range 12 and 19 is 45.”
• The program should ask the user to enter the numbers N1, N2 and M and check that N2 is larger than N1 and display an error if otherwise.
• If the numbers are chosen, for example as N1 =11, N2 =14 and M=5 such that there are no multiples of 5 in the range, the display should have the additional statement “There are no multiples of 5 in the specified range.”
2. Rewrite the above program as a function called addM and call it from a main program with the numbers N1 =12, N2 =19 and M=3. Save it in a file called “problem2.cpp” and upload the file “problem2.cpp”.
Explanation / Answer
please rate - thanks
#include<iostream>
using namespace std;
int addm(int,int,int);
int main()
{int n1,n2,m,sum;
cout<<"Enter starting number: ";
cin>>n1;
cout<<"Enter ending number: ";
cin>>n2;
if(n1>n2)
cout<<"Program aborting-start number greater then end number ";
else
{cout<<"Enter multiple number: ";
cin>>m;
sum=addm(n1,n2,m);
if(sum==0)
cout<<"There are no multiples of "<<m<<" in the specified range. ";
else
cout<<"The sum of multiples of "<<m<<" in the range "<<n1<<
" and "<<n2<<" is "<<sum<<". ";
}
system("pause");
return 0;
}
int addm(int n1,int n2,int m)
{ int i,sum;
sum=0;
for(i=n1;i<=n2;i++)
if(i%m==0)
sum+=i;
return sum;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.