Getting answer as remainder instead of quotient - java

When I run it I get this:
Enter 2 Integers, Do note that you will Get a Quotient and a Remainder.
Enter the First Number: 3
Now enter the Second Number: 9
Your Quotient is: 0
Your Remainder is: 3
It should be:
Enter 2 Integers, Do note that you will Get a Quotient and a Remainder.
Enter the First Number: 3
Now enter the Second Number: 9
Your Quotient is: 3
Your Remainder is: 0
Code:
import java.util.Scanner;
public class Remainder {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int a = 0, b = 0, quotient, remainder;
int smallerNumber = 0;
int biggerNumber = 0;
System.out.println("Enter 2 Integers, Do note that you will Get a Quotient and a Remainder.");
System.out.print("Enter the First Number: ");
a = reader.nextInt();
System.out.print("Now enter the Second Number: ");
b = reader.nextInt();
remainder = (a % b);
quotient = (a / b);
remainder = (a % b);
if(a > b){
biggerNumber = a;
a = b;
}else{
smallerNumber = a;
biggerNumber = b;
}
System.out.print("Your Quotient is: ");
System.out.println(quotient);
if (remainder > 0){
System.out.print("Your Remainder is: ");
System.out.println(remainder);
}
}
}

You are entering the numbers in the wrong order. You are doing a / b, and 3 / 9 in integer math is zero. Enter 9 first, and 3 second, and things will work.
Note also that you are looking for the bigger and smaller number after computing the remainder and quotient (and you compute the former twice). Swap the order of the code, and divide bigger/smaller for better results...
System.out.println("Enter 2 Integers, Do note that you will Get a Quotient and a Remainder.");
System.out.print("Enter the First Number: ");
a = reader.nextInt();
System.out.print("Now enter the Second Number: ");
b = reader.nextInt();
if(a > b){
biggerNumber = a;
smallerNumber = b;
}
else {
smallerNumber = a;
biggerNumber = b;
}
remainder = biggerNumber % smallerNumber;
quotient = biggerNumber / smallerNumber;

You have a and b backwards. Instead,
quotient = b / a;
remainder = b % a;
Also, a and b should probably be renamed to first and second...

Related

How to reverse the order of an int without turning it into a string

