What did I do wrong?

what did I do wrong? everything worked fine but my
else {
cout << "no, it is not divisible by 9" << endl;
is being ignored and I can't get it on output.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <iostream>
using namespace std;

int main()
{
    int n, s=0;
 
     cout << "Enter a positive integer between 1 and 1000: ";
    cin >> n;
    if(n>1000){
      cout << "please enter between 1 and 1000" << endl;
      return 0;
    }
    else {
    while (n != 0) {
      s = s + n % 10;
      n = n / 10;
    }

    
 for (int i = 1; i <= 1; ++i) {
   if (n % 9 == 0){
     cout << "yes, it is divisible by 9 and the sum is " <<s<< endl;
   }
   else {
     cout << "no, it is not divisible by 9" << endl;
   }
 }
    }

 return 0;
}
By the time you get to line 22 ... you have well and truly changed n. You'll need to keep a copy (or, better, write a function to sum digits).

And there's not much point to the loop starting on line 21: it runs precisely once.
Thank you, I'm still new to coding and I did change some stuff you have mentioned and I think its working now.
Topic archived. No new replies allowed.