60 likes | 210 Views
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.
E N D
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
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); • }
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); • }
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); }
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); }