Diferència entre revisions de la pàgina «ASIX-M3-UF1-A2.4-Exercicis estructura iterativa: for»
| Línia 193: | Línia 193: | ||
--> | --> | ||
| − | 14. Escriu un programa que llegeixi un número | + | 14. Escriu un programa que llegeixi un número num i mostri per pantalla el següent: |
<pre> | <pre> | ||
| + | Exemple num=5 | ||
1 | 1 | ||
1 2 | 1 2 | ||
1 2 3 | 1 2 3 | ||
| − | + | 1 2 3 4 | |
| − | 1 2 3 | + | 1 2 3 4 5 |
</pre> | </pre> | ||
<!-- | <!-- | ||
| Línia 212: | Línia 213: | ||
</source> | </source> | ||
--> | --> | ||
| + | |||
| + | 15. Escriu un programa que llegeixi un número i mostri per pantalla el següent: | ||
| + | <pre> | ||
| + | Exemple num=5 | ||
| + | 5 4 3 2 1 | ||
| + | 4 3 2 1 | ||
| + | 3 2 1 | ||
| + | 2 1 | ||
| + | 1 | ||
| + | </pre> | ||
| + | <!-- | ||
| + | <source lang="python"> | ||
| + | print("Dibuix amb números consecutius") | ||
| + | |||
| + | num = int(input("Introdueix un número enter positiu: ")) | ||
| + | for i in range(num,0): | ||
| + | for j in range(num,0): | ||
| + | print(j, end='') | ||
| + | print("") | ||
| + | </source> | ||
| + | --> | ||
| + | |||
| + | 16. Escriu un programa que llegeixi un número N i mostri per pantalla el següent: | ||
| + | <pre> | ||
| + | Exemple N=5 | ||
| + | 1 | ||
| + | 2 1 | ||
| + | 3 2 1 | ||
| + | 4 3 2 1 | ||
| + | 5 4 3 2 1 | ||
| + | </pre> | ||
| + | <!-- | ||
| + | <source lang="python"> | ||
| + | print("Dibuix amb números consecutius decreixents") | ||
| + | |||
| + | num = int(input("Introdueix un número enter positiu: ")) | ||
| + | for i in range(1,num+1): | ||
| + | for j in range(1,num-i): | ||
| + | print(j, end='') | ||
| + | for j in range(i,0): | ||
| + | print(j, end='') | ||
| + | print("") | ||
| + | </source> | ||
| + | --> | ||
| + | |||
| + | |||
| + | 17. Escriu un programa que llegeixi un número enter més gran que 0 i mostri el factorial de tots els números entre 1 i el número introduït. | ||
| + | <pre> | ||
| + | 1! = 1 | ||
| + | 2! = 2 * 1 = 2 | ||
| + | 3! = 3 * 2 * 1 = 6 | ||
| + | 4! = 4 * 3 * 2 * 1 = 24 | ||
| + | ... | ||
| + | </pre> | ||
| + | |||
| + | <!-- | ||
| + | do{ | ||
| + | System.out.print("Introduce un número > 0: "); | ||
| + | N = sc.nextInt(); | ||
| + | }while(N<0); | ||
| + | |||
| + | for(int i = 0; i <= N ; i++){ //para cada número desde 1 hasta N | ||
| + | |||
| + | //se calcula su factorial | ||
| + | factorial = 1; | ||
| + | for(int j = 1; j <= i; j++){ | ||
| + | factorial = factorial * j; | ||
| + | } | ||
| + | |||
| + | //se muestra el factorial | ||
| + | System.out.printf("%2d! = %.0f %n", i, factorial); | ||
| + | --> | ||
| + | |||
| + | 17. Escriu un programa que mostri la següent sortida: | ||
| + | |||
| + | <pre> | ||
| + | ZYWXVUTSRQPONMLKJIHGFEDCBA | ||
| + | YWXVUTSRQPONMLKJIHGFEDCBA | ||
| + | WXVUTSRQPONMLKJIHGFEDCBA | ||
| + | XVUTSRQPONMLKJIHGFEDCBA | ||
| + | VUTSRQPONMLKJIHGFEDCBA | ||
| + | UTSRQPONMLKJIHGFEDCBA | ||
| + | TSRQPONMLKJIHGFEDCBA | ||
| + | SRQPONMLKJIHGFEDCBA | ||
| + | RQPONMLKJIHGFEDCBA | ||
| + | QPONMLKJIHGFEDCBA | ||
| + | PONMLKJIHGFEDCBA | ||
| + | ONMLKJIHGFEDCBA | ||
| + | NMLKJIHGFEDCBA | ||
| + | MLKJIHGFEDCBA | ||
| + | LKJIHGFEDCBA | ||
| + | KJIHGFEDCBA | ||
| + | JIHGFEDCBA | ||
| + | IHGFEDCBA | ||
| + | HGFEDCBA | ||
| + | GFEDCBA | ||
| + | FEDCBA | ||
| + | EDCBA | ||
| + | DCBA | ||
| + | CBA | ||
| + | BA | ||
| + | A | ||
| + | </pre> | ||
Revisió del 10:18, 29 oct 2020
1. Imprimir els números del 1 al 100.
2. Imprimir els números parells del 0 al 98.
3. Imprimir els números del 100 al 1.
4. Demanar cinc números i mostrar la suma.
5. Igual que l'anterior però només sumant els valors més grans de 10.
6. Calcular el producte dels números del 1 al 10.
7. Demanar cinc números enters positius i mostrar el valor més gran.
8. Fer un quadrat d'asteriscos. Per exemple si l'usuari introdueix el valor 5, imprimirà:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
9. Com l'anterior però fent un quadrat buit. Per exemple si l'usuari introdueix el valor 5, imprimirà:
* * * * *
* *
* *
* *
* * * * *
10. Realitzar un programa que mostri el següent dibuix per a un nombre de línies demanat a l'usuari (a l’exemple 5)
*
* *
* * *
* * * *
* * * * *
11. Realitzar un programa que mostri el següent dibuix per a un nombre de línies demanat a l'usuari (a l’exemple 5)
* * * * *
* * * *
* * *
* *
*
12. Realitzar un programa que mostri el següent dibuix per a un nombre de línies demanat a l'usuari (a l’exemple 5)
*
* *
* * *
* * * *
* * * * *
13. Realitzar un programa que mostri el següent dibuix per a un nombre de línies demanat a l'usuari (a l’exemple 5)
* * * * *
* * * *
* * *
* *
*
14. Escriu un programa que llegeixi un número num i mostri per pantalla el següent:
Exemple num=5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
15. Escriu un programa que llegeixi un número i mostri per pantalla el següent:
Exemple num=5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
16. Escriu un programa que llegeixi un número N i mostri per pantalla el següent:
Exemple N=5
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
17. Escriu un programa que llegeixi un número enter més gran que 0 i mostri el factorial de tots els números entre 1 i el número introduït.
1! = 1 2! = 2 * 1 = 2 3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24 ...
17. Escriu un programa que mostri la següent sortida:
ZYWXVUTSRQPONMLKJIHGFEDCBA YWXVUTSRQPONMLKJIHGFEDCBA WXVUTSRQPONMLKJIHGFEDCBA XVUTSRQPONMLKJIHGFEDCBA VUTSRQPONMLKJIHGFEDCBA UTSRQPONMLKJIHGFEDCBA TSRQPONMLKJIHGFEDCBA SRQPONMLKJIHGFEDCBA RQPONMLKJIHGFEDCBA QPONMLKJIHGFEDCBA PONMLKJIHGFEDCBA ONMLKJIHGFEDCBA NMLKJIHGFEDCBA MLKJIHGFEDCBA LKJIHGFEDCBA KJIHGFEDCBA JIHGFEDCBA IHGFEDCBA HGFEDCBA GFEDCBA FEDCBA EDCBA DCBA CBA BA A