Advertisement
Guest User

Inverter Numero

a guest
Oct 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. int numDigitos(int valor);
  4. int inverterNumero(int num);
  5.  
  6.  
  7. int main()
  8. {
  9.     int num;
  10.     printf("Insira o numero: ");
  11.     scanf("%d", &num);
  12.     printf("O numero %d invertido eh %d",num, inverterNumero(num));
  13.     return 0;
  14. }
  15.  
  16. int numDigitos(int valor)
  17. {
  18.     int contaDigitos=0;
  19.     do
  20.     {
  21.         contaDigitos++;
  22.         valor/=10;
  23.     }
  24.     while (valor!=0);
  25.     return contaDigitos;
  26. }
  27.  
  28. int inverterNumero(int num) //1234
  29. {
  30.     int digitos = numDigitos(num);
  31.     int i, potencia;
  32.     float total=0;
  33.  
  34.     for(i=0; i<digitos; i++)
  35.     {
  36.         potencia = ceil(pow(10,i));
  37.         total+= ((num/potencia)%10 ) * pow(10,digitos-i-1);
  38.     }
  39.     return (int)total;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement