﻿ Java: Divide the two integers using subtraction operator

# Java Exercises: Divide the two specified integers using subtraction operator

## Java Basic: Exercise-164 with Solution

Write a Java program to divide the two given integers using subtraction operator.

Pictorial Presentation: Sample Solution:

Java Code:

``````import java.util.Scanner;
public class Solution {
public static float divide_using_subtraction(int dividend, int divider) {
if (divider == 0) {
return 0;
}
float result = 0;
while (dividend > divider) {
dividend -= divider;
result++;
}
float decimalPart = (float) dividend / (float) divider;
result += decimalPart;
return result;
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input the dividend: ");
int dividend = in.nextInt();
System.out.print("Input the divider: ");
int divider = in.nextInt();
System.out.println("\nResult: " + divide_using_subtraction(dividend,divider));
}
}
```
```

Sample Output:

```Input the dividend:  625
Input the divider:  25

Result: 25.0
```

Flowchart: Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## Java: Tips of the Day

Java: Anagrams

Generates all anagrams of a string.

```public static List<String> anagrams(String input) {
if (input.length() <= 2) {
return input.length() == 2
? Arrays.asList(input, input.substring(1) + input.substring(0, 1))
: Collections.singletonList(input);
}
return IntStream.range(0, input.length())
.mapToObj(i -> new SimpleEntry<>(i, input.substring(i, i + 1)))
.flatMap(entry ->
anagrams(input.substring(0, entry.getKey()) + input.substring(entry.getKey() + 1))
.stream()
.map(s -> entry.getValue() + s))
.collect(Collectors.toList());
}
```