0

Math logic with variables and loop

I want to solve problem about my math homework (I'm middle-schooler). I have 2 variables : 'm' and 'n', I must find the values of each variable that will make m-n==4, Example : m = 6, n = 2, m-n = 6-2 = 4 Note : the value must be between 1 and 10 I keep failing on making a program that solve this problem, can you give me an example?? Using JAVA

7th Aug 2019, 11:54 AM
Fajar Kurniawan
5 Antworten
+ 1
Show us your failed attempt, post or link it here, then someone will give you a hint!
7th Aug 2019, 12:03 PM
HonFu
HonFu - avatar
+ 1
Hey Fajar, is there user input required? maybe for the value of <m> and <n> ? or the 4 is user input ?
7th Aug 2019, 12:28 PM
Ipang
+ 1
@Ipang no, no user input is required. And @Paul @HonFu [#GoGetThatBugChamp!] I got 2 and -2 as the result, that's not what I expected neither what it should be to solve the math problem. My Code somehow looks like this : https://code.sololearn.com/cLlUD1uMhRzo/#java
7th Aug 2019, 2:03 PM
Fajar Kurniawan
+ 1
@Fajar okay ... Since the condition is m - n = 4, first check which is greater between <m> and <n>, if <n> is greater than <m> we need to swap <m> and <n>, to prevent negative result from subtraction. <m> be at least equal to 4, because by then <n> will be zero; <m>(4) - <n>(0) = 4. Also see that <m> will be at least equal to 4 + <n>. I wrote this method, but I think it may not be what you want, it displays all possible values of <m> and <n> where their subtraction results as 4. The method requires an argument <value>, in your case it is 4. Hope this gives you an idea to solve the case 👍 private static void task(int value) { for (int m = value, n = 0; m <= 10; m++, n++) { System.out.println(m + " and " + n); } System.out.println(); } (Edit) I think I don't clearly understand what the program was supposed to do. Maybe you can provide more examples with different number?
7th Aug 2019, 2:50 PM
Ipang
0
Hint: instead of trying to find m and n at the same time. Pick a value for m and check if the value of n satisfying n-m == 4 is in your valid range.
7th Aug 2019, 12:20 PM
Paul