Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- #include<errno.h>
- #include<limits.h>
- #include<ctype.h>
- #include<string.h>
- void factoriprimi(long numar){
- long cnt = 0;
- while(numar % 2 == 0){
- cnt++;
- numar = numar/2;
- }
- if( numar == 2){
- printf(" 2 ");
- }
- if(cnt == 1){
- printf("2 ");
- }
- if(cnt > 1){
- printf(" 2^%ld ", cnt);
- }
- long cnt1 = 0;
- for(long i = 3; i <= sqrt(numar); i = i+2){
- while(numar % i == 0){
- //printf("%d ", i);
- numar = numar/i;
- cnt1++;
- }
- if(cnt1 == 1){
- printf(" %ld ", i);
- }
- if(cnt1 > 1){
- printf(" %ld^%ld ", i, cnt1);
- }
- }
- if(numar > 2)
- printf(" %ld ", numar);
- }
- int main(int argc, char *argv[]){
- int val0;
- char *endptr;
- /*if(argc <= 1){
- fprintf(stderr, "Nu a fost introdus niciun numar", argv[0]);
- exit(EXIT_FAILURE);
- }*/
- if (argc != 2)
- {
- fprintf(stderr, "Utilizare: %s numar\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- long val = strtol(argv[1], &endptr, 10);
- if ((errno == ERANGE && (val == INT_MAX || val == INT_MIN))|| (errno != 0 && val == 0)) {
- perror("strtol");
- exit(EXIT_FAILURE);
- }
- if (*endptr != '\0' || strcmp(argv[1], "") == 0 ){
- fprintf(stderr, "%s invalid", argv[1]);
- exit(EXIT_FAILURE);
- }
- if(strncmp (argv[1], "0", 1) == 0){
- fprintf(stderr, "Invalid, incepe cu 0");
- exit(EXIT_FAILURE);
- }
- val0 =val;
- //sscanf("%d", &s);
- factoriprimi(val0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment