Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 SECENG 1111

2 Even/Odd #include 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); elseprintf("%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 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 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 int main(void) { int Min, Max, Nmbr; int Cnt=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); }


Download ppt "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."

Similar presentations


Ads by Google