sum of digits of a number program in java

package core;

import java.util.Scanner;

public class SumOfDigits {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  Scanner sc = new Scanner(System.in);

  System.out.println("Enter a number");

  int n = sc.nextInt();

  int sum = 0;

  int lastDigit = 0;

  while (n != 0) {

   lastDigit = n % 10;

   sum = sum + lastDigit;

   n = n / 10;
  }

  System.out.println("sum of digits of a numbr is " + sum);

 }

}

Search This Blog

All the rights are reserved to this blog is belongs to me only.. Powered by Blogger.