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...
I would like to know how to implement the extended euclidean algorithm in Java which takes...
I would like to know how to implement the extended euclidean algorithm in Java which takes 2 integers, public int xgcd(int a, int b) and returns the multiplicative inverse of 'a' modulo 'b' and if the multiplicative inverse of 'a' does not exist it should return -1. I know to do it in a recursive manner but I need help with the non-recursive or iterative manner without using biginteger.math. Something like using the pseudocode would do. I would really appreciate...
Please code C# Write a method with an int return type that has two int parameters....
Please code C# Write a method with an int return type that has two int parameters. The method returns the larger parameter as an int. If neither is larger, the program returns -1. Call this method three times, once with the first argument larger, once with the second argument larger, and once with both arguments equal
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...
Java Generic Linked List Problem: How do I remove a slice of a linked list and...
Java Generic Linked List Problem: How do I remove a slice of a linked list and add its data to another linked list in java? Example: Private<T> head; Private int size; Public List<T> slice(int from, int to) { // This method will remove the data from the given range (inclusive) and add it to a new List and return this new list. }
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
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT