1.11M Members

quotient in loop

 
0
 

hello

this is a homework so please dont give me the answer to the problem.

            int num1, num2;
            for (num1 = 1; num1 <= 101; num1++)
            {
                for (num2 = 1; num2 <= 101; num2++)
                {
                    if (num1 / num2 == 5)
                        Console.WriteLine(num1 + "/" + num2);
                }
            }
            Console.ReadLine();

this is what i have so far. the 2 numbers that com up first is right but then its relly get wrong. I want it to be every time the 2 numbers have the quotient of 5 the 2 numbers will com up on the screen. like if 10/2 = 5 then the 10/5 will com up.

sorry for my bad english

 
0
 

Since num1 and num2 are integers, I suspect the output of num1 / num2 is being cast as an integer, which is giving you a bunch of wrong outputs, since any output that equals 5 to 5.99999999 will be converted to 5, and equate to true, in if (num1 / num2 == 5). Try casting the output as double, that way only the outputs that equal 5.0, will equate to true.

 
0
 

it type out rigt the first to like
5/1
10/2
but then it write 11/2, 15/3 and so on

 
0
 

i fix it

 
0
 

Good, please mark it as solved. thank you.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: