Alex9090

Untitled

Nov 29th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<errno.h>
  5. #include<limits.h>
  6. #include<ctype.h>
  7. #include<string.h>
  8. void factoriprimi(long numar){
  9.  
  10. long cnt = 0;
  11. while(numar % 2 == 0){
  12. cnt++;
  13. numar = numar/2;
  14. }
  15. if( numar == 2){
  16. printf(" 2 ");
  17. }
  18. if(cnt == 1){
  19. printf("2 ");
  20. }
  21. if(cnt > 1){
  22. printf(" 2^%ld ", cnt);
  23. }
  24. long cnt1 = 0;
  25. for(long i = 3; i <= sqrt(numar); i = i+2){
  26. while(numar % i == 0){
  27. //printf("%d ", i);
  28. numar = numar/i;
  29. cnt1++;
  30. }
  31. if(cnt1 == 1){
  32. printf(" %ld ", i);
  33. }
  34. if(cnt1 > 1){
  35. printf(" %ld^%ld ", i, cnt1);
  36. }
  37. }
  38. if(numar > 2)
  39. printf(" %ld ", numar);
  40. }
  41.  
  42. int main(int argc, char *argv[]){
  43. int val0;
  44. char *endptr;
  45. /*if(argc <= 1){
  46. fprintf(stderr, "Nu a fost introdus niciun numar", argv[0]);
  47. exit(EXIT_FAILURE);
  48. }*/
  49.  
  50. if (argc != 2)
  51. {
  52. fprintf(stderr, "Utilizare: %s numar\n", argv[0]);
  53. exit(EXIT_FAILURE);
  54. }
  55.  
  56. long val = strtol(argv[1], &endptr, 10);
  57. if ((errno == ERANGE && (val == INT_MAX || val == INT_MIN))|| (errno != 0 && val == 0)) {
  58. perror("strtol");
  59. exit(EXIT_FAILURE);
  60. }
  61. if (*endptr != '\0' || strcmp(argv[1], "") == 0 ){
  62. fprintf(stderr, "%s invalid", argv[1]);
  63. exit(EXIT_FAILURE);
  64. }
  65. if(strncmp (argv[1], "0", 1) == 0){
  66. fprintf(stderr, "Invalid, incepe cu 0");
  67. exit(EXIT_FAILURE);
  68. }
  69. val0 =val;
  70. //sscanf("%d", &s);
  71. factoriprimi(val0);
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment