Advertisement
Shavit

P. 187 Ex. 7.40 A

Nov 8th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.44 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class DigitCount {
  7.  
  8.     public static void main(String[] args)
  9.    
  10.     {
  11.         Scanner in = new Scanner (System.in);
  12.        
  13.         int digits = 0;
  14.         int num;
  15.        
  16.         System.out.print("Enter a number: ");
  17.         num = in.nextInt();
  18.        
  19.         while(num > 0)
  20.         {
  21.             digits = digits + num % 10;
  22.             num = num / 10;
  23.         }
  24.        
  25.         System.out.printf("The digits sum is %d", digits);
  26.        
  27.         in.close();
  28.     }
  29.    
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement