Skip to content

Instantly share code, notes, and snippets.

@ani03sha
Created February 12, 2019 08:27
Show Gist options
  • Save ani03sha/582d9403c084268a19f572cca843043b to your computer and use it in GitHub Desktop.
Save ani03sha/582d9403c084268a19f572cca843043b to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number between 0 and 1000");
int n = sc.nextInt();
if (n > 0 && n < 1000) {
int sum = 0;
while (n > 0) {
sum = sum + n % 10;
n = n / 10;
}
System.out.println("Sum of digits is: " + sum);
} else {
System.out.println("INVALID INPUT: Enter a valid number between 0 and 1000");
}
sc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment