Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
C++ Program to compute division upto n decimal places
Given with the value of x and y as a positive integer with the value of n for number of decimal places and the task is to generate the division up to n decimal places.
Example
Input-: x = 36, y = 7, n = 5 Output-: 5.14285 Input-: x = 22, y = 7, n = 10 Output-: 3.1428571428
Approach used in the below program is as follows −
- Input the value of a, b and n
- Check if b is 0 than division will go upto infinite and if a is 0 than result will be 0 as something divides to 0 is 0
- If n is greater than 1 than store the value of remainder and subtract it from the dividend after that multiply the result by ten. Start the next iteration
- Print the result
ALGORITHM
START Step 1-> declare function to compute division upto n decimal places void compute_division(int a, int b, int n) check IF (b == 0) print Infinite End check IF(a == 0) print 0 End check IF(n 0) && (b 0))) print “-” set a = a > 0 ? a : -a set b = b > 0 ? b : -b End Declare and set int dec = a / b Loop For int i = 0 and i In main() Declare and set int a = 36, b = 7, n = 5 Call compute_division(a, b, n) STOP
Example
#includeusing namespace std; void compute_division(int a, int b, int n) { if (b == 0) { cout 0) && (b 0))) { cout 0 ? a : -a; b = b > 0 ? b : -b; } int dec = a / b; for (int i = 0; i Output
5.14285
Advertisements
