Advertisement
arabdy

Suma dijitos ciclo

Sep 23rd, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.47 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Punto_1 {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner sc = new Scanner(System.in);
  8.         System.out.println("Escriba un numero");
  9.         long n = sc.nextLong();
  10.         int resultado = sumarDigitos(n);
  11.         System.out.println( resultado);
  12.         sc.close();
  13.     }
  14.  
  15.     public static int sumarDigitos(long n) {
  16.  
  17.         long np = n;
  18.        
  19. int s = 0;
  20.         while (np != 0) {
  21.  
  22.             s = (int) ((np % 10) + s);
  23.             np = np / 10;
  24.  
  25.         }
  26.  
  27.         return s;
  28.     }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement