Advertisement
YouKnowWho07

add the digits of a number

Dec 1st, 2023 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.43 KB | None | 0 0
  1. /*Write a program to add the digits of a number.
  2. Sample execution:
  3. Enter a number: 69837
  4. The sum of the digits of this number is: 33
  5. */
  6.  
  7. #include<stdio.h>
  8. int main()
  9. {
  10.     int rem,n,sum=0; /// rem=remaining
  11.     printf("enter number: ");
  12.     scanf("%d", &n);
  13.  
  14.     while(n>0)
  15.     {
  16.         rem=n%10;
  17.         sum=sum+rem;
  18.         n=n/10;
  19.      }
  20.       printf("sum of digits: %d", sum);
  21. }
  22.  
  23. // input: 123
  24. // output: 6
  25.  
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement