Last active
April 26, 2018 07:26
-
-
Save jordi-petit/1b9de5e5bf4300cce2d69f73246ac8fe to your computer and use it in GitHub Desktop.
AP1 2017-09-19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Escriure tots els nombres de 1 a 1000 | |
#include <iostream> | |
using namespace std; | |
int main() { | |
int i = 1; | |
while (i <= 1000) { | |
cout << i << endl; | |
i = i + 1; | |
} | |
cout << "al final i=" << i << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Escriure tots els nombres de 1 a n, on n és donat per l'usuari | |
#include <iostream> | |
using namespace std; | |
int main() { | |
int n; | |
cin >> n; | |
int i = 1; | |
while (i <= n) { | |
cout << i << endl; | |
i = i + 1; | |
} | |
cout << "al final i=" << i << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Escriure tots els nombres de n a 1 | |
#include <iostream> | |
using namespace std; | |
int main() { | |
int n; | |
cin >> n; | |
int i = n; | |
while (i >= 1) { | |
cout << i << endl; | |
i = i - 1; | |
} | |
cout << "al final i=" << i << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Escriure tots els nombres de n a 1 | |
// sense usar la i i modificant la n | |
#include <iostream> | |
using namespace std; | |
int main() { | |
int n; | |
cin >> n; | |
while (n >= 1) { | |
cout << n << endl; | |
n = n - 1; | |
} | |
cout << "al final n=" << n << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Escriure tots els nombres senars de 1 a n | |
#include <iostream> | |
using namespace std; | |
int main() { | |
int n; | |
cin >> n; | |
int i = 1; | |
while (i <= n) { | |
cout << i << endl; | |
i = i + 2; | |
} | |
cout << "al final i=" << i << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Calcular el factorial d'un natural | |
#include <iostream> | |
using namespace std; | |
int main() { | |
int n; | |
cin >> n; | |
int f = 1; | |
int i = 1; | |
while (i <= n) { | |
f = f * i; | |
i = i + 1; | |
} | |
cout << f << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Calcular ("a mà") la suma dels n primers naturals | |
#include <iostream> | |
using namespace std; | |
int main() { | |
int n; | |
cin >> n; | |
int s = 0; | |
int i = 1; | |
while (i <= n) { | |
s = s + i; | |
i = i + 1; | |
} | |
cout << s << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Calcular l'n-èsim número de Fibonacci | |
#include <iostream> | |
using namespace std; | |
int main() { | |
int n; | |
cin >> n; | |
int a = 0; | |
int b = 1; | |
int i = 1; | |
while (i <= n) { | |
int c = a; | |
a = b; | |
b = b + c; | |
i = i + 1; | |
} | |
cout << a << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment