Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int main(int argc, char *argv[]) {
  6.   int a;
  7.   while(1) {
  8.     int inp = scanf("%d", &a);
  9.  
  10.     if (a < 0 || inp != 1) {
  11.       fprintf(stderr, "Error: Chybny vstup!\n");
  12.       return 100;
  13.     }
  14.  
  15.     // end of input
  16.     if (a == 0)
  17.       break;
  18.  
  19.  
  20.     printf("Prvociselny rozklad cisla %d je:\n", a);
  21.  
  22.     if (a == 1) {
  23.       printf("1\n");
  24.       continue;
  25.     }
  26.  
  27.     // counting factors that are two
  28.     int count = 0;
  29.     while (a % 2 == 0) {
  30.       count++;
  31.       a /= 2;
  32.     }
  33.  
  34.     if (count == 1) {
  35.       printf("%d", 2);
  36.     } if (count > 1) {
  37.       printf("2^%d", count);
  38.     }
  39.  
  40.     if (a == 1) {
  41.       printf("\n");
  42.       continue;
  43.     } else if (count > 0) {
  44.       printf(" x ");
  45.     }
  46.  
  47.     // looking for all the odd prime factors
  48.     for (int i = 3; i <= a/2; i += 2) {
  49.  
  50.       count = 0;
  51.       while (a % i == 0) {
  52.         count++;
  53.         a /= i;
  54.       }
  55.  
  56.       if (count == 1)
  57.         printf("%d", i);
  58.       else if (count > 1)
  59.         printf("%d^%d", i, count);
  60.  
  61.       if (a == 1) {
  62.         printf("\n");
  63.         break;
  64.       } else if (count > 0) {
  65.         printf(" x ");
  66.       }
  67.     }
  68.  
  69.     if (a != 1) {
  70.       printf("%d\n", a);
  71.  
  72.     }
  73.   }
  74.  
  75.   return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement