Applications of Array in Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Applications of Array In Java.

Student Name: Advanced Internet Programming


Subject Code: CAP716/20CAP726

UID: 20MCA1020 Section/Group :1B


Semester: 1st Date of Performance: 10/10/20
Experiment No. 2

1. Aim/Overview of the practical:


Write a program to print the sum of the elements of an array following the given below condition.

2. Task to be done:
If the array has 6 and 7 in succeeding orders, ignore the numbers between 6 and 7 and consider the other
numbers for calculation of sum.

Eg1) Array Elements - 10,3,6,1,2,7,9


O/P: 22
[i.e 10+3+9]

Eg2) Array Elements - 7,1,2,3,6


O/P:19

Eg3) Array Elements - 1,6,4,7,9


O/P:10
3. Algorithm/Flowchart :
4. Dataset:

Eg1) Array Elements -

10,3,6,1,2,7,9 Eg2) Array Elements

- 7,1,2,3,6 Eg3) Array Elements -

1,6,4,7,9

5. Code for experiment/practical:

Class MyClass{

public static void main(String[] args)

{ Scanner s = new Scanner(System.in);


System.out.print("Enter no. of elements in
array:"); int n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter the elements:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();

int sum = 0,gap=0;


for(int i=0;i<a.length;i+
+){ if(a[i]==6){
for(int j=a.length-
1;j>=i;j--){ if(a[j]==7)
break;
gap+= [j];
}
break;
}
sum=sum+a[i];
}
System.out.println(gap+sum);
}
}
6. Result/Output/Writing Summary:
Example 1

Example 2

Example 3
Learning outcomes (What I have

learnt): 1.array initialization

2.looping statements

3.arithmetic operation on

array

4.importation of various

libraries

5.diffrent functions in java

You might also like