SlideServe
Menu
Préférences
© SlideServe · v2026

while loop

andrew-gillespie avatar
5 diapositives · 263 Views · Nov 28, 2014

while loop. Write a program that asks the user to enter a number, then displays whether this number is even or odd. The program repeats until the user quits. Write a program that asks the user to enter a number, then counts by 2 starting from 0 to the given number.

while loop - slide 1
01 / 5while loop
1 / 17

Télécharger la présentation

while loop
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. while loop • Write a program that asks the user to enter a number, then displays whether this number is even or odd. The program repeats until the user quits. • Write a program that asks the user to enter a number, then counts by 2 starting from 0 to the given number. • Write a program which finds the factorial of a given number CENG 111

  2. Even/Odd • #include <stdio.h> • int main(void) { • charansw = 'y'; • intnum; • while(answ == 'y') { • printf("Please enter an integer: "); • scanf("%d", &num); • if(num%2 == 0) printf("%d is even.\n", num); • else printf("%d is odd.\n", num); • printf("Do you want to give another number? (y/n): "); • scanf("%c%c", &answ, &answ); • } • return(0); • }

  3. Count by 2 • #include <stdio.h> • int main(void) { • int num, stp = 0; • printf("Please enter a number: "); • scanf("%d", &num); • while(stp <= num) { • printf("%d ", stp); • stp = stp + 2; • } • printf("\n"); • return(0); • }

  4. Factorial #include <stdio.h> int main(void) { int num, Fact = 1; printf("Please enter a number: "); scanf("%d", &num); while(num > 1) { Fact = Fact * num; num = num -1; } printf("The factorial is %d\n", Fact); return(0); }

  5. Min/Max #include <stdio.h> int main(void) { int Min, Max, Nmbr; intCnt=1; printf("Please enter number %d: ", Cnt); scanf("%d", &Min); Max = Min; Cnt = Cnt + 1; while(Cnt <= 10) { printf("Please enter number %d: ", Cnt); scanf("%d", &Nmbr); if(Nmbr < Min) Min = Nmbr; if(Nmbr > Max) Max = Nmbr; Cnt = Cnt + 1; } printf("The minimum is %d\n", Min); printf("The maximum is %d\n", Max); return(0); }