0

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

mch
  • 9,424
  • 2
  • 28
  • 42
  • 4
    Think you mean to check if `i` is divisible by 4 and 6, whereas you're currently checking if `num1` and `num2` are respectively. Also, if you want to check between `num1` and `num2` inclusive, you'll need to scrutinize the head of your for loop. – HolyHoratio Apr 09 '19 at 10:20
  • 2
    Just a hint: A number is divisible by both 4 and 6, if it is divisible by 12... – Aconcagua Apr 09 '19 at 10:20
  • Thank you for submitting a question on StackOverflow! Please include sufficient code (including the #include statements) so that a member can compile your code by simply cutting and pasting. – Gardener Apr 09 '19 at 10:21
  • About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Apr 09 '19 at 10:27
  • Side note: It is not the task of a console application to keep the console open. This only prevents the programme from being used in a script (where there isn't any body to press a key...). So you shouldn't do stuff like `system("pause")`, `getchar()` or `getch()` (from conio.h). Instead, open a console (cmd.exe) first, navigate to your programme and start it from within console... – Aconcagua Apr 09 '19 at 12:42

4 Answers4

4

Here is the correct program:

#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;i<=num2;i++) // if you want to include num1 and num2
    //for(int i = num1+1;i<num2;i++) // if you do not want to include num1 and num2
    {
        if ( i % 4 == 0 && i % 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;
}
dzuda11
  • 147
  • 11
3

The condition in if( num1 % 4 == 0 && num2 % 6 == 0 ) is wrong.

You need to change it to:-

(i % 4 == 0 && i % 6 == 0)

Sujit Prasad
  • 117
  • 6
1

There are two issues:

At first, as 4 and 12 used as bounds shall yield 12, not 0, you need to include the bounds as well in your loop:

for(int i = num1; i <= num2; i++)
//                   ^ (!)
//             ^ - 1 dropped

Then your condition to select the summands is not correct:

if(num1 % 4 == 0 && num2 % 6 == 0)

Be aware that this is always true for num1 == 4 and num2 == 12, so you'd sum up all numbers in between... What you actually want is to check the variable running in between these two borders, which is i:

if(i % 4 == 0 && i % 6 == 0) // i will be 4, 5, ... , 11, 12 (with above fixed loop)

Additionally, we can have it a bit shorter: a number is divisible by both 4 and 6, if it is divisible by 12. So your check might simply look like:

if(i % 12 == 0)
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
0

As stated, you only need check for divisibility by 12 and you need to use i as the check in the conditional. My system does not have pause(), so you can just use another cin >> call to create the pause. Of course, then you have to provide a letter/number input. A space is not sufficient.

#include <iostream>
using namespace std;

int main()
{
  int num1, num2, sum=0;
  int wait_var;

  cout << "Input first number : ";
  cin >> num1;
  cout << "Input second number : ";
  cin >> num2;


  for(int i = num1 + 1;i<num2;i++) // bounds are correct
  {
    if ( i % 12 == 0) // check for divisibility by 12 of i, not of the num1 and num2
    {
      sum = sum + i;
    }
  }
  cout<< "The sum of all integers that are both divisible by 4 and 6 between " << num1 << " and " << num2 << " is " << sum << endl;
  cin >> wait_var;
  return 0;
}
Gardener
  • 2,591
  • 1
  • 13
  • 22