hi ! I want to sum of digts on C++ but when get the nagative number give me the result if pastive number like when u write -98 answer u 17 not -17

#include<iostream>
#include<math.h>


using namespace std;


int main(){
    int N=0 ;
    cin>>N;
    int sum=0;
    while(N!=0){

            sum += (N%10);
        N = N / 10;
    }
    cout << sum << endl;


}

Recommended Answers

All 3 Replies

Multiply by -1 if the number is negative. Use an if statement.

How do I do that "ddanbe" when I used the while?

Use the if/else statement after completing the while loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.