-2

I need to make a program in C++ in which I insert a number 'n' that's with 4 digits and it's a simple number, in the console and the console is showing me the multiplication of the certain number with 4 digits which I first needed to write. I tried to write (n/!2) in order to make the program execute only if the number can't divide by 2 and the console showed "main.cpp:22:36: warning: division by zero [-Wdiv-by-zero]". I've tried removing (n /! 2) and the code got executed without a problem. I will be glad if someone can tell me how can I fix it.

#include <bits/stdc++.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
int getProduct(int n)
{
    int product = 1;
    while (n != 0) {
        product = product * (n % 10);
        n = n / 10;
    }
    return product;
}
int main()
{
    int n;
    cout << "insert n ";
    cin >> n;
    if (n >= 1000 && n <= 9999 && n /! 2) {
        cout << (getProduct(n));
    }
}
user3840170
  • 11,878
  • 11
  • 37
  • 1
    I can't compile this. Please read [Why should I not `#include `?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) – Ted Lyngmo Jan 11 '21 at 18:01
  • 5
    What this `(n /! 2)` suppose to mean? – Slava Jan 11 '21 at 18:03
  • @Slava (n /! 2) is the same as !+ or !- but with division, I hope. I tried adding the ! before / but it got me another error: expected ‘)’ before ‘!’ token – Тодор Славов Jan 11 '21 at 18:11
  • 1
    @ТодорСлавов `!+` and `!-` is not a special thing. Using `!` on an integer will treat the integer as a `bool` where anything but `0` is `true` (1) and `0` is `false` (0). The `!` reverses that so that `true` becomes `false` and vice versa. When a `bool` is used in arithmetic context (like in your division), it's promoted to an `int` (0 or 1). In this case `0`. – Ted Lyngmo Jan 11 '21 at 18:15
  • "(n /! 2) is the same as !+ or !- but with division" I am afraid you do not understand what `!+` or `!-` does. You need to learn basics of the language instead of inventing your own understanding on how operations work in C++ – Slava Jan 11 '21 at 19:48

4 Answers4

2

In the calculation n / !2 You effectively do n / 0.

! is short for not, not 2 is a boolean expression where 2 (anything but 0) is true.

not true is false.

false is promoted to an int in the calculation and false there becomes 0.

Solution: Use n % 2 != 0 or n & 1 to check for odd numbers.

Ted Lyngmo
  • 37,764
  • 5
  • 23
  • 50
1

You are looking for modulo operation. Modulo (the remainder from division) is equal to zero if number is divisible by the other one and non-zero otherwise.

if (n >=1000 && n <= 9999 && (n % 2) != 0)
Yksisarvinen
  • 13,037
  • 1
  • 18
  • 42
1

tried to write (n/!2) in order to make the program execute only if the number can't divide by 2

n / !2

This evaluates to n / 0. which triggers your warning.

To check if n is odd, you should do this instead:

n % 2 != 0
a.Li
  • 1,228
  • 3
  • 8
  • 18
0

! operator have higher precedence than / , So your expression becomes

=> n/(!2) => n/0

Instead you shoud use (n%2==0) to check for even number.