Hey can any1 figure out wat is wrong in this program...after i compiled it and executed the program ....I inputed the value of n...but after that it does not show the output...Can any1 help me plz..!!

The program which I wrote is below::

#include<iostream.h>


int sumofdigits(int n)


{


int sum=0, x;


while(n>0)


{
x=n%10;


sum+=x;


}


return sum;


}


void main()


{


int n, sum;


cout<<"Enter any number"<<endl;


cin>>n;


sum=sumofdigits(n);


cout<<"Sum of digits ="<<sum;


cout<<endl;


}

Recommended Answers

All 2 Replies

You forgot to update the loop counter.

while(n>0)
{
   x=n%10;
   sum+=x;
   n/=10;
}

hey Dave Sinkula thanx a lot...

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.