Categories: C

Loops and its examples

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

Share
Published by
Rabecca Fatima

Recent Posts

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

4 days ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

2 weeks ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

3 weeks ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

1 month ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

1 month ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

1 month ago