-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_sum_digits.java
More file actions
31 lines (30 loc) · 953 Bytes
/
array_sum_digits.java
File metadata and controls
31 lines (30 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.*;
public class array_sum_digits {
// Main function
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array: ");
int arr_size = sc.nextInt();
int[] arr = new int[arr_size];
for(int i = 0; i < arr_size; i++){
System.out.println("Enter the element "+(i+1)+": ");
arr[i] = sc.nextInt();
}
show(arr);
}
// Function to calculate the sum of digits of a number
static int sum_of_digits(int num){
int sum = 0;
while(num > 0){
sum += num % 10;
num /= 10;
}
return sum;
}
// Function to display the sum of digits of each element in the array
static void show(int []arr){
for(int x : arr){
System.out.println("The sum of digits of "+x+" is: "+sum_of_digits(x));
}
}
}