We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

quotient in loop

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

2
Contributors
4
Replies
17 Minutes
Discussion Span
4 Months Ago
Last Updated
5
Views
Johan__
Newbie Poster
20 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

tinstaafl
Nearly a Posting Virtuoso
1,476 posts since Jun 2010
Reputation Points: 429
Solved Threads: 261
Skill Endorsements: 14

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

Johan__
Newbie Poster
20 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

i fix it

Johan__
Newbie Poster
20 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Good, please mark it as solved. thank you.

tinstaafl
Nearly a Posting Virtuoso
1,476 posts since Jun 2010
Reputation Points: 429
Solved Threads: 261
Skill Endorsements: 14

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.1207 seconds using 2.72MB