1 / 5

while loop

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.

Download Presentation

while loop

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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); }

More Related