#include<iostream.h>
int sumofdigits(int); //function declaration
int main()
{
int n,sod;
cout<<"Please enter a valid +ve Integer"<<endl;
cin>>n; //reading Integer
while(n<0) //validating Integer
{
cout<<"You have entered a -ve Number"<<endl;
cout<<"Please enter a valid +ve Integer"<<endl;
cin>>n;
}
sod=sumofdigits(n); //function calling
cout<<"Sum of digits of given Integer is :"<<sod;
return 0;
}
int sumofdigits(int n) //called function
{
int sod=0; //initializing variable
while(n!=0)
{
sod=sod+n%10;
n=n/10;
}
return sod;
}
No comments:
Post a Comment