SeanNers

summingDigits

Aug 15th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. package vn.com.hoangminh.Physics;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class summingDigits {
  6.  
  7. public static void main(String[] args ) {
  8.  
  9. Scanner input = new Scanner(System.in);
  10.  
  11. System.out.print("Nhập số cần tính : ");
  12. int nhap = input.nextInt();
  13.  
  14. int soLuongTu = Integer.toString(nhap).length();
  15.  
  16. summingDigits compute = new summingDigits();
  17.  
  18. System.out.print("Tổng của các chữ số trong " + nhap + " là : " + compute.sumCompute(soLuongTu, nhap));
  19. }
  20.  
  21. public int sumCompute(int soLuongTu, int nhap) {
  22.  
  23. int sum = 0;
  24. for(int i = 0; i < soLuongTu; i++) {
  25. sum = sum + nhap % 10;
  26. nhap /= 10;
  27. }
  28. return sum;
  29. }
  30.  
  31.  
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment