Common examples of loops in C

May 27, 2017 9555

This article is an extension of the ‘Decide and loop’ chapter of C. If you need to learn basics then visit the C course first. You can also practice a good number of questions from practice section.

Factorial of a number

This example just demonstrates the calculation of the factorial using a while loop. You can visit this to see the same example using recursion.

#include <stdio.h>

int main()
{
printf("Enter a number to calculate the factorial of a number\n");
int number;
scanf("%d",&number);
int fact = 1;
while (number>0)
{
fact = fact*number;
number--;
}
printf("Factorial is %d\n",fact);
return 0;
}


This example follows the following steps:

1. A variable ‘fact’ is assigned a value of 1.
2. The value of the variable ‘fact’ is changed to the value of “‘fact’ * number” (fact = fact*number)
3. The ‘number’ is changed to 1 less than its previous value.
4. Steps 2 and 3 are repeated until the value of the ‘number’ becomes positive.

Let’s calculate the factorial of 4 using this algorithm.

Firstly, the ‘fact’ is given a value of 1 and then in the first iteration of the loop, it is changed to fact*number (i.e., 1*4) and 4 is changed to 3.

In the second iteration, the value of ‘fact’ is again changed to faact*number (i.e., 4*3) and the ‘number’ is changed to 2.

In the third iteration, the value of ‘fact’ becomes 4*3*2 (initial value of fact was 4*3 and the value of ‘number’ is 2) and 2 is changed to 1.

In the fourth and last iteration, ‘ fact’ becomes 4*3*2*1 and the value of the ‘number’ is changed to 0 and so the loop stops.

Prime numbers

This is a simple example of checking whether a number is prime or not. Prime number using Sieve algorithm is demonstrated in this article.

#include <stdio.h>

int main()
{
printf("Enter the number\n");
int number;

scanf("%d",&number);

int i = 2;
int prime = 1;

//if the number is not divisible by any number less than the square root of the number
//then it is prime
while ( (i*i) <= number )
{
if(number%i == 0)
{
prime = 0;
break;
}
i++;
}

if (number < 2)
prime = 0;

if (prime)
printf("The number is prime\n");
else
printf("The number is not prime\n");
return 0;
}


The code is straight forward and very simple to understand.

Initially, we are making the value of the variable ‘prime’ True (1) and then we will check if the number is divisible by any number or not and if it is then we will make it False (0).

A number is a prime number if it hasn’t any factor between 2 and the square root of itself and this is exactly what we are checking. Our loop goes from 2 to square root of the number and if any factor is found then the value of ‘prime’ is changed to 0 (False) and the loop is stopped.

if number < 2 → prime = 0 – We may also get a number which is less than 2 and for this case, we are checking explicitly and making the value of ‘prime’ ‘False’.

Palindrome

A Palindrome reads same from beginning and end. For e.g., 121, 1221, etc.This is the example of doing the same using while loop.

#include <stdio.h>

int main()
{
printf("Enter the number\n");
int number;

scanf("%d",&number);

int temp = number;
int reverse_num = 0;

while (temp!=0)
{
reverse_num = (reverse_num*10)+(temp%10);
temp = temp/10;
}

if (reverse_num == number)
printf("Yes! a palindrome\n");
else
printf("No\n");

return 0;
}


In this example, we are just making a new number which is in the reverse order of the previous one and finally comparing the both and to do so we are first multiplying the ‘reverse_num’ with 10 and adding it to the remainder of the division of ‘temp’ with 10. Suppose, the value of ‘temp’ is 45 then (reverse_num*10)+(temp%10) will give us 5 and then temp = temp/10 will make the value of ‘temp’ equal to 4. In the next iteration, the value of ‘reverse_num’ will become 54 ((5*10)+(4%10)) and the value of ‘temp’ will become 0.

Armstrong Numbers

A number ‘ n’ is called Armstrong number if the sum of each of its digits raised to the power of the number of digits is equal to the number itself. For example, 153 is an Armstrong number because 153 = (1*1*1)+(5*5*5)+(3*3*3).

The example given below checks if a given number is an Armstrong number or not.

#include <stdio.h>
#include <math.h>

int main()
{
printf("Enter the number\n");
int number;
scanf("%d",&number);

int number_of_digits = ((int)log10(number))+1;
int sum_arm = 0;
int temp = number;

while(temp!=0)
{
sum_arm = sum_arm + ((int)pow(temp%10,number_of_digits));
temp = temp/10;
}

if (sum_arm == number)
printf("Yes, an Armstrong number\n");
else
printf("No\n");

return 0;
}


Please note that we are using the ‘math.h’ library in the above code and hence we need to link this library while compiling our file by using:

cc -c filename.c cc -o filename filename.c -lm

number_of_digits = ((int)math.log10(number))+1 – We are just calculating the number of digits here.

Let’s take a case of 153.

pow(temp%10,number_of_digits) will calculate (3*3*3) and temp = temp/10 will change 153 to 15. In the next iteration, the value of ‘sum_arm’ will become (3*3*3)+(5*5*5) and temp = temp/10 will change 15 to 1. And in the last iteration the value of ‘sum_arm’ will become (3*3*3) + (5*5*5) + (1*1*1).

Liked the post?
Developer and founder of CodesDope.
Editor's Picks