Created
February 12, 2019 08:27
-
-
Save ani03sha/582d9403c084268a19f572cca843043b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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