Here is a C program that uses a while loop to continuously get numbers from the user until an even number is entered:
#include <stdio.h>
int main()
{
int number;
while(1)
{
printf("Enter a number: ");
scanf("%d", &number);
if(number%2 == 0)
{
printf("Even number %d entered, terminating program\n", number);
break;
}
}
return 0;
}
This program uses a while(1) loop, which creates an infinite loop. It gets a number from the user using scanf. It then checks if the number is even using the modulo (%) operator
Here is a C program that uses a while loop to continuously get numbers from the user until an even number is entered:
#include <stdio.h>
int main()
{
int number;
while(1)
{
printf("Enter a number: ");
scanf("%d", &number);
if(number%2 == 0)
{
printf("Even number %d entered, terminating program\n", number);
break;
}
}
return 0;
}
This program uses a while(1) loop, which creates an infinite loop. It gets a number from the user using scanf. It then checks if the number is even using the modulo (%) operator
Here is a C program that uses a while loop to continuously get numbers from the user until an even number is entered:
#include <stdio.h>
int main()
{
int number;
while(1)
{
printf("Enter a number: ");
scanf("%d", &number);
if(number%2 == 0)
{
printf("Even number %d entered, terminating program\n", number);
break;
}
}
return 0;
}
This program uses a while(1) loop, which creates an infinite loop. It gets a number from the user using scanf. It then checks if the number is even using the modulo (%) operator
Infinite Loop // Infinite Loops, Page 226 #include<stdio.h> int main() { int i = 7; while (1) /* any non zero number can be provided here. It is an infinite loop */ { printf("i = %d\n", i); i++; } getchar(); return 0; } Dr. Yousaf, PIEAS Infinite Loop // Infinite Loops, Page 226 #include<stdio.h> int main() { int i = 7; do { printf("i = %d\n", i); i++; } while (1); /* any non zero number can be provided here. It is an infinite loop */ getchar(); return 0; } Dr. Yousaf, PIEAS Infinite Loop // Infinite Loops, Page 226 #include<stdio.h> int main() { int i = 7; for (; ;) { printf("i = %d\n", i); i++; } getchar(); return 0; } Dr. Yousaf, PIEAS break and continue Statements
Dr. Yousaf, PIEAS
Break Statement #include<stdio.h> int main() { int i;
for (i = 1; i <= 10; i++)
{ if ( i ==5) break; printf("i = %d\n", i); } getchar(); return 0; } Dr. Yousaf, PIEAS Break Statement #include<stdio.h> int main() { int i;
for (i = 1; i <= 10; i++)
{ if ( i ==5) // what’s about if we write if ( i > 3) break; printf("i = %d\n", i); } getchar(); return 0; } Dr. Yousaf, PIEAS Break Statement #include<stdio.h> int main() { int i;
for (i = 1; i <= 10; i++)
{ if ( i ==5) // what’s about if we write if ( i < 3) break; printf("i = %d\n", i); } getchar(); return 0; } Dr. Yousaf, PIEAS // Infinite loop using while and break statement #include<stdio.h> int main() { int num; printf("Please enter an integer number from 10 to 20\n"); while (1) { scanf("%d",&num); if (num>= 10 && num <=20) break; else printf("Invalid. Please enter an integer number from 10 to 20\n"); } // Rest of the code goes here Dr. Yousaf, PIEAS // Infinite loop using for loop and break statement #include<stdio.h> int main() { int num; printf("Please enter an integer number from 10 to 20\n"); for( ; ;) { scanf("%d",&num); if (num >= 10 && num <=20) break; else printf("Please enter an integer number from 10 to 20\n"); } // Rest of the code goes here
Dr. Yousaf, PIEAS
// Infinite loop using do while loop and break statement #include<stdio.h> int main() { int num; printf("Please enter an integer number from 10 to 20\n"); do { scanf("%d",&i); if (num >= 10 && num <=20) break; else printf("Invalid. Please enter an integer number from 10 to 20\n"); } while (1); // Rest of the code goes here Dr. Yousaf, PIEAS The break statement //More about break; //Page 221 for (i = 2; i<=n/2; i++) /* if used in loop, it only breaks the { if(n%i == 0) loop and then executes next { prime = 0; statements */ #include<stdio.h> break; } int main() } { int i, n, prime = 1; if (prime == 0) printf("Enter a positive integer"); printf("The number %d is not a scanf("%d", &n); prime number\n", n); if (n ==2) else printf("The number %d is a printf("The number %d is a prime number\n", n); prime number\n", n); else } getchar(); return 0; } {
Dr. Yousaf, PIEAS
The break Statement • break – Causes immediate exit from a while, for, do/while or switch structure – Program execution continues with the first statement after the structure – Common uses of the break statement are: • Escape early from a loop • Skip the remainder of a switch structure
Dr. Yousaf, PIEAS
Another Example: for and break Together int mynum = 3; int guess; The notation for(;;) is for(;;) used to create an { infinite for loop. printf("Guess my number:"); while(1) creates an scanf("%d",&guess); infinite while loop if(guess==mynum) instead. { printf("Good guess!\n"); break; To get out of an } infinite loop like this else one, we have to use printf("Try again.\n"); the break statement. }
Dr. Yousaf, PIEAS
continue Statement #include<stdio.h> int main() { int i;
for (i = 1; i <= 10; i++)
{ if ( i ==5) continue; printf("i = %d\n", i); } getchar(); return 0; } Dr. Yousaf, PIEAS continue Statement #include<stdio.h> int main() { int i;
for (i = 1; i <= 10; i++)
{ if ( i ==5) continue; printf("i = %d\n", i); } getchar(); return 0; } Dr. Yousaf, PIEAS continue Statement #include<stdio.h> int main() { int i;
for (i = 1; i <= 10; i++)
{ if ( i ==5) // what’s about if we write if ( i > 3) continue; printf("i = %d\n", i); } getchar(); return 0; } Dr. Yousaf, PIEAS continue Statement #include<stdio.h> int main() { int i;
for (i = 1; i <= 10; i++)
{ if ( i ==5) // what’s about if we write if ( i < 3) continue; printf("i = %d\n", i); } getchar(); return 0; } Dr. Yousaf, PIEAS continue Statement /*Enter 20 integer values. This program would tell how many odd numbers you entered */ #include<stdio.h> int main() { int i = 0, num, count=0; while(i < 20) { printf("Enter 20 integer numbers:"); scanf("%d",&num); i++; if(num%2==0) continue; count++; } printf("You entered %d odd numbers", count); getchar(); return 0; } Dr. Yousaf, PIEAS The continue Statement Used for skipping the remainder of the body of a while, for or do/while structure and proceeding with the next iteration of the loop – while and do/while • Loop-continuation test is evaluated immediately after the continue statement is executed – for • Increment expression is executed, then the loop-continuation test is evaluated
Dr. Yousaf, PIEAS
exit() function
Dr. Yousaf, PIEAS
exit() // About exit() function. Page 236 #include<stdio.h> #include<stdlib.h> // to use exit() function. int main() { int i, j, k; for (i = 0; i<5; i++) printf("%d\n", i); getchar(); exit(0); // it will exit the program j = 23; printf("j = %d\n", j); getchar(); return 0; } Dr. Yousaf, PIEAS Continue and exit together #include<stdio.h> // Continue to enter integer values. This program #include<stdlib.h> //will terminate when you enter 5 odd numbers. int main() { int num; int count=0; while(1) { printf("\nEnter an odd number :"); scanf("%d",&num); if(num%2==0) continue; count++; if(count==5) exit(0); } getchar(); return 0; } Dr. Yousaf, PIEAS Examples
Dr. Yousaf, PIEAS
For Loop -- Example Write a complete and an efficient C program which should take 20 integer values from the user, add all the even values entered, and display the total number of even values entered and the final sum of the even values.
Dr. Yousaf, PIEAS
For Loop -- Example Write a complete and an efficient C program which should take 20 integer values from the user, add all the even values entered, and display the total number of even values entered and the final sum of the even values.
Write a complete and an efficient C program which
should take 20 integer values from the user, multiply all the odd values entered and display the total number of odd values entered and the final product of all odd values.
Dr. Yousaf, PIEAS
For Loop -- Example Write a complete and an efficient C program that takes an integer number from the user and calculates its factorial.
Dr. Yousaf, PIEAS
For Loop -- Example Write a complete and an efficient C program that takes an integer number from the user and calculates its factorial. #include <stdio.h> int main() { int n, num=1,factorial=1; printf(“Enter value of N: ”); scanf(“%d”,&n); for(num=1; num<=n; num++) { factorial=factorial*num; } printf(“The factorial of %d = %d”, n, factorial); getchar(); return 0; } Dr. Yousaf, PIEAS while Loop -- Example Write a C program that gets a number from the user and display it. If the number is even the program will terminate otherwise the program will get another number. The while loop should terminate only when an even number is provided.
Dr. Yousaf, PIEAS
while Loop -- Example Write a C program that gets a number from the user. If the number is even the program will terminate otherwise the program will get another number. The while loop should terminate only when an even number is provided. #include <stdio.h> int main() { int a=1; while(a%2!=0) { printf(“\n Enter Value of a = ”); scanf(“%d”,&a); printf(“You entered %d\n”, a);
} printf(“\nEnd of While loop”); getchar(); return 0; } Dr. Yousaf, PIEAS while Loop -- Example
Try to print even numbers between two given
integer numbers A and B such that A can be greater or smaller than B. Numbers A and B are to be taken from the user.