Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int chiedi(string msg,int lmin,int lmax) {
  8. int x;
  9. cout<<"Inserire numero: ";
  10. cin>>x;
  11. while (x<lmin || x>lmax){
  12. cout<<"Inserire numero: ";
  13. cin>>x;
  14. }
  15. return x;
  16. }
  17. bool primo(int a) {
  18. for (int i=1;i<a;i++) {
  19. if (a%i==0) {
  20. return false;
  21. }
  22. }
  23. return true;
  24. }
  25. int divisori(int x) {
  26. int sommadiv=0;
  27. for (int i=1; i<x+1; i++) {
  28. if(x%i==0) {
  29. sommadiv+=i;
  30. }
  31. }
  32. return sommadiv;
  33. }
  34. bool perfetto(int a) {
  35. if(round(int(sqrt(a)))==float(sqrt(a))){
  36. return true;
  37. }
  38. return false;
  39. }
  40. int scifre(int a) {
  41. int sommacifre=0;
  42. while(a>0) {
  43. int m=a%10;
  44. sommacifre=sommacifre+m;
  45. a=a/10;
  46. }
  47. return sommacifre;
  48. }
  49. bool palindromo(int x){
  50.  
  51. }
  52.  
  53. int main(){
  54. int n;
  55. cout<<"Inserisci N"<<endl;
  56. cin>>n;
  57. while(n<=0 or n>100) {
  58. cout<<"Inserisci N"<<endl;
  59. cin>>n;
  60. }
  61. string msg="Inserire numero: ";
  62. int somtre[n];
  63. int somt=0;
  64. int quadp=0;
  65. int sdp[n];
  66. int sommadivprima=0;
  67. int spal=0;
  68. int c=0;
  69. for (int i=0;i<n;i++){
  70. int x=chiedi(msg,0,50);
  71. if(scifre(x)%3==0) {
  72. somtre[somt]=x;
  73. somt++;
  74. }
  75. if(perfetto(x) && x%2==0) {
  76. quadp++;
  77. }
  78. if(primo(divisori(x))) {
  79. sdp[sommadivprima]=x;
  80. sommadivprima++;
  81. }
  82. }
  83. cout<<"Numeri con somma cifre divisibile per 3: ";
  84. for (int i=0;i<somt-1;i++) { cout<<somtre[i],", "; }
  85. cout<<somtre[somt]<<endl;
  86. cout<<"Quanti numeri pari sono quadrati perfetti: "<<quadp<<endl;
  87. cout<<"Quanti numeri con somma dei divisori prima: "<<sommadivprima+1<<", ovvero ";
  88. for (int i=0;i<sommadivprima-1;i++) { cout<<sdp[i],", "; }
  89. cout<<sdp[sommadivprima]<<endl;
  90. cout<<"Non riesco a costruire la funzione sui palindromi";
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement