C++: Program to Add and Multiply the digits of a Number

We are the students of Computer Science and Engineering. We have to make many programs in various programming languages. We have to submit them in college assignment work or in school. Many times teacher just taught us the syntax of a language. The logic part has to be done on our own.



1. #include <iostream>2.3. int main()4. {5.    int n,sum=0,mul=1,rem;6.    std::cout<<"Enter the number"<<"\n";7.    std::cin>>n;8.      if(n==0){9.      mul=0;10.     sum=0;11. }12.    while(n>0){13.    rem=n%10;14.   sum+=rem;15.   mul*=rem;16.   n=n/10;17. }18.    std::cout<<"The sum is "<<sum<<" and the multiplication is "<<mul<<"\n";19.    return 0;20. }

The image of the output of this code:


Note: We can't add and multiply for large numbers.

 Follow us on
                              
You may also like:
C++: Check if the String is Palindrome
C++ STL: Merge_Sort using Vector
C++: Implementation of Queue using Array (Data Structure)
How do I learn data structures and algorithms from scratch?
C++: Implementation of Heapsort

Popular Posts

Top 10 Web Sites Every Programmer Should Visit Daily

How do I improve my logical thinking and problem solving skills?

C++: STL Iterators