Loops and its examples

  • February 27, 2022
  • C
Matrix multiplication

Let us take a look at the various ways to use loops in c.

Infinite loop

An infinite loop in c does not end, in other words, its condition never becomes true and keeps on updating forever. It is also known as an endless loop or indefinite loop.

Example:
#include <stdio.h>

int main() {
   
    for(;;)
   {
       printf("\nCoderzpy!");
   }
    
    return 0;
}
Output:
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!

Here for(;;) is an infinite loop where there is no initialization, condition, or updating statement, therefore the loop becomes endless, and it continues forever.

Need for infinite loop:

  • All servers use an infinite loop to accept clients’ requests continuously.
  • All the operating systems run in an infinite loop, as it does not exist after performing some task. It comes out of an infinite loop only when the user manually shuts down the system.
  • All the games also run in an infinite loop. 
The sum of first n natural numbers:

Here we are calculating the sum of n natural numbers using for loop.

#include <stdio.h>
int main()
{
    int num, count, sum = 0;

    printf("Enter a positive integer: ");
    scanf("%d", &num);

    // for loop terminates when num is less than count
    for(count = 1; count <= num; ++count)
    {
        sum += count;
    }

    printf("Sum = %d", sum);

    return 0;
}
Output:
Enter a positive integer: 10
Sum = 55
Check for Prime Number:

For checking prime numbers, calculate the number of factors of a number and check if it is greater than or equal to 2.

#include <stdio.h>

int main() {
   int n,i,count=0;
   scanf("\n%d",&n);
    for(i=1;i<=n;i++)
   { 
      if(n%i==0)
       count++;
   }
    if(count==2)
    {
        printf("%d is a prime number.",n);
    }
    else
    {
        printf("%d is not a prime number.",n);
    }
    return 0;
}
output:
11
11 is a prime number.

Note: also read about  Decision-Making in C & Loops in C

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *