Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4.  
  5. int eh_numero_de_armstrong(int n);
  6. int intpow(int base, int exp);
  7.  
  8. int main()
  9. {
  10.     int x;
  11.  
  12.     printf("Digite um numero qualquer: ");
  13.     scanf("%d", &x);
  14.  
  15.     if (eh_numero_de_armstrong(x))
  16.         printf("%d eh um numero de Armstrong.\n", x);
  17.     else
  18.         printf("%d nao eh um numero de Armstrong.\n", x);
  19.  
  20.     return 0;
  21. }
  22.  
  23. int eh_numero_de_armstrong(int n)
  24. {
  25.     char str[100];
  26.     int soma_alg = 0;
  27.  
  28.     sprintf(str, "%d", n);
  29.  
  30.     for (int i = 0; i < strlen(str); i++)
  31.         soma_alg += intpow(str[i] - '0', strlen(str));
  32.  
  33.     if (n == soma_alg)
  34.         return 1;
  35.     else
  36.         return 0;
  37. }
  38.  
  39. int intpow(int base, int exp) {
  40.     int result=base;
  41.  
  42.     for (int i = 1; i < exp; i++) {
  43.         result *= base;
  44.     }
  45.  
  46.     return result;
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement