Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

Please help me in writing a c++ program (Eclipse with Cygwin compiler.docx

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité

Consultez-les par la suite

1 sur 2 Publicité

Please help me in writing a c++ program (Eclipse with Cygwin compiler.docx

Télécharger pour lire hors ligne

Please help me in writing a c++ program (Eclipse with Cygwin compiler fyi) Commentaries are most welcomed. Many thanks in advance.
Part 2: happy.cpp Write a C++ program happy.cpp which prints the first 88,888 happy numbers The numbers should be printed 10 per line, with a single space between the values. The last line should only have 8 numbers on it. The first line of the output should be 1 7 10 13 19 23 28 31 32 44 See wikipedia or wolfram for the definition of a happy number.
Solution
//infinite happy number\'s
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int k, rep = 0;
int SumOfSq(int num) {
int total = 0;
while(num) {
int digit = num % 10;
num /= 10;
total += pow(digit,2);
}
return total;
}
bool Happy(int num) {
int temp = 0;
while(num != 1) {
if(k == rep) exit(1);
num = SumOfSq(num);
if(temp++ > 10) {
return 0; //Not happy
}
}
rep++;
return 1; //Happy
}
int main() {
cout << \"How many Happy Numbers to find? \";
cin >> k;
for(int j = 1;;j++)
if(Happy(j)) cout << j << \" \";
}
.

Please help me in writing a c++ program (Eclipse with Cygwin compiler fyi) Commentaries are most welcomed. Many thanks in advance.
Part 2: happy.cpp Write a C++ program happy.cpp which prints the first 88,888 happy numbers The numbers should be printed 10 per line, with a single space between the values. The last line should only have 8 numbers on it. The first line of the output should be 1 7 10 13 19 23 28 31 32 44 See wikipedia or wolfram for the definition of a happy number.
Solution
//infinite happy number\'s
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int k, rep = 0;
int SumOfSq(int num) {
int total = 0;
while(num) {
int digit = num % 10;
num /= 10;
total += pow(digit,2);
}
return total;
}
bool Happy(int num) {
int temp = 0;
while(num != 1) {
if(k == rep) exit(1);
num = SumOfSq(num);
if(temp++ > 10) {
return 0; //Not happy
}
}
rep++;
return 1; //Happy
}
int main() {
cout << \"How many Happy Numbers to find? \";
cin >> k;
for(int j = 1;;j++)
if(Happy(j)) cout << j << \" \";
}
.

Publicité
Publicité

Plus De Contenu Connexe

Similaire à Please help me in writing a c++ program (Eclipse with Cygwin compiler.docx (20)

Plus par ellenj4 (20)

Publicité

Plus récents (20)

Please help me in writing a c++ program (Eclipse with Cygwin compiler.docx

  1. 1. Please help me in writing a c++ program (Eclipse with Cygwin compiler fyi) Commentaries are most welcomed. Many thanks in advance. Part 2: happy.cpp Write a C++ program happy.cpp which prints the first 88,888 happy numbers The numbers should be printed 10 per line, with a single space between the values. The last line should only have 8 numbers on it. The first line of the output should be 1 7 10 13 19 23 28 31 32 44 See wikipedia or wolfram for the definition of a happy number. Solution //infinite happy number's #include <iostream> #include <cmath> #include <cstdlib> using namespace std; int k, rep = 0; int SumOfSq(int num) { int total = 0; while(num) { int digit = num % 10; num /= 10; total += pow(digit,2); } return total; } bool Happy(int num) { int temp = 0; while(num != 1) { if(k == rep) exit(1); num = SumOfSq(num); if(temp++ > 10) { return 0; //Not happy
  2. 2. } } rep++; return 1; //Happy } int main() { cout << "How many Happy Numbers to find? "; cin >> k; for(int j = 1;;j++) if(Happy(j)) cout << j << " "; }

×