Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners how to calculate max and min in the loop

Have Programming Question? Ask it here!
 
 
 


Asri
how to calculate max and min in the loop
4 Answer(s)      6 years ago
Posted in : Java Beginners



Advertisement
View Answers

August 11, 2008 at 1:49 PM


import java.io.*;
public class max_min1
{

public static void main(String a[]) throws IOException
{
int max,min;
max=Integer.parseInt(a[0]);
min=Integer.parseInt(a[0]);
for(int i=0;i<a.length;i++)
{
if(Integer.parseInt(a[i])<min)
min=Integer.parseInt(a[i]);
if(Integer.parseInt(a[i])>max)
max=Integer.parseInt(a[i]);
}
System.out.println("MAX: "+max+" MIN: "+min);
}
}


August 11, 2008 at 1:53 PM


here...a is an array....the data type is String....
This pgm takes values as command line...
the first value is assignd to both min and max variables....
now, in a loop...till the end of the array...check each values against the max and min variables....then assign accordding to the comparison...
we have to convert this array values into integer.


August 11, 2008 at 2:08 PM


Hi friend,

Import the java input output packages

import java.io.*;



public class MaxMinFun {
public static void main(String[] args) throws IOException
{

// Max Number to be inputted

System.out.println("Enter Number to be input: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = in.readLine();

// Enter the Number and store it in array;

int arlen = Integer.parseInt(str.toString());
int[] arr = new int[arlen];
for(int i=0;i<arlen;i++)
{
str = in.readLine();
arr[i] = Integer.parseInt(str.toString());
}

//code to calculate the maximum number :

int arrayMax[]= arr;
int max = arrayMax[0];
for (int i=0; i<arrayMax.length; i++)
{
if(max<arrayMax[i])
{
max = arrayMax[i];
}
}
System.out.println("max = " + max);

//code to calculate the minimum number :

int arrayMin[]= arr;
int min=arrayMin[0];
for (int i=1; i<arrayMin.length; i++)
{
if(min>arrayMin[i])
{
min = arrayMin[i];
}
}
System.out.println("min = " + min);

}
}

Thanks




August 11, 2008 at 2:08 PM


is it the answer u r looking for....
Then if u got a better one, plz inform me also...
thanks and reg..
prashu..
prashobvee@gmail.com


Related Tutorials/Questions & Answers:
how to calculate max and min in the loop - Java Beginners
how to calculate max and min in the loop  thanks coz giving me... and min value in the loop. the input is from the user. could u teach me. thanks... main(String a[]) throws IOException { int max,min; max=Integer.parseInt
how to calculate max and min - RUP
how to calculate max and min  hye!i'm a new for the java and i want to ask a question . Could anyone help me to teach how to calculate the maximum... function max and min in java. public class MathMaxMin { public static
Advertisements
Using Min, Max and Count on HQL.
Using Min, Max and Count on HQL.  How to use Min, Max and Count on HQL
Mysql Min Max
Mysql Min Max       Mysql Min Max is useful to find the minimum and maximum records from a table. Understand with Example The Tutorial illustrate an example from Mysql Min Max
hibernate criteria Max Min Average Result Example
hibernate criteria Max Min Average Result Example In this Example, We... the Projections class max, min ,avg methods. Here is the simple Example code files...("Max Salary is : "); System.out.print(row[i]); System.out.printf("\n
How to get max date in Hibernate?
How to get max date in Hibernate?  Hi, Tell the code to get the max... in Hibernate for getting the max date. Here is example code: Projections.max("addmision_date") Check the complete source code at Hibernate Criteria Max Date Example
Max()
Max()  How to find maximum values A101,A107,A108 from a table in sql
Max()
Max()  How to find maximum values A101,A107,A108 from a table in sql
How to calculate days in a year
How to calculate days in a year  Hi, How to calculate no of days in a year? I want a program that calculates no of days in a year by specifying year a parameter. Thanks   Hi, Please check the thread example
How to calculate attending hours?
How to calculate attending hours?  I need to calculate attending hours of a employee in a day in my project to make their monthly salary . By using work starting time and ending time. How should I do it using Date class? Or any
Find max and min value from Arraylist
Find max and min value from Arraylist In this tutorial, you will learn how... max() and min() respectively. Here is an example that consists of ArrayList of integers and using the max and min methods of Collections class, we have find
How to calculate the average in Hibernate?
How to calculate the average in Hibernate?  Hi, I have to calculate the average in Hibernate. How to calculate the average in Hibernate? Thanks   Hi, You can use the project in Hibernate. Here is the example code
how to calculate EMI of the loan
how to calculate EMI of the loan   package carloanapp; import java.io.FileWriter; import java.io.IOException; public class CarLoan { Customer gObjCustomer = null; double gDbRequestedLoanAmount = 0; double
how many max. arguments we pass to the any function ? how many max. arguments we pass to the any function ?
how many max. arguments we pass to the any function ? how many max. arguments we pass to the any function ?   how many max. arguments we pass to the any function
How to write a loop and a while loop
How to write a loop and a while loop  How do I write a 1 loop and a 1 while loop for the example code: public boolean isTheFirstOneBigger (int num1, int num2) { if (num1 > num2) { return true
how to calculate the price on the option box
how to calculate the price on the option box  How i calculate the value when i using a option box with 2 option..first option i used for product name... to calculate this value
How to calculate area of rectangle
How to Calculate Area of Rectangle       In this section we will learn how to calculate area... explain the static method how to display the rectangle values. First of all we have
loop to input multiple points to calculate final distance and speed
loop to input multiple points to calculate final distance and speed  import java.util.Scanner; public class Travel { private double time; private double distance; public Travel() { time=0; distance=0
how to calculate the employee tax - JSP-Servlet
how to calculate the employee tax  How to calculate the employee tax.Can u please send to me calculation tables
How to calculate sum of HH:MM:SS Format?
How to calculate sum of HH:MM:SS Format?  how to calculate sum of HH:MM:SS? for example: 10:25:36+26:50:56 calculate sum of these values
how many max. arguments we pass to the any function ?
how many max. arguments we pass to the any function ?   how many max. arguments we pass to the any function
Min Value
Min Value   How to access min value of column from database table... min(id) from QuestionModel result_info"; Query query = session.createQuery(selectQuery); System.out.println("Min Value : " +query.list().get(0
how to calculate salary of all the employees 10%extra then actual salary - SQL
how to calculate salary of all the employees 10%extra then actual salary  how to calculate salary of all the employees 10% extra then actual salary  Hi Friend, We are providing you a code that will calculate
How to calculate number of weeks in a specified month
How to calculate number of weeks in a specified month  I am create one program that contain three combo box. 1.cmbyear 2.cmbmonth 3.cmbweek i am select year and month. then automatically calculate number of weeks in a specified
How to calculate area of triangle
.style1 { margin-right: 0px; } How to Calculate...; In this section we will learn how to calculate area of triangle. We... in this program we will fine the how to the display massage the area
Hibernate Min() Function (Aggregate Functions)
(...), min(...), max(...) , count(*), count(...), count(distinct ...), count(all... Hibernate Min() Function (Aggregate Functions)       In this section, we will show you, how to use
javascript max size
javascript max size   How to retrive Max size in JavaScript
How does the Java for each loop work?
How does the Java for each loop work?  How does the Java for each loop work
JDBC Select Max Example
JDBC Select Max Example In this tutorial we will learn how use  MAX... values   that  pass in Max function  i.e  "SELECT MAX... query "SELECT MAX(user_id) FROM user"  fetch the  maximum
min project with vb - WebSevices
min project with vb  i have do the min project with VB Frantend and oracle backend
how can i calculate loan - Java Interview Questions
how can i calculate loan  negotiating a consumer loan is not always.... the program should then calculate the face value required in order for the consumer to receive the amount needed. it should also calculate the monthly payment
How to use for loop in jsp
How to use 'for' loop in jsp page ? This is detailed java code that shows how to use 'for' loop in jsp page. 'if' statement is used to test conditions while 'for' loop is used to define a loop that reiterates statements. It specifies
for loop
for loop  how to performs the for loop when 2 inputs given like displays the triangle shape and how to take initial values or declaration values plz explain in detailed ex. 1 12 123 1234
How to use 'for' loop in jsp page?
How to use 'for' loop in jsp page?       This is detailed java code that shows how to use 'for' loop in jsp page. 'if' statement is used to test conditions while
how can you calculate you your age in daies??
how can you calculate you your age in daies??  **hi, I am beginner in java! can any one help me to write programm to calculate age in daies???**   Hi Friend, Try the following code: import java.util.*; public
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....   */ import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....   */ import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....   */ import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....   */ import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
how to i convert this case to loop...please help.....
how to i convert this case to loop...please help.....  import..."); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit...(); case 'q': Quit(); case 'Q': Quit(); break
How to call Array Length for loop in PHP
How to call Array Length for loop in PHP  Hello, I am learner in PHP scripting Language. How can I use the array length for loop in PHP programming language? Kindly provides online help guide or reference links. Thanks
 

 

 

DMCA.com