Advertisement Remove all ads

Write an Object Oriented Program in C++ to Read an Integer Number and Find the Sum of Digits of Integer [Hint : Input 125 Output 8 I.E. 1 + 2 + 5 = 8] - Computer Science 1

Advertisement Remove all ads
Advertisement Remove all ads
Advertisement Remove all ads

Write an object oriented program in C++ to read an integer number and find the sum of digits of integer [Hint : input 125 output 8 i.e. 1 + 2 + 5 = 8]

Advertisement Remove all ads

Solution

# include <iostream.h>
class digit
{
   private :
      int num;
   public :
      void getdata();
      void sum() ;
} ;
void digit::getdata()
{
    cout << "Enter the number";
    cin >> num ;
}
void digit::sum()
{
    int rem, add = 0;
    while (num > 0)
{
    rem = num % 10;
    add = add + rem;
    num = num /10;
}
 cout << "The sum of all digit of number = "<< add;
}
void main()
{
   digit S;
   S.getdata();
   S.sum();
}
Concept: C++ Programming
  Is there an error in this question or solution?

APPEARS IN

Advertisement Remove all ads
Share
Notifications

View all notifications


      Forgot password?
View in app×