Skip to content

Commit 8cb16c0

Browse files
committed
feat: Calculate sum of dynamic array elements entered by user
🧠 Logic: - Takes input `n` from user to define array size. - Initializes an array `A` of size `n`. - Accepts `n` integer inputs from the user and stores them in the array. - Prints each array element. - Iterates through the array and calculates the sum of all elements. - Displays the total sum. Signed-off-by: Somesh diwan <[email protected]>
1 parent be34c11 commit 8cb16c0

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
11
import java.util.Scanner;
22

3-
class ArraySum
4-
{
5-
public static void main(String[] args)
6-
{
3+
class ArraySum {
4+
public static void main(String[] args) {
75
int n, sum = 0;
86
Scanner sc = new Scanner(System.in);
97

108
System.out.println("Enter Size of an Array");
119

1210
n = sc.nextInt();
13-
1411
int [] A = new int[n];
1512

1613
System.out.println("Enter Array Elements: ");
1714

18-
for(int i = 0; i<A.length; i++)
19-
{
15+
for(int i = 0; i<A.length; i++) {
2016
A[i] = sc.nextInt();
2117
}
2218

2319
System.out.println("Array elements are: ");
2420

25-
for(int i = 0; i<A.length; i++)
26-
{
21+
for(int i = 0; i<A.length; i++) {
2722
System.out.println(A[i]);
2823
}
2924

3025
System.out.println("Sum of Array Elements are: ");
3126

32-
for(int i = 0; i<A.length; i++)
33-
{
27+
for(int i = 0; i<A.length; i++) {
3428
sum = sum + A[i];
3529
}
3630
System.out.println(sum);
3731
}
38-
}
32+
}

0 commit comments

Comments
 (0)