Hello everyone! I am new to Java so this may be a very easy question.

I am trying to write a program and display just the remainder after two numbers have been divided. Here is where i am:

import java.util.Scanner;
public class Midterm1
{
public static void main( String[] args )
{

Scanner input = new Scanner( System.in );// starts scanner

int input1;// user inputed number 1
int input2;// user inputed number 2
int answer;
System.out.print( "Enter input1: "); //prompt
input1 = input.nextInt(); // read number from user

System.out.print( "Enter input2: "); //prompt
input2 = input.nextInt(); // read number from user
answer = input1 % input2;

if( input1 % input2 == 0)
System.out.println( "No Remainder" );

if( input1 % input2 != 0)
System.out.printf( "%s%d\n", "The Remainder is:", answer);

}
}


For some reason instead of displaying the remainder it is displaying "input1".

Thank you for you help!

On my computer it seems to be working fine. You should generally use the print command instead of printf -- it is much easier to read. Basically you just include whatever pieces you want and put them together with + signs:

System.out.print("The Remainder is: " + answer + "\n");

You should know that there are 2 methods you can use to do this -- print and println. println is a shortcut to writing a print method and then adding a "\n" at the end. For instance, the following 2 are equivalent:

System.out.print("hi\n");
System.out.println("hi");

Also, in general you should use code tags with your code. I suspect that your code may be giving you issues because of a printf problem.

The program compiles and runs fine but the answer it is returning at the end is wrong. Example: If i input 2 for the variable input1 and 3 for variable input2 the answer should be 66667 becasue 2/3=.6666667. So it should return the remainder 66667..... right? When I run the program with those numbers it returns the number 2. ????

Thank you for checking out my program! I will look into what you said about the print thing but I don't think that is why the program is returning the wrong value. I think my math may be off somewhere or maybe im using the wrong symbol or something. Thanks again for the help and any addition information would be greatly appreciated!

jtodd, you are thinking of division in terms of more advanced mathematics. Think back to grade 2 or 3 when you didn't know about decimals and the teacher was like "ok class, what is the remainder?" and you had to answer in a whole number? That is what x%y gives you.

Ok, I haven't thought of it like that. So, if 2 is input1 and 3 is input2 the answer(remainder) should be 1?? Is that right? If so my program is giving me 2.

2%3 is the remainder of 2/3, or "3 into 2". 3 goes into 2 zero times, but there are 2 left over, so the remainder is 2.

3%2 is the remainder of 3/2, or "2 into 3". 2 goes into 3 one time with one left over, so the remainder is 1.

Look up what a remainder is on google. Our job isn't to teach basic math.

Kvass... Thank you very much! I understand now!

Down vote my post all you want, but you shouldn't expect hand holding - being pointed towards the thousands of resources available online about remainders should have been enough. And next time you get help, please be considerate enough to follow our rules, such as posting in code tags and marking the thread solved when you solve your problem.

d

BestJewSinceJC- I did not down vote your post... Frankly I didn't even know we could do that. I am not expecting hand holding. I honestly didn't understand why I was coming up with the wrong answer. I joined this site because it looked like a great community to help learn among friendly, like minded people. I am not looking for criticism every time I post. I believe the friendly helpful attitude is what makes a site like this great! Once I have a better understanding of java and programing I hope to be able to contribute and help others understand much like Kvass has helped me. I apologies if I broke any rules set in place by the site... As I explained I am new to this site and I will be sure to read up on the rules before I repost. I will also work on marking this thread solved. Your not much of a ppl person are you? lol...just a joke.

Thank you to everyone that was helpful in this post! I hope you all have a wonderful day!

Your not much of a ppl person are you? lol

Sorry if I came off rude. . what I was saying is, there's nothing wrong with asking for help, but I think it is important to use the resources at your disposal (i.e. the internet) before asking people to re-explain things. But my annoyance with the down-voting was admittedly misplaced.

import java.util.Scanner;
public class Midterm1
{
public static void main( String[] args )
{

Scanner input = new Scanner( System.in );// starts scanner

int input1;// user inputed number 1
int input2;// user inputed number 2
int answer;
System.out.print( "Enter input1: "); //prompt
input1 = input.nextInt(); // read number from user

System.out.print( "Enter input2: "); //prompt
input2 = input.nextInt(); // read number from user
answer = input1 % input2;

if( input1 % input2 == 0)
System.out.println( "No Remainder" );

if( input1 % input2 != 0)
System.out.printf( "%s%d\n", "The Remainder is:", answer);

}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.