Question

how do i create a java method that takes two ints and divides first int in...

how do i create a java method that takes two ints and divides first int in the parameter by the second int using recursion and nno division or multiplication

Homework Answers

Answer #1

Hello, I have created the method with the comments where it is required. I have made main method to test our divide method. so it takes two parameters and it is recursive, where I have not use any kind of mulitplication or division.Also output is there also. please go through it and if you have any doubt, comment down. otherwise give me upvote.

Thank you and enjoy your code.

import java.util.Scanner;
import java.lang.*;

public class Division {
// For testinng the method
public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);
System.out.println("Enter two numbers seperated by space:");
int a = scnr.nextInt();
int b = scnr.nextInt();
// If any number is negative, then ans is also negative.
int sign = 1;
if(a*b < 0){
sign = -1;
}
// so at last we are calculating the division is multiplied by sign.
int ans = sign * divide(Math.abs(a),Math.abs(b));
System.out.println("Division is: "+ ans);
}

public static int divide(int a, int b){
if (a<b){
System.out.println("Reminder is: "+ a);
return 0;
}
// till a<b, this will execute.
// so every time divident is decreased by dividor and answer is increament by 1 every time
// and when divident will be less then divider, It will terminate with return 0 as mentioned above.
return 1 + divide(a-b,b);
}
}

// Output:

THank you. Hope you liked my answer.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Takes two ints as parameters. Determines the remainder when the first int is divided by the...
Takes two ints as parameters. Determines the remainder when the first int is divided by the second. If the remainder is 0 or 1 return true otherwise false. ----------------------------------------------- public class Class1 { public static boolean closeEnough (int i, int j) {     //Enter code here } }
Write a Java method that takes a String as Input and replaces the first two occurrencesof...
Write a Java method that takes a String as Input and replaces the first two occurrencesof a given character to another provided character.   For example, if the input String is “Java is Great” and asked to replace the letter a with x then the output would be: “Jxvx is Great” The method takes a String and two characters and returns the modified String. Please make your own assumptions if needed, you do not need to write the main.
Write a Java swap method that could swap values of two integer variables. Do not use...
Write a Java swap method that could swap values of two integer variables. Do not use any predefined method. Also write a method call that swaps the values of the following two variables. int first = 7, second = 5;
I am working on recursion. I need to create a reverseString method in which if i...
I am working on recursion. I need to create a reverseString method in which if i set the parameter like ReverseString(Maruf, 0) then the output will be furaM. But if i enter ReverseString(Maruf, 2) then the output will be raM. It is removing 2 characters from the reverse. I have to take 2 parameters. method signature is this public static String reverseString(String str, int i){ // Complete the method } Example: Starting word is Hello, reversed from index 2 is...
Java: How does Recursion change my view on repetition, and why do I need to be...
Java: How does Recursion change my view on repetition, and why do I need to be concerned about Optimality? As usual, Please comment on two of your classmates posts.
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input and output with the Java console. Setup: Create a class called ConsolePractice with a main method. Create a static field in your class that stores a scanner object. You will use this scanner for all user input. private static Scanner scanner = new Scanner(System.in); Part A: Create a static method called “divide”. Your method should do the following: Accept two integers as parameters, a...
I have a method that create the grid, but I want to use the grid and...
I have a method that create the grid, but I want to use the grid and change the grid in other method, how do I have the grid when I call the method 2. class: mian: If method 2 create_grid method method 2 method 3 java please
How do you code in Java a method (lets say its called PosOddAverage) that: - Takes...
How do you code in Java a method (lets say its called PosOddAverage) that: - Takes an array of doubles as input - Prints the avearge of positive odd numbers in the array - Prints 'ERROR' for empty input array An Example: Input: [1.2,1.5,0.0,-2.3,3.5,7.1,-1.3] Output: 4.03333333 Thanks :)
5) Write a java program with scanner object to input first number and second number as...
5) Write a java program with scanner object to input first number and second number as variable with input statement and system out print statement. Steps: 1) Declare scanner object 2) Ask system out print for first number and declare variable first number 3) Ask system out print for second number and declare variable second number 4) Declare first for loop where variable int i has already a value (your choice), that i is less then equal to ? and...
How do I convert this while loop to tail recursion? private void markForward (int row, int...
How do I convert this while loop to tail recursion? private void markForward (int row, int col, int distance,            int oldMarker, int newMarker) {       while (col+ distance < BOARD_SIZE) {        if(board[row][col+ distance] == oldMarker ) {            board[row][col+ distance] = newMarker;            //markForward (row, col, distance, oldMarker, newMarker);        }        if(row+distance < BOARD_SIZE && board[row+distance][col+distance] == oldMarker) {            board[row+distance][col+distance] = newMarker;       ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT