Advertisement
CR7CR7

sumDigits

Sep 23rd, 2022
946
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 1
  1. import java.util.Scanner;  
  2. public class SumOfDigitsExample1  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. int number, digit, sum = 0;  
  7. Scanner sc = new Scanner(System.in);  
  8. //System.out.print("Enter the number: ");  
  9. number = sc.nextInt();  
  10. while(number > 0)  
  11. {  
  12. //finds the last digit of the given number    
  13. digit = number % 10;  
  14. //adds last digit to the variable sum  
  15. sum = sum + digit;  
  16. //removes the last digit from the number  
  17. number = number / 10;  
  18. }  
  19. //prints the result  
  20. System.out.println(sum);  
  21. }  
  22. }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement