Activity in Programmin1

You might also like

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

Activity In Programming

Exercise 1:
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
for (double element: myList) {
System.out.println(element);
}
}
}
Exercise 2:
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " "); }
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i]; }
System.out.println("Total is " + total);
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i]; }
System.out.println("Max is " + max);
}
}
Exercise 3:
public class average {
public static void main(String[] args) {
int[ ] aryNums = { 24, 6, 47, 35, 2, 14 };
int i;
int arrayTotal = 0;
int average = 0;
for (i=0; i < aryNums.length; i++) {
arrayTotal = arrayTotal + aryNums[ i ];
}
average = arrayTotal / aryNums.length;
System.out.println("total: " + average);
}
}
Exercise 4:
public class oddaverage {
public static void main(String[] args) {
int[ ] aryNums = { 24, 6, 47, 35, 2, 14 };
int i;
int oddNum = 0;
for (i=0; i < aryNums.length; i++) {
oddNum = aryNums[ i ] %2;
if (oddNum == 1) {
System.out.println("odd number = " +
aryNums[i]); }
}
}
Exercise 5:
-In this program it holds the values 1.9, 2.9, 3.4, 3.5 in an array. It uses the foreach loop, which
is an easier version of the normal for loop, to print out each of the values of the array until they
are all printed with their own line because it uses the System.out.println syntax.
Sample Output:
1.9
2.9
3.4
3.5
Exercise 6:
-This program is built from the previous one that holds the former numbers in the array, only
this time it holds 2 extra variables which are declared namely double total = 0; and double max
= myList[0]; along with the second for loop to sum up all the elements and the third for loop to
find the largest element in the array.
Sample Output:
1.9
2.9
3.4
3.5
Total is 11.7
max is 3.5
Exercise 7:
-In this program, we have set up an array to hold the following values: 23, 6, 47, 35, 2, and 14.
We used the variable int to hold these elements, using the for loop, it loops all of the numbers in
the array so that it can be added in our arrayTotal variable, which then stops once all the
numbers have been used up and the total of this variable is 128. Once outside the for loop there
is another variable declared average which divides the the total of the elements by the number
of max elements, in this case 128 / 6, which is equal to 21.333333… but because we are using
int, it round downs our answer to 21.
Output:
total: 21
Exercise 8:
-In this program it uses the same code just with some adjustments so that it only prints out the
odd numbers. So instead of the variable arrayTotal inside the for loop it uses the new variable
we have which is named oddNum, which essentially just checks if an element can hold a
remainder or not because of the modulo(%) operator, if it does have a remainder, it is deemed as
an odd number. After the for loop there is an if statement that dictates that if it can be equal to 1
and is not equal to anything else, then it will be printed out on the next statement with “odd
number = “.
Sample Output:
odd number = 47
odd number = 35

Submitted BY: Charles Rich C. Ramada


Gr&Sec: 11 – PROG A - PYTHON

You might also like