Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
50 views

Java Practice Problems Activity 2

The document contains 5 outputs from Java practice problems. Each output demonstrates a different Java concept: Output 1 calculates mathematical operations on two user-input integers. Output 2 removes an element from an integer array based on user input. Output 3 calculates the sum and average of 5 user-input integers stored in an array. Output 4 determines if three user-input integers are in increasing, decreasing, or neither order. Output 5 sorts an integer array in ascending and descending order to find the smallest and largest elements.

Uploaded by

Ian Tarega
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Java Practice Problems Activity 2

The document contains 5 outputs from Java practice problems. Each output demonstrates a different Java concept: Output 1 calculates mathematical operations on two user-input integers. Output 2 removes an element from an integer array based on user input. Output 3 calculates the sum and average of 5 user-input integers stored in an array. Output 4 determines if three user-input integers are in increasing, decreasing, or neither order. Output 5 sorts an integer array in ascending and descending order to find the smallest and largest elements.

Uploaded by

Ian Tarega
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

IAN L.

TAREGA
BSIT-2C
ACTIVITY NO.2 – JAVA PRACTICE PROBLEMS
OUTPUT NO. 1

package tarega_activites;

import java.util.Scanner;

public class Tarega_Activites {

public static void main(String[] args) {

int num1, num2, sum, diff, prod, gap, max, min;

double ave;

Scanner output = new Scanner(System.in);

System.out.println("Input First Integer: ");

num1 = output.nextInt();

System.out.println("Input Second Integer: ");

num2 = output.nextInt();

System.out.println("Expected Output:\n");

sum = num1 + num2;

System.out.println("Sum of two integers: "+sum);

diff = num1 - num2;

System.out.println("Difference of two integers: "+diff);

prod = num1 * num2;

System.out.println("Product of two integers: "+prod);

ave = sum / 2;
System.out.println("Average of two integers: "+ave);

if(num1 > num2){

gap = num1 - num2;

System.out.println("Distance of two integers: "+gap);

}else if(num2 > num1){

gap = num2 - num1;

System.out.println("Distance of two integers: "+gap);

if(num1 > num2){

System.out.println("Max Integers: "+num1);

System.out.println("Min Integers: "+num2);

}else{

System.out.println("Max Integers: "+num2);

System.out.println("Min Integers: "+num1);

OUTPUT NO.2

package tarega_activites;

import java.util.Scanner;

import java.util.ArrayList;
public class Tarega_Activites {

public static void main(String[] args) {

int i, j;

Scanner output = new Scanner(System.in);

ArrayList <Integer> arr_new = new ArrayList<>();

System.out.println("Enter Index to the number to remove: ");

j = output.nextInt();

int[] num = new int[]{1, 15, 16, 81, 28};

for(i = 0; i < num.length; i++)

if(num[i] != num[j])

arr_new.add(num[i]);

System.out.println("New array values: "+arr_new+"\n");

OUTPUT NO.3

package tarega_activites;

import java.util.Scanner;

import java.util.Arrays;
public class Tarega_Activites {

public static void main(String[] args) {

int i, sum = 0;

double average;

int[] num = new int[5];

Scanner output = new Scanner(System.in);

System.out.println("Input the 5 numbers: ");

for(i = 0; i < 5; i++){

num[i] = output.nextInt();

sum += num[i];

average = sum/5;

System.out.println("The sum of 5 no is: "+sum);

System.out.println("The Average is: "+average);

OUTPUT NO.4

package tarega_activites;

import java.util.Scanner;
public class Tarega_Activites {

public static void main(String[] args) {

int x, y, z;

Scanner output = new Scanner(System.in);

System.out.println("Input first number: ");

x = output.nextInt();

System.out.println("Input second number: ");

y = output.nextInt();

System.out.println("Input third number: ");

z = output.nextInt();

if((x > y && y < z) || (x < y && y > z)){

System.out.println("\nNeither increasing or decreasing order");

}else if(z > x && z > y){

System.out.println("\nIncreasing order");

}else if(x > y && x > z){

System.out.println("\nDecreasing order");

OUTPUT NO. 5

package tarega_activites;

import java.util.*;
public class Tarega_Activites {

public static void main(String[] args)

Integer arr[] = new Integer[]{1, 4, 17, 7, 25, 3, 100};

int k = 0;

System.out.println("Original Array: ");

System.out.println(Arrays.toString(arr));

System.out.println("Smallest element of the said array: ");

Arrays.sort(arr);

System.out.print(arr[k] + " ");

System.out.println("\nLargest element of the said array: ");

Arrays.sort(arr, Collections.reverseOrder());

System.out.print(arr[k] + "\n");

You might also like