it:informatica:linguaggio_c:ciclo_while_1
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linguaggio_c/ciclo_while_1?rev=1551188125&do=diff
Differences
This shows you the differences between two versions of the page.
— | it:informatica:linguaggio_c:ciclo_while_1 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Ciclo while (Esempio 1) ====== | ||
+ | **Concetti: | ||
+ | ciclo //while//, istruzione //if//, operatore di modulo (%) | ||
+ | **Testo: | ||
+ | Realizzare un programma in linguaggio C che: | ||
+ | * richieda l' | ||
+ | * calcoli quanti numeri pari e dispari sono stati inseriti | ||
+ | * stampi il risultato di tale conteggio | ||
+ | |||
+ | **Soluzione: | ||
+ | <file C ciclo_while_1.c> | ||
+ | /* Inserire 5 numeri da tastiera e contare il numero di numeri pari e dispari */ | ||
+ | |||
+ | #include < | ||
+ | #define NUMERI 5 | ||
+ | |||
+ | int main(){ | ||
+ | int n; | ||
+ | int x; | ||
+ | int n_pari, n_dispari; | ||
+ | |||
+ | n=0; | ||
+ | n_pari=0; | ||
+ | n_dispari=0; | ||
+ | |||
+ | while(n< | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | if (x%2 == 0) { | ||
+ | printf(" | ||
+ | n_pari = n_pari+1; | ||
+ | }else{ | ||
+ | printf(" | ||
+ | n_dispari = n_dispari+1; | ||
+ | } | ||
+ | |||
+ | n = n+1; | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Commenti: | ||
+ | Il programma esegue 5 volte il blocco di codice interno al ciclo //while// che consiste nel: | ||
+ | * richiedere l' | ||
+ | <code c> | ||
+ | scanf(" | ||
+ | </ | ||
+ | * vedere se un numero è pari //if (x%2 == 0)// o dispari (ramo //else// del costrutto //if//) | ||
+ | <code c> | ||
+ | if (x%2 == 0) { | ||
+ | /* Codice eseguito nel caso in cui x sia pari */ | ||
+ | |||
+ | }else{ | ||
+ | /* Codice eseguito nel caso in cui x sia dispari */ | ||
+ | } | ||
+ | </ | ||
+ | * nel caso di numero pari incrementare la variabile di conteggio //n_pari//, nel caso di numero dispari incrementare la variabile // | ||
+ | * stampare il risultato all' | ||
+ | <code c> | ||
+ | printf(" | ||
+ | </ |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linguaggio_c/ciclo_while_1?rev=1551188125&do=diff