Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package vn.com.hoangminh.Physics;
- import java.util.Scanner;
- public class summingDigits {
- public static void main(String[] args ) {
- Scanner input = new Scanner(System.in);
- System.out.print("Nhập số cần tính : ");
- int nhap = input.nextInt();
- int soLuongTu = Integer.toString(nhap).length();
- summingDigits compute = new summingDigits();
- System.out.print("Tổng của các chữ số trong " + nhap + " là : " + compute.sumCompute(soLuongTu, nhap));
- }
- public int sumCompute(int soLuongTu, int nhap) {
- int sum = 0;
- for(int i = 0; i < soLuongTu; i++) {
- sum = sum + nhap % 10;
- nhap /= 10;
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment