Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. // Exercicio 21 - Leia um numero e decompor em primos diferenciadamente
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4.  
  5. bool teste_primo(int n) // função que testa se o número é primo
  6. {
  7. int i, primo;
  8. for (i=2;i<=n;i++)
  9. {
  10. if(n%i==0)
  11. {
  12. primo++;
  13. }
  14. }
  15. if(primo==1)
  16. {
  17. return true;
  18. }
  19. }
  20.  
  21. int proximop(int n)//Procura o próximo primo
  22. {
  23. for(int a=n+1;a>=n+2;a++)
  24. {
  25. if(teste_primo(a))
  26. {
  27. return a; // Retorna para a principal
  28. break;
  29. }
  30. }
  31. }
  32.  
  33. void fatorar(int n)
  34. {
  35. int c = 2, fator, contador = 0;
  36. for(c = 0; c<n; c++){
  37. fator = proximop(c);
  38. while(n%fator == 0){
  39. n/=fator;
  40. contador++;
  41. }
  42. if(contador >= 1){
  43. printf("%dx%d\n",contador,proximop(c));
  44. }
  45. contador = 0;
  46. }
  47. return;
  48. }
  49.  
  50. int main()
  51. {
  52. int n,c, i; // f é o fator
  53. printf("Digite um número: ");
  54. scanf("%d",&n);
  55. fatorar(c);
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement