

 Thread Tools  Search this Thread 
#1
 
 
how to find median of given number in java? Hi, I am new to programming language. I recently started learning java language. Yesterday our sir has given us program like find median of given numbers in java?. I use various method but I unable to write that program. That's why I asked this question on this forum. So if you have any idea about this please help me. 
#2
 
 
Re: how to find median of given number in java? Here I posted code for finding median, try to compile in your computer. Code: import java.util.Scanner; public class arraytesting{ public static void main(String args[]) { Scanner p=new Scanner(System.in); System.out.print("Enter the numbers : "); int s=p.nextInt(); int[] arr1=new int[s]; System.out.print(arr1.length+"indexes\n"); for(int p=0;p<size;p++){ System.out.print("enter an integer number: "); int num=p.nextInt(); arr[p]=num; } int p; for( p=0;p<size;++p){//for p<s int count=0; for (int t=0;t<=s;++t){ if(arr1[p]>arr1[p]){ count++; } } if(count==arr1.length/2){ break; } System.out.print(arr1[p]); } } } 
#3
 
 
Re: how to find median of given number in java? Here is simple code to find median. Try to understand each line. Code: package net.sourceforge.jiu.util; public class MedianNum { private MedianNum() { } public static void swaping(int[] p, int s1, int s2) { int temp1 = p[si1]; p[s1] = p[s2]; p[s2] = temp1; } public static int find(int[] p, int from, int to) { int lowvalue = from; int highvalu = to; int med = (lowvalu + highvalu) / 2; do { if (highvalu <= lowvalu) { return a[med]; } if (highvalu == lowvalu + 1) { if (a[lowvalu] > a[highvalu]) { swaping(p, lowvalu, highvalu); } return p[med]; } int middle = (lowvalu + highvalu) / 2; if (p[middle] > p[highvalu]) { swaping(a, middle, highvalu); } if (p[lowvalu] > p[highvalu]) { swaping(p, lowvalu, highvalu); } if (p[middle] > p[lowvalu]) { swaping(p, middle, lowvalu); } swaping(p, middle, lowvalu + 1); int lowl = lowvalu + 1; int highh = highvalu; do { do { lowl++; } while(a[lowvalu] > p[lowl]); do { highh; } while(a[highh] > p[lowvalu]); if (highh < lowl) { break; } swaping(p, lowl, highh); } while(true); swaping(p, lowvalu, highh); if (highh <= median) { lowvalu = lowl; } if (highh >= med) { highvalu = highh  1; } } while(true); } } 
#4
 
 
Re: how to find median of given number in java? Here I am not writing any code to find median of given numbers rather I just given you logic behind it. Let's see how we have to find median of given number.If there are 7 numbers then the median will be at number 4 and if there are 8 numbers then also median will be at number 4. Use following formula to get logic behind it: MedianPosition = (nuberofitems+1) / 2 It means that if numberofitems is 5 then median will be 3. I think this will help you. 

Tags: array, for loop, java, java compiler, median number 
Thread Tools  Search this Thread 

Similar Threads for: "how to find median of given number in java?"  
Thread  Thread Starter  Forum  Replies  Last Post 
Java : test a number to find out if its is even or odd  teenQ  Software Development  4  06042012 06:42 AM 
Determine a whole number in java  Remedy  Software Development  5  25022010 04:39 AM 
How to format a number in java?  kamina23  Software Development  4  20012010 09:01 PM 
Program : How to calculate the median in C++?  UseME  Software Development  3  19012010 12:08 PM 
How to write java program to find factorial of number?  Balamohan  Software Development  5  28112009 10:14 PM 