 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count number of digits after decimal on dividing a number in C++
We are given two integer numbers let’s say num1 and num2 and the task is to divide the num1 with num2 and calculate the count of digits after decimal on dividing these given numbers.
For Example
Input − num1 = 2, num2 = 5 Output − count is 1
Explanation − when we divide 2 with 5 i.e. ? = 0.4, so digits after decimal is 1 therefore count is 1.
Input − num1 = 2, num2 = 0 Output − Floating point exception (core dumped)
Explanation − when we divide any number with 0 it will return an error and terminate the program abnormally.
Input − num1 = 2, num2 = 3 Output − Infinite
Explanation − when we divide 2 with 3 i.e. 2/3 = 0.666..., digits after decimal is infinite therefore we will print infinite.
Approach used in the below program is as follows
- Input two variables let’s say, num1 and num2 
- Create a variable count to store the count of decimal numbers and initializes it with 0 
- Create a variable um of unordered_map type 
- Start loop while num1%num2 != 0 
- Inside the loop, set num1 with num1%num2 
- Increment the value of count by 1 
- Check if um.find(num1) != um.end() then return -1 
- Outside the loop, return the value in count. 
- Print the result. 
Example
#include <iostream> #include <unordered_map> using namespace std; int countdigits(int x, int y){    int result = 0; // result variable    unordered_map<int, int> mymap;    // calculating remainder    while (x % y != 0){       x = x % y;       result++;       if (mymap.find(x) != mymap.end()){          return -1;       }       mymap[x] = 1;       x = x * 10;    }    return result; } int main(){    int res = countdigits(2, 5);    (res == -1)? cout << "count is Infinty" : cout <<"count is "<<res;    return 0; }  Output
If we run the above code we will get the following output −
count is 1
