Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why is my quotient's decimal i.e after dot coming 0 and not precise value

+2 votes
asked Sep 8, 2020 by Renuka Aole (140 points)

Write C program to perform division operation on two integer numbers and also print remainder of it.

  Input                        Output

Test case 1        44                            14.6667

                           3                              2           

         

Test case 2        6                               0.8571

                          7                               6

2 Answers

+1 vote
answered Sep 10, 2020 by Peter Minarik (84,720 points)

In most programming languages we differentiate between integral and floating point numbers.

If you do integral operation (+, -, *, /, %, etc) the result is also an integral number.

Similarly, the same arithmetic operations applied of floating point numbers will yield floating point results.

E.g.:

  • 3/2 = 1 (only integral parts are kept for integral arithmetic)
  • 3.0 / 2.0 = 1.5. (floating point arithmetic)
  • 4/2 = 2 (integral arithmetic)
  • 4.0 / 2.0 = 2.0 (and not 2 -- floating point arithmetic)
  • 4 / 2.0 = 2.0 (if integrals and floating points are mixed, the result is floating point)
You can google around to see how exactly integral and floating point arithmetic work in C.
I hope this helped.
0 votes
answered Sep 30, 2020 by Amar Kulkarni (140 points)

Type cast one integer to float.

quotient = (flaot*)integer1/integer 2;

commented Oct 2, 2020 by Peter Minarik (84,720 points)
Did you mean (float) instead of (flaot*)?

(There's no such thing as "flaot". Also, why the pointer?)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...