Following program should produce an integer division of two numbers entered from the keyboard. Which lines of code contain logical errors or mistakes that result in compilation errors?

#include <iostream>                                   //1

int main()
  int n1, n2;
  std::cout << "Enter two integers: ";
  std::cin >> n1 >> n2;                               //2

  if (n2 =! 0)                                        //3
      std::cout << "Result is: " << (n1/n2) << '\n';  //4
      std::cout << "Can not divide by zero\n";

  return 0;
There is an error in the line 3 since assignment is performed inside the if clause:

n2 = !0;
if (n2)
Therefore, the program will always assigns a non-zero value to n2 (1), then executes the condition (which is always true) and divides the first number by the second.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Sign Up Now
or Subscribe for future quizzes