Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class functions {
- private static final Scanner scanner = new Scanner(System.in);
- public static void main (String[] args) {
- int size1 = enterArraySize();
- int[] arr1 = inputArray(size1);
- float number1 = arrayToInt(size1, arr1);
- int size2 = enterArraySize();
- int[] arr2 = inputArray(size2);
- float number2 = arrayToInt(size2, arr2);
- divide(number1, number2);
- }
- public static int inputData (int min, int max) {
- int n = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- n = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a positive integer number:");
- }
- if (!isIncorrect && (n < min || n > max)) {
- isIncorrect = true;
- System.out.println("Please, enter a positive number:");
- }
- } while (isIncorrect);
- return n;
- }
- public static int enterArraySize() {
- System.out.println("Input number of digits: ");
- final int min = 1;
- final int max = 10;
- return inputData(min,max);
- }
- public static int[] inputArray(int n) {
- final int min = 1;
- final int max = 9;
- int[] arr = new int[n];
- System.out.println("Enter the numbers one by one: ");
- for (int i = 0; i < n; i++)
- arr[i] = inputData(min, max);
- return arr;
- }
- public static int arrayToInt(int n, int[] arr) {
- int c = 0;
- for (int i = 0; i < n; i++)
- c = c * 10 + arr[i];
- return c;
- }
- public static void divide(float number1, float number2) {
- float number = number1 / number2;
- int k = 1;
- int numberForLoopVirgin = (int) number;
- if (numberForLoopVirgin == 0)
- k = 0;
- else {
- while ((numberForLoopVirgin /= 10) > 0)
- k++;
- }
- System.out.println("How many decimal places do you want to output? (<= 6)");
- int decimalPlaces;
- final int min = 0;
- final int max = 7;
- decimalPlaces = inputData(min, max);
- for (int i = 0; i < decimalPlaces; i++)
- number = number * 10;
- int n = decimalPlaces + k;
- int[] arr = new int[n];
- int numberInt = (int) number;
- for (int i = 0; i < n; i++) {
- arr[i] = numberInt % 10;
- numberInt /= 10;
- }
- System.out.println("Result is: ");
- if (k == 0)
- System.out.println("0");
- for (int i = n - 1; i > decimalPlaces - 1; i--)
- System.out.println(arr[i]);
- if (decimalPlaces > 0) {
- System.out.println(",");
- for (int i = decimalPlaces - 1; i > -1; i--)
- System.out.println(arr[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement