Hi, I'm having some major problems with the following question of a java assignment. For some reason, the program I wrote works for some values, but not for others. The program works fine for sets of values such as (second number is the divisor): 4 and 2, 1 and 2, 3 and 4, 4 and 7 Some of the many sets of values that don't work include: 1 and 100, 5 and 7 (a weird one,this one should've worked..), 4 and 5. Modifying my program (ie fixing it) would be greatly appreciated. Thanks in advance. If at all possible, an explanation of what was changed would also be appreciated. (If you have enough time or if it won't take too long).
The Assignment Question: Create a Java program containing a method named digits(). This method will have two parameters of type long. The first parameter will be divided by the second parameter, the divisor. The method will output the result of the division, with the result either terminating or repeating. If it repeats, digits() will output two groups of size N, where N is one less than the divisor. The driver (the main method) will input two positive integers from the user, rejecting negative inputs and not allowing the second value to be 0. Use a loop that will repeat until the first value is 0.
My "incomplete" but executable program:
import java.awt.*; import hsa.Console;
public class Assign4_4 { static Console c; static long num1,num2,value1 = 0,value2 = 0,result1,result2,r,F = 0,N,A,count;
public static void main(String [] args) { c = new Console();
c.setTextColor(Color.red); c.println("\n[Assign4_4]"); c.setTextColor(Color.gray); c.println("\nDescription of Program: "); c.setTextColor(Color.blue); c.println("This Program will ask for two positive integers of type long and will output the"); c.println("division between the first value and the second value, along with an indication of the result being either terminating or repeating."); c.println("Negative values and entering 0 as the second value will be rejected."); c.println("The Program ends when the first value entered is equal to 0."); c.setTextColor(Color.black);
while(true) { result1 = 0; result2 = 0; c.println("\nPlease enter the first value: "); num1 = c.readLong(); if(num1 == 0) break; else if(num1 < 0) { c.println("Invalid input. Please enter a positive integer."); num1 = c.readLong(); } c.println("Please enter the second value: "); num2 = c.readLong(); if(num2 < 1) { c.println("Invalid input. Please enter a positive integer greater than 0."); num1 = c.readLong(); } value1 = num1; value2 = num2; result1 = digits(num1,num2); if(F == 1) { c.print("\n"); c.print(value1); c.print(" divided by "); c.print(value2); c.print(" is "); c.print("0."); c.print(result1); c.println("\nThe result consists of terminating decimals."); } else if(F == 2) { c.print("\n"); c.print(value1); c.print(" divided by "); c.print(value2); c.print(" is "); c.print("0."); c.print(result1); c.println("\nThe result consists of repeating decimals."); } else if(F == 3) { result2 = num1/num2; c.print("\n"); c.print(value1); c.print(" divided by "); c.print(value2); c.print(" is "); c.print(result2); c.println("\nThe result is a whole number."); } } }
public static long digits(long num1, long num2) { r = num1%num2; if(r == 0) { F = 3; return r; } A = num1*10/num2; while(true) { result2 = num1*10/num2; r = num1*10%num2; result1 = result1*10 + result2; num1 = r; if(r == 0) { F = 1; return result1; } else if(r == A) { F = 2; count = 0; while(count<(((num2 - 1) * 2)-1)) { result2 = num1*10/num2; r = num1*10%num2; result1 = result1*10 + result2; num1 = r; count++; } return result1; } } } }