Add Digits Of A Number

Introduction
  • In this blog, we will learn how to add the digits of a number. 
Software Requirements
  • Turbo C++ OR C 
Programming
  1. #include < stdio.h >   
  2. int main()   
  3. {  
  4.     int n, t, sum = 0, remainder;  
  5.     printf("Enter an integer\n");  
  6.     scanf("%d", & n);  
  7.     t = n;  
  8.     while (t != 0)  
  9.     {  
  10.         remainder = t % 10;  
  11.         sum = sum + remainder;  
  12.         t = t / 10;  
  13.     }  
  14.     printf("Sum of digits of %d = %d\n", n, sum);  
  15.     return 0;  
  16. }  
Explanation
  • From this above program, we can get the digits of a number. Then, we can add the values given by the user.

    programming
Output
 
Output
 
Conclusion: Thus, the program can be printed and executed successfully.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now