I have been tasked with the assignment of creating a method that will take the 3 digit int input by the user and output its reverse (123 - 321). I am not allowed to convert the int to a string or I will lose points, I also am not allowed to print anywhere other than main.
public class Lab01
{
public int sumTheDigits(int num)
{
int sum = 0;
while(num > 0)
{
sum = sum + num % 10;
num = num/10;
}
return sum;
}
public int reverseTheOrder(int reverse)
{
return reverse;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Lab01 lab = new Lab01();
System.out.println("Enter a three digit number: ");
int theNum = input.nextInt();
int theSum = lab.sumTheDigits(theNum);
int theReverse = lab.reverseTheOrder(theSum);
System.out.println("The sum of the digits of " + theNum + " is " + theSum);
}
You need to use the following.
% the remainder operator
/ the division operator
* multiplication.
+ addition
Say you have a number 987
n = 987
r = n % 10 = 7 remainder when dividing by 10
n = n/10 = 98 integer division
Now repeat with n until n = 0, keeping track of r.
Once you understand this you can experiment (perhaps on paper first) to see how
to put them back in reverse order (using the last two operators). But remember that numbers ending in 0 like 980 will become 89 since leading 0's are dropped.
You can use below method to calculate reverse of a number.
public int reverseTheOrder(int reverse){
int result = 0;
while(reverse != 0){
int rem = reverse%10;
result = (result *10) + rem;
reverse /= 10;
}
return result;
}

Need Explaination : Adding Two Binary Numbers in Java

Can anyone please explain this code?
Programme: How to add two binary number.
explain below the asterix sign
import java.util.Scanner;
public class Exercise17 {
public static void main(String[] args)
{
long binary1, binary2;
int i = 0, remainder = 0;
int[] sum = new int[20];
Scanner in = new Scanner(System.in);
System.out.print("Input first binary number: ");
binary1 = in.nextLong();
System.out.print("Input second binary number: ");
binary2 = in.nextLong();
while (binary1 != 0 || binary2 != 0)
{
// explain code below
// what does %10 means?
// why %2?
sum[i++] = (int)((binary1 % 10 + binary2 % 10 + remainder) % 2);
remainder = (int)((binary1 % 10 + binary2 % 10 + remainder) / 2);
binary1 = binary1 / 10;
// why to divide by /10?
binary2 = binary2 / 10;
}
if (remainder != 0) {
sum[i++] = remainder;
}
--i;
System.out.print("Sum of two binary numbers: ");
while (i >= 0) {
System.out.print(sum[i--]);
}
System.out.print("\n");
}
}
%10 means remainder of division by 10.
%2 is remainder of division by 2.
/ 10 is basically shift by one digit.
Imagine you have two "binary" numbers 101 and 110. %10 will get you the last digit for each of the numbers (1 and 0). Then you sum these digits and %2 gets you the digit of the sum.
The best way to understand the code is to run it in a debugger.

Does anyone have an idea of printing the values in reverse in this code I wrote?

Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, the program should: output the individual digits of 3456 as 3 4 5 6 and the sum as 18, output the individual digits of 8030 as 8 0 3 0 and the sum as 11, output the individual digits of 2345526 as 2 3 4 5 5 2 6 and the sum as 27, and output the individual digits of 4000 as 4 0 0 0 and the sum as 4.
Moreover, the computer always adds the digits in the positive direction even if the user enters a negative number. For example, output the individual digits of -2345 as 2 3 4 5 and the sum as 14.
This is the question I'm having minor difficulties with, the only part I can't figure out is how can I print the single integers in the order that he wants, from what I learned so far I can only print them in reverse. Here's my code:
import java.util.*;
public class assignment2Q1ForLoop {
static Scanner console = new Scanner (System.in);
public static void main(String[] args) {
int usernum, remainder;
int counter, sum=0, N;
//Asaking the user to enter a limit so we can use a counter controlled loop
System.out.println("Please enter the number of digits of the integer");
N = console.nextInt();
System.out.println("Please enter your "+N+" digit number");
usernum = console.nextInt();
System.out.println("The individual numbers are:");
for(counter=0; counter < N; counter++) {
if(usernum<0)
usernum=-usernum;
remainder = usernum%10 ;
System.out.print(remainder+" ");
sum = sum+remainder ;
usernum = usernum/10;
}
System.out.println();
System.out.println("the sum of the individual digits is:"+sum);
}
}
You have to storeremainder variables in an array and then print them in the loop from last index to first as shown in this tutorial.
You can either store digits in array and then print them, or you can try something like that:
final Scanner console = new Scanner(System.in);
System.out.println("Please enter your number");
final int un = console.nextInt();
long n = un > 0 ? un : -un;
long d = 1;
while (n > d) d *= 10;
long s = 0;
System.out.println("The individual numbers are:");
while (d > 1) {
d /= 10;
final long t = n / d;
s += t;
System.out.print(t + " ");
n %= d;
}
System.out.println();
System.out.println("the sum of the individual digits is:" + s);
An idea would be : convert int to string and write a method
getChar(int index) : String
which gives you for example 4 from 3456 with
getChar(2);
See Java - Convert integer to string
Here, I wrote a code for your problem with using a stack. If you want a simple code, you can comment my solution and I will wrote another one.
Scanner c1 = new Scanner(System.in);
System.out.print("Enter the number: ");
int numb = c1.nextInt();
numb = Math.abs(numb);
Stack<Integer> digits = new Stack<Integer>();
while(numb>0){
int n = numb%10;
digits.push(n);
numb = numb/10;
}
int sum = 0;
while(!digits.isEmpty()){
int n = digits.pop();
sum+=n;
System.out.print(n+" ");
}
System.out.print(sum);

Conversion from Base 10 to Base 2

I am trying to convert a number in base 10 to a number in base 2, but I have a problem. When I run the code, I get the base 2 number in the wrong order. For example, I input 54 and get 110110 instead of 011011, the correct value.
import java.util.Scanner;
public class DecimalToBinary
{
public static void main(String arg[]){
int quotient;
int remainder;
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter a decimal number:");
quotient = keyboard.nextInt();
do {
remainder = quotient % 2;
quotient = quotient / 2;
// String x = String.valueOf(remainder);
// System.out.print(x);
System.out.print (remainder);
} while (quotient != 0);
}
}
Java has a built-in method to do this:
Integer.toString(int i, int radix);
where
i is your base ten number, and
radix is the base you want it in, in your case, 2.
It will return a string in binary.
Another inbuilt java function for this is
Integer.toBinaryString(int num)
where num is the base 10 number you want to convert
Base 10 to Base 2 should Reverse output,I think you can use array,then reverse output array
public static void main(String arg[]){
int quotient;
int remainder;
List<Integer> arrayList=new ArrayList<Integer>();
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a decimal number:");
quotient = keyboard.nextInt();
do {
remainder = quotient % 2;
quotient = quotient / 2;
// String x = String.valueOf(remainder);
// System.out.print(x);
// System.out.print (remainder);
arrayList.add(remainder);
} while (quotient != 0);
ListIterator<Integer> li;
for (li = arrayList.listIterator(); li.hasNext();) {// 将游标定位到列表结尾
li.next();
}
for (; li.hasPrevious();) {// 逆序输出列表中的元素
System.out.print(li.previous() + " ");
}
}
try this solution, its simple:
int quotient;
int remainder;
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter a decimal number:");
quotient = keyboard.nextInt();
Stack<Integer> s=new Stack<Integer>();
do {
remainder = quotient % 2;
quotient = quotient / 2;
s.add(remainder);
// String x = String.valueOf(remainder);
// System.out.print(x);
//System.out.print (remainder);
} while (quotient != 0);
while(!s.isEmpty()){
System.out.print(s.pop());
}
I was able to fix your code
public class CustomDecimalToBinary {
public static void Dec2Bin() {
int quotient;
int remainder;
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter a decimal number:");
quotient = keyboard.nextInt();
do {
remainder = quotient % 2;
// String x = String.valueOf(remainder);
// System.out.print(x);
System.out.print (remainder);
} while ((quotient /= 2) != 0);
}
}
Example output:
Please enter a decimal number:
54
011011
EDIT: It appears I solved the problem to get your desired output but your code produces the correct output...

Issues while executing Armstrong number program

I am trying out a code that finds out whether a number entered is Armstrong or not. Here is the Code:
import java.util.*;
public class Arm {
int a, b, c;
void m1() {
Scanner obj = new Scanner(System.in);
System.out.println("Enter a number");
int number = obj.nextInt();
number = (100 * a) + (10 * b) + (1 * c);
if ((a * a * a) + (b * b * b) + (c * c * c) == number) {
System.out.println("number is armstrong");
} else {
System.out.println("number is not armstrong");
}
}
public static void main(String args[]) {
Arm obj = new Arm();
obj.m1();
}
}
Here the value of a,b and c comes out to be zero. But that is not the correct result. Say if we enter a number 345. Then a,b and c should be 3, 4 and 5 respectively.
Please guide.
That is not how you calculate a, b, c.
To find a,b,c we repeatedly divide by 10 and get the remainder by modulus.
int digit = 0;
int sum = 0;
while(num > 0)
{
digit = num % 10;
sum += Math.pow(digit, 3);
num = num/10;
}
Why do we use / and %
Consider 345.
Now to get the last digit what can be done?
What does a modulus return? The remainder, so If we perform %10 we get the last digit.
345 % 10 = 5
Now we want the second last digit.
So we divide the number by 10, so we get the quotient
345 / 10 = 34
Now again if we can perform the modulus we get the 4 and so on..
What does 100 * a + 10 * b + 1 * c do?
That is used to get a number if we have the individual digits.
Suppose we have 3, 4, 5 we know that we get 345 out of it but how do we do it?
3 * 100 = 300
4 * 10 = 40
5 * 1 = 5
-----------
300 + 40 + 5 = 345
Now to complete your whole program.
public boolean isAmg(int num)
{
int digit = 0;
int sum = 0;
int copyNum = num; //used to check at the last
while(num > 0)
{
digit = num % 10;
sum += Math.pow(digit, 3);
num = num / 10;
}
return sum == copyNum;
}

Categories

Resources