We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c05008f commit 4a2a951Copy full SHA for 4a2a951
challenge19.java
@@ -0,0 +1,23 @@
1
+import java.util.Scanner;
2
+
3
+// Create a program that computes the sum of the digits of an integer.🚀
4
+public class challenge19 {
5
+ public static void main(String[] args) {
6
+ Scanner sc = new Scanner(System.in);
7
+ System.out.println("Welcome to Sum of Digits!");
8
+ System.out.print("Enter a number: ");
9
+ int num = sc.nextInt();
10
+ int sum = sumOfDigits(num);
11
+ System.out.println("The sum of the digits is: " + sum);
12
+ }
13
14
+ public static int sumOfDigits(int num) {
15
+ int sum = 0;
16
+ while (num > 0) {
17
+ int digit = num % 10;
18
+ sum += digit;
19
+ num /= 10;
20
21
+ return sum;
22
23
+}
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments