Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4a2a951

Browse files
🍵
1 parent c05008f commit 4a2a951

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎challenge19.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /