You are reading the article Java Program To Replace Element Of Integer Array With Product Of Other Elements updated in December 2023 on the website Minhminhbmm.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Java Program To Replace Element Of Integer Array With Product Of Other Elements
Array of Integers refers to an array where all the elements are of Integer type. It is alsocalled an Integer array.
As per the problem statement we have to create an Integer array and display the array elements where all the array elements are the product of other elements of the array.
In this article, we will see how to replace array elements by the product of other array elements by using Java programming language.
To show you some instances − Instance-1 int arr[] = { 2, 3, 1, 4, 6 } At Index-0 value will be = 3 * 1 * 4 * 6 = 72 At Index-1 value will be = 2 * 1 * 4 * 6 = 48 At Index-2 value will be = 2 * 3 * 4 * 6 = 144 At Index-3 value will be = 2 * 3 * 1 * 6 = 36 At Index-4 value will be = 2 * 3 * 1 * 4 = 24 So, the updated array elements are {72, 48, 144, 36, 24} Instance-2 int arr[] = { 1, 3, 2, 4 } At Index-0 value will be = 3 * 2 * 4 = 24 At Index-1 value will be = 1 * 2 * 4 = 8 At Index-2 value will be = 1 * 3 * 4 = 12 At Index-3 value will be = 1 * 3 * 2 = 6 So, the updated array elements are {24, 8, 12, 6} Algorithm Algorithm-1Step-1 − Declare and initialize an integer array.
Step-2 − Find the product of all array elements.
Step-3 − Divide the product value with the value of the respective index and replace the result. Continue this step till you reach the last array element.
Step-4 − Print the updated array.
Algorithm-2Step-1 − Declare and initialize an integer array.
Step-2 − Find the product of left sub array elements. The elements before the respective element to be replaced.
Step-3 − Find the product of right sub array elements. The elements after the respective element to be replaced.
Step-4 − Find the sum of product values of left and right subarrays and replace the resulting value.
Step-5 − Continue Step-2, 3 and 4 till you reach the last array element.
Step-6 − Print the updated array.
SyntaxTo get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.
Below refers to the syntax of it −
array.lengthwhere, ‘array’ refers to the array reference.
To get the String representation of the respective array we can use the toString() method of Arrays class in Java.
Arrays.toString(A) Multiple Approaches −We have provided the solution in different approaches.
By Dividing the Respective Index Element with the Total Product Value
By Finding the Product Values of Left and Right Sub Array
Let’s see the program along with its output one by one.
Approach-1 − By Dividing the respective Index Element with the Total Product ValueIn this approach, we will replace the array element by dividing the total product value with the respective element to be replaced.
Example import java.util.Arrays; public class Main { public static void main(String args[]) { int arr[] = { 2, 3, 1, 4, 6 }; System.out.println("Array elements are: "+Arrays.toString(arr)); int product = 1; for(int i=0;i<arr.length;i++) { product*=arr[i]; } for(int i=0;i<arr.length;i++) { arr[i] = product/arr[i]; } System.out.println("Updated array: "+Arrays.toString(arr)); } } Output Array elements are: [2, 3, 1, 4, 6] Updated array: [72, 48, 144, 36, 24] Approach-2 − By Finding the Product Values of Left and Right Sub ArrayIn this approach, we will replace the array element finding the sum of product of left subarray elements and product of right sub array elements.
Example import java.util.Arrays; public class Main{ public static void main(String args[]) { int arr[] = {4,2,3,1}; int size=arr.length; int temp[]= new int[size]; int sum=1; System.out.println("Array elements are: "+Arrays.toString(arr)); for(int index=0; index<arr.length; index++) { int leftProduct=1; int rightProduct=1; int i=0; if(i<index) { for(i=0;i<index;i++) { leftProduct*=arr[i]; } } for(i=index+1;i<arr.length;i++){ rightProduct*=arr[i]; } sum=leftProduct*rightProduct; temp[index]=sum; } arr=temp; System.out.println("Updated array: "+Arrays.toString(arr)); } } Output Array elements are: [4, 2, 3, 1] Updated array: [6, 12, 8, 24]In this article, we explored how to replace the array elements with the product of other array elements in Java by using different approaches.
You're reading Java Program To Replace Element Of Integer Array With Product Of Other Elements
Find Mean And Median Of An Unsorted Array In Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.
As per the problem statement we have to find mean and median of an unsorted array in Java.
Mean of an array can be derived by calculating the average value of all the elements present inside the array.
Mean= (sum of all elements present in array) / (total number of elements present)Median of an array represents the middle element present in an odd number sorted array and if the sorted array consists of even number, then median can be found out by calculating the average of middle two numbers.
Let’s explore the article to see how it can be done by using Java programming language.
To show you some instances Instance-1Given Array= [12, 23, 34, 45, 15].
Mean value of that array= (12 + 23 + 34 + 45 + 15) / (5) = 129 / 5 = 25.8
Sorted array of given array= [12, 15, 24, 34, 45]
As this is an odd numbered array the median is the middle element.
Median = 24
Instance-2Given Array= [38, 94, 86, 63, 36].
Mean value of that array= (38 + 94 + 86 + 63 + 36) / (5) = 317 / 5 = 63.4
Sorted array of given array= [36, 38, 63, 86, 94]
As this is an odd numbered array the median is the middle element.
Median = 63
Instance-3Given Array= [54, 67, 23, 95, 24, 60].
Mean value of that array= (54 + 67 + 23 + 95 + 24 + 60) / (6) = 323 / 6 = 53.83
As this is an even numbered array the median is the average value of middle two elements.
Sorted array of given array= [23, 24, 54, 60, 67, 95]
Median = (54 + 60) / 2 = 57
Algorithm
Step 1 − Declare and initialize an array of integer type.
Step 2 − Sort the array in ascending order.
Step 3 − In first user- defined method we find mean value. And in second user- defined method we find the median value.
Step 4 − Call both user-defined method and pass the array and the length value as parameters.
Step 5 − After finding the mean and median values print both the values as output.
SyntaxTo get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length
Below refers to the syntax of it −
array.lengthwhere, ‘array’ refers to the array reference.
You can use Arrays.sort() method to sort the array in ascending order.
Arrays.sort(array_name); Multiple ApproachesWe have provided the solution in different approaches
By Using Static Input Method
By Using User Input Method
Let’s see the program along with its output one by one.
Approach-1: By Using Static Input MethodIn this approach, we declare an array by static input method and pass this array and its length as parameter in our user defined method, then inside the method by using the algorithm we can find the mean and median values.
Example import java.util.*; public class Main { public static void main(String args[]) { int inputArray[] = { 23, 24, 65, 87, 85, 12, 76,21}; int len = inputArray.length; System.out.println("Mean of given array "+ Arrays.toString(inputArray)+ " is = " + mean(inputArray, len)); System.out.println("Median of given array "+ Arrays.toString(inputArray) + " is = " + median(inputArray, len)); } public static double mean(int arr[], int len) { int sum = 0; for (int i = 0; i < len; i++) sum += arr[i]; return (double)(arr[(len - 1) / 2] + arr[len / 2]) / 2.0; } public static double median(int arr[], int len) { Arrays.sort(arr); if (len % 2 != 0) { return (double)arr[len / 2]; } return (double)(arr[(len - 1) / 2] + arr[len / 2]) / 2.0; } } Output Mean of given array [23, 24, 65, 87, 85, 12, 76, 21] is = 49.125 Median of given array [23, 24, 65, 87, 85, 12, 76, 21] is = 44.5 Approach-2: By Using User Input MethodIn this approach, we declare an array by user input method and pass this array and its length as parameter in our user defined method, then inside the method by using the algorithm we can find the mean and median values.
Example import java.util.*; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.print("Enter the number of elements: "); int len=sc.nextInt(); int[] inputArray = new int[len]; System.out.println("Enter the elements: "); for(int i=0; i < len; i++) { inputArray[i]=sc.nextInt(); } System.out.println("Mean of given array "+ Arrays.toString(inputArray) + " is = " + mean(inputArray, len)); System.out.println("Median of given array "+ Arrays.toString(inputArray) + " is = " + median(inputArray, len)); } public static double mean(int arr[], int len) { int sum = 0; for (int i = 0; i < len; i++) sum += arr[i]; return (double)sum / (double)len; } public static double median(int arr[], int len) { Arrays.sort(arr); if (len % 2 != 0){ return (double)arr[len / 2]; } System.out.println(arr[(len - 1)]); System.out.println(arr[(len - 1)]); return (double)(arr[(len - 1) / 2] + arr[len / 2]) / 2.0; } } Output Enter the number of elements: 8 Enter the elements: 2 5 3 7 1 6 8 4 Mean of given array [2, 5, 3, 7, 1, 6, 8, 4] is = 4.5 Median of given array [2, 5, 3, 7, 1, 6, 8, 4] is = 4.5In this article, we explored how to find mean and median of an array in an unsorted array by using Java programming language.
Java Menu Driven Program To Determine Cost Of New Membership
When we go anywhere for any type of membership they have different prices for different plans. For example silver membership, gold membership, diamond membership, platinum membership etc. where the silver membership costs less than others and platinum membership costs more than other plans.
In this article we will see how to check the cost of a membership by using the Java programming language. We will be implementing the application using a switch case.
To show you some instances Instance-1Suppose we have Rs 1840 as base membership price and you want to have a silver membership. The cost of silver membership is 1932. Cost of silver membership is calculated as Base amount + 10% tax – 5% discount.
Instance-2Suppose we have Rs 1840 as base membership price and you want to have a gold membership. The cost of gold membership is 2116. Cost of gold membership is calculated as Base amount + 20% tax – 5% discount.
Instance-3Suppose we have Rs 1840 as base membership price and you want to have a diamond membership. The cost of diamond membership is 2300. Cost of diamond membership is calculated as Base amount + 30% tax – 5% discount.
Instance-4Suppose we have Rs 1840 as base membership price and you want to have a platinum membership. The cost of platinum membership is 2484. Cost of platinum membership is calculated as Base amount + 40% tax – 5% discount.
AlgorithmStep-1 − Ask user to input the current base membership price.
Step-2 − Display the menu.
Step-3 − Ask the user to enter their choice.
Step-4 − Use a switch case to go to the choice and perform the operation.
Step-5 − Print the result.
Let’s see the program to understand it clearly.
Exampleimport
java
.
util.
*
;
public
class
Main
{
public
static
void
main
(
String
args[
]
)
{
int
num;
Scanner
sc=
new
Scanner
(
System
.
in)
;
System
.
out.
(
"Enter the base price to opt for a membership: "
)
;
num=
sc.
nextInt
(
)
;
mainLoop:
while
(
true
)
{
System
.
out.
println
(
"n***Menu***"
)
;
System
.
out.
println
(
"1. Check cost for Silver Membership"
)
;
System
.
out.
println
(
"2. Check cost for Gold Membership"
)
;
System
.
out.
println
(
"3. Check cost for Diamond Membership"
)
;
System
.
out.
println
(
"4. Check cost for Platinum Membership"
)
;
System
.
out.
println
(
"5. Terminate the program"
)
;
System
.
out.
println
(
"Enter action number (1-5)"
)
;
int
command=
sc.
nextInt
(
)
;
switch
(
command)
{
case
1
:
double
a1=
num+
(
.1
*
num)
-
(
.05
*
num)
;
System
.
out.
println
(
"Cost for Silver Membership is "
+
a1)
;
}
else
{
System
.
out.
println
(
"Base membership price should be greater than 1000"
)
;
}
break
;
case
2
:
double
a2=
num+
(
.2
*
num)
-
(
.05
*
num)
;
System
.
out.
println
(
"Cost for Gold Membership is "
+
a2)
;
}
else
{
System
.
out.
println
(
"Base membership price should be greater than 1000"
)
;
}
break
;
case
3
:
double
a3=
num+
(
.3
*
num)
-
(
.05
*
num)
;
System
.
out.
println
(
"Cost for Diamond Membership is "
+
a3)
;
}
else
{
System
.
out.
println
(
"Base membership price should be greater than 1000"
)
;
}
break
;
case
4
:
double
a4=
num+
(
.4
*
num)
-
(
.05
*
num)
;
System
.
out.
println
(
"Cost for Platinum Membership is "
+
a4)
;
}
else
{
System
.
out.
println
(
"Base membership price should be greater than 1000"
)
;
}
break
;
case
5
:
System
.
out.
println
(
"Program terminated"
)
;
break
mainLoop;
default
:
System
.
out.
println
(
"Wrong choice!!"
)
;
}
}
}
}
Output Enter the base price to opt for a membership: 2000 ***Menu*** 1. Check cost for Silver Membership 2. Check cost for Gold Membership 3. Check cost for Diamond Membership 4. Check cost for Platinum Membership 5. Terminate the program Enter action number (1-5) 1 Cost for Silver Membership is 2100.0 ***Menu*** 1. Check cost for Silver Membership 2. Check cost for Gold Membership 3. Check cost for Diamond Membership 4. Check cost for Platinum Membership 5. Terminate the program Enter action number (1-5) 2 Cost for Gold Membership is 2300.0 ***Menu*** 1. Check cost for Silver Membership 2. Check cost for Gold Membership 3. Check cost for Diamond Membership 4. Check cost for Platinum Membership 5. Terminate the program Enter action number (1-5) 3 Cost for Diamond Membership is 2500.0 ***Menu*** 1. Check cost for Silver Membership 2. Check cost for Gold Membership 3. Check cost for Diamond Membership 4. Check cost for Platinum Membership 5. Terminate the program Enter action number (1-5) 4 Cost for Platinum Membership is 2700.0 ***Menu*** 1. Check cost for Silver Membership 2. Check cost for Gold Membership 3. Check cost for Diamond Membership 4. Check cost for Platinum Membership 5. Terminate the program Enter action number (1-5) 5 Program terminatedIn this article, we explored how to check the cost of membership in Java by using a menu driven approach.
Elements Of The Modern Hiring Process
Modern hiring processes have changed, and formerly unique strategies are now highly valued tactics to attract the best job applicants.
New hiring priorities consider diversity, remote work, expanded onboarding and other factors.
Hiring managers should avoid common hiring mistakes, like long processes, too many interviewers and a lack of transparency.
This article is for hiring managers who want to improve their recruitment processes and for job seekers who want to know what to expect during the hiring process.
The job market is constantly evolving, and hiring practices must adapt to allow organizations to recruit and retain the best talent and thus fuel business growth. Today’s hiring landscape involves terms like “the Great Resignation,” “quiet quitting” and the “Great Reshuffle,” signaling an increase in job vacancies and the need to hire engaged professionals who are willing to invest their talents in an organization. Additionally, shifts to remote work and digital employment platforms are changing how people apply for jobs and engage with career opportunities.
We’ll explore the latest hiring process trends to show job candidates what they can expect and help recruiters create a successful hiring and onboarding experience that meets candidates’ expectations and the company’s needs.
Elements of the modern hiring processThe process of hiring employees has shifted rapidly and evolved well beyond large teams of faceless panel interviews and “Don’t call us; we’ll call you” employer attitudes. Here are some essential elements of the hiring process that job seekers and employers should understand.
1. The shift to remote work is affecting the hiring process.The pandemic moved many employees to remote work plans, bringing telecommuting into the mainstream. This remote-work shift is showing no signs of dissipating. According to a FlexJobs survey, 80% of women and 69% of men said remote work is an important factor when they are considering job offers. Remote employees can avoid the toll of the work commute while enjoying more flexibility to care for their families and achieve a positive work-life balance.
Employees aren’t the only ones who benefit from remote work. Businesses see higher productivity levels from remote workers, higher employee retention and a more engaged workforce.
It makes sense that more employers are adding flexible, hybrid and remote work options to their job listings and adjusting their hiring processes to attract the best employees.
Tip
To start implementing flexible work policies that attract top talent, consider compressed workweeks, remote work during the holidays, and telework options for part-time staff.
6. Diversity has become an essential hiring consideration.The 2023 Recruiting Trends Report revealed that 57% of hiring managers prioritize finding diverse job candidates. Additionally, 59% say diversity, equity and inclusion (DE&I) is a top hiring trend, and 52% plan to hire diverse leadership.
Today’s businesses are using tech tools to enable a more diverse talent pool. For example, AI-assisted job listing creation and blind resume-reviewing tools aim to eliminate unconscious bias in the hiring process. Additionally, including diverse hiring team members helps companies identify potential biases they might have missed otherwise.
7. Companies are investing in onboarding to retain new hires.The hiring process doesn’t end after a hiring manager writes a job offer letter. The next hiring phase is onboarding, a crucial and often overlooked element in retaining talent.
Poor onboarding can result in lower productivity and morale. In contrast, a strategic and thoughtful first two weeks of work can give a new employee confidence and quickly get them up to speed.
Hiring managers today are working with HR to create detailed onboarding processes that instill an organization’s mission and values, along with explaining benefits and scheduling meetings and training sessions. Investing in your new hire’s first days will yield dividends for years to come.
Tip
Create an employee hiring checklist to standardize onboarding. Include customized steps for various roles, including managers, team members and seasonal employees.
Javascript Program To Find Closest Number In Array
We will write a JavaScript program to find the closest number in an array by comparing each element with the target number and keeping track of the closest one. The program will use a loop to go through each element in the array and use a conditional statement to compare the difference between the target number and the current element. If the difference is smaller than the current closest difference, we will update the closest number. The result of this program will be the closest number to the target in the given array.
ApproachThis program finds the closest number to a target value in an array of numbers −
Define a variable to store the difference between the target value and the current value in the loop.
Set the difference to a very high number, so that any number in the array will be smaller and become the new closest number.
Loop through the array of numbers, and for each number, calculate the absolute difference between the target value and the current number.
If the current difference is smaller than the stored difference, update the stored difference to the current difference and store the current number as the closest number.
Repeat the process for all numbers in the array.
After the loop, the closest number to the target value is the number stored in the variable.
ExampleHere is an example of a JavaScript function that takes an array of numbers and a target number as input and returns the closest number in the array to the target number −
function findClosest(numbers, target) { let closest = numbers[0]; let closestDiff = Math.abs(target - closest); for (let i = 1; i < numbers.length; i++) { let current = numbers[i]; let currentDiff = Math.abs(target - current); if (currentDiff < closestDiff) { closest = current; closestDiff = currentDiff; } } return closest; } const arr = [45, 23, 25, 78, 32, 56, 12]; const target = 50; console.log(findClosest(arr, target)); Explanation
The function findClosest takes two arguments: an array of numbers and a target number target.
We create a variable closest and set it equal to the first number in the numbers array, and assume that this is the closest number to the target.
We also create a variable closestDiff which calculates the difference between the target number and the closest number using Math.abs(). Math.abs() returns the absolute value of a number, ensuring that the difference is always positive.
We then use a for loop to iterate through the numbers array. For each iteration, we store the current number in the current variable and calculate the difference between the target and current number in currentDiff.
If currentDiff is less than closestDiff, we update closest to be current and closestDiff to be currentDiff.
Finally, the function returns the closest number to the target.
How To Set The Vertical Alignment Of The Content In An Element With Javascript?
In this tutorial, we will learn how to set the vertical alignment of the content in an element in JavaScript.
The vertical-align property of HTML DOM Style is used to set the vertical alignment of the content in an element.
The vertical-align attribute specifies or returns the vertical alignment of an element’s content. The vertical-align attribute controls the vertical alignment of an inline-block or table-cell box. The vertical-align attribute is used to align the box of an inline element within its line box vertically. It may, for example, be used to arrange a picture within a line of text vertically. It is also used to align the content of a table cell vertically.
Vertical-align only applies to inline, inline-block, and table-cell elements; it cannot be used to align block-level components.
Using the Style verticalAlign PropertyThe verticalAlign property sets or retrieves the vertical alignment of the content. The top property aligns the top of the element and its descendants with the top of the whole line.
Syntax document.getElementById("myTable").style.verticalAlign="top";The id of the table element is fetched using the getElementById() method, and the vertical alignment of the text is set to the top of the box.
ExampleWe used width and height components in this example to construct a box with a solid blue border. A certain text is written in the box. According to the default setting, the text is aligned in the middle. The top property of the vertical-align attribute is used to change this to the top of the box.
table
{
border
:
3
px solid blue
;
width
:
200
px
;
height
:
200
px
;
}
function
myFunction
(
)
{
document
.
getElementById
(
“myTable”
)
.
style
.
verticalAlign
=
“top”
;
}
Using Different Values in verticalAlign PropertyThe verticalAlign property specifies or returns the vertical alignment of an element’s content. The element is positioned in the center of the parent element via the middle property. The bottom attribute aligns the element’s bottom with the lowest element in the line.
Syntax document.getElementById("myTable").style.verticalAlign="middle"; document.getElementById("myTable2").style.verticalAlign="bottom";The table element’s id is obtained using the getElementById() function, and the text’s vertical alignment is set to the middle and bottom of the box.
Exampletable
{
border
:
3
px solid green
;
width
:
200
px
;
height
:
200
px
;
}
function
myFunction
(
)
{
document
.
getElementById
(
“myTable”
)
.
style
.
verticalAlign
=
“middle”
;
document
.
getElementById
(
“myTable1”
)
.
style
.
verticalAlign
=
“middle”
;
document
.
getElementById
(
“myTable2”
)
.
style
.
verticalAlign
=
“bottom”
;
document
.
getElementById
(
“myTable3”
)
.
style
.
verticalAlign
=
“bottom”
;
}
Using Bootstrap to set vertical alignmentBootstrap is a set of free and open-source tools for building responsive websites and online apps. It is the most widely used HTML, CSS, and JavaScript framework for creating mobile-first, responsive websites. Nowadays, webpages are optimized for all browsers (IE, Firefox, and Chrome) and screen sizes (Desktop, Tablets, Phablets, and Phones).
Vertical Alignment in Bootstrap alters the vertical alignment of items using vertical-alignment utilities. Vertical-align utilities only impact inline (Present in a single line), inline-block (Present as blocks on a single line), inline-table, and table cell (Elements in a table cell) elements.
SyntaxUsing the Bootstrap align attribute, the class is described to baseline, top, or middle.
ExampleIn this example, we have created a div element. The text is added to the class element, and the alignment of the content is changed accordingly, first to baseline, then to the top, middle, and bottom. The following text content is changed to text-top and text-bottom.
In this tutorial, we have learned how to set the vertical alignment of the content in an element using JavaScript. The vertical-align attribute is used to complete this task. The top, middle, and bottom properties are discussed in this tutorial. The vertical alignment using Bootstrap is also discussed.
Update the detailed information about Java Program To Replace Element Of Integer Array With Product Of Other Elements on the Minhminhbmm.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!