Using java make a program is to find all of the numbers between 1 and 10,000 tha
ID: 3628349 • Letter: U
Question
Using java make a program is to find all of the numbers between 1 and 10,000 that have a perfect median. A number n has a perfect median m if the sum of all the integers less than m is equal to the sum of all the integers greater than m, up to and including n. For example, the number n = 8 has the perfect median m = 6, because the sum of the integers from 1 to 5 is equal to the sum of the integers from 7 to 8. As you complete this assignment, you will learn about.You might make use of the formula 1+2+...+(m-1) = m(m-1)/2
Explanation / Answer
please rate - thanks
I didn't use the formula, used the definition. it takes a while to run
public class main
{ public static void main ( String[] args )
{int i,sum1,sum2,j,k;
for(i=1;i<=10000;i++)
{for(j=1;j<=i;j++)
{sum1=0;
sum2=0;
for(k=1;k<j;k++)
sum1+=k;
for(k=j+1;k<=i;k++)
sum2+=k;
if(sum1==sum2)
System.out.println(i+" has a perfect median of "+j);
}
}
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.