Advertisement
DrAungWinHtut

singleDigitCalculator.cpp

Jan 7th, 2025
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath> // For pow()
  3. using namespace std;
  4.  
  5. struct CV {
  6.     int numbs[100];
  7.     int count;
  8. };
  9.  
  10. int pow2(int x, int y);
  11. int countNumber(int n);
  12. CV splitNumbers(int N);
  13.  
  14. int main() {
  15.     CV student = splitNumbers(12345678);
  16.     for (int i = 0; i < student.count; i++) {
  17.         cout << student.numbs[i] << endl;
  18.     }
  19.     return 0;
  20. }
  21.  
  22. int countNumber(int n) {
  23.     int count = 0;
  24.     while (n > 0) {
  25.         count++;
  26.         n /= 10;
  27.     }
  28.     return count;
  29. }
  30.  
  31. CV splitNumbers(int N) {
  32.     CV result;
  33.     int index = 0;
  34.     result.count = countNumber(N);
  35.  
  36.     while (N > 0) {
  37.         int divisor = pow2(10, result.count - 1);
  38.         result.numbs[index++] = N / divisor;
  39.         N %= divisor;
  40.         result.count--;
  41.     }
  42.  
  43.     result.count = index; // Update the count to actual numbers added
  44.     return result;
  45. }
  46.  
  47. int pow2(int x, int y) {
  48.     int result = 1;
  49.     for (int i = 0; i < y; i++) {
  50.         result *= x;
  51.     }
  52.     return result;
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement