I need to write a program that will get 2 integers. The program will then display the sum of all integers that are both divisible by 4 and 6 between the two numbers
I tried doing the code down below:
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum=0;
cout << "Input first number : ";
cin >> num1;
cout << "Input second number : ";
cin >> num2;
for(int i = num1 + 1;i<num2;i++)
{
if ( num1 % 4 == 0 && num2 % 6 == 0)
{
sum = sum + i;
}
}
cout<< "The sum of all integers that are both divisible by 4 and 6 between " << num1 << " and " << num2 << " is " << sum << endl;
system("pause");
return 0;
}
My expected result should be
Input first number : 4
Input second number : 12
The sum of all integers that are both divisible by 4 and 6 between 4 and 12 is 12
"since 12 is the only number that is both divisible by 4 and 6"
But the actual results are
Input first number : 4
Input second number : 12
The sum of all integers that are both divisible by 4 and 6 between 4 and 12 is 56