Advertisement
Abelsor

S4_Ejercicio_8

Mar 2nd, 2023
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3.  
  4. using namespace std;
  5.  
  6. int checkArmstrong(int);
  7. int checkPerfect(int);
  8.  
  9. int main()
  10. {
  11.     int n=1,accuArm=0,accuPerfect=0;
  12.  
  13.     while(n>0 && n<=1000){
  14.       cin>>n;
  15.      
  16.       accuPerfect += checkPerfect(n);
  17.       accuArm += checkArmstrong(n);
  18.      
  19.     }
  20.     cout<<accuArm<<endl;
  21.     cout<<accuPerfect;
  22.  
  23.     return 0;
  24. }
  25.  
  26. int checkPerfect(int n)
  27. {
  28.     int acumulador = 0;
  29.     for(int i=1 ; i<n ; i++){
  30.         if(!(n%i))
  31.             acumulador += i;
  32.     }
  33.     if(n==acumulador)
  34.         return 1;
  35.    
  36.     else   
  37.         return 0;
  38. }
  39.  
  40. int checkArmstrong(int n)
  41. {  
  42.     int aux = n;
  43.     int digito;
  44.     int cant_digitos = 0;
  45.     int acumulador = 0;
  46.    
  47.     // Contar digitos
  48.     while(aux>0){
  49.         cant_digitos++;
  50.         aux = aux/10;
  51.     }
  52.    
  53.     // Sumar digitos
  54.     aux = n;
  55.     while(aux>0){
  56.         digito = aux%10;
  57.        
  58.         acumulador += pow(digito,cant_digitos);
  59.        
  60.         aux /= 10;
  61.     }
  62.    
  63.     if(n == acumulador)
  64.         return 1;
  65.        
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement