To solve a problem, if some statements need to be executed repetitively, a loop is used. In this programming language, three types of loops are used. These are: for, while, and do-while.
for loop
Let us discuss this with an example. In the following program, we will calculate the factorial of a number. We know that factorial can not be calculated for a negative number. We can calculate the factorial if the number is 0 or positive.
We know that the factorial of a number is calculated by multiplying numbers from 1 to that number. Hence, we need to perform multiplication operations repetitively. So a loop is required in order to solve the problem. In the following example, we are using for loop.
#include <stdio.h>
int main (void)
{
int n, i, result;
printf("This program is being used for factorial calculation\n");
result = 1;
printf("Enter value of n:");
scanf("%d", &n);
if(n < 0)
{
printf("Wrong input (n should be >= 0)\n");
}
else
{
for (i = 2; i <= n; i++)
{
result = result * i;
}
printf("Factorial of %d is %d\n", n, result);
}
return 0;
}
In a for loop, there are three segments. The first part is known as initialization, where one or more variables are initialized. If there is no variable to initialize, the part can be blank. The next part is a condition, the loop executes repetitively when this condition is true. If the condition is false at any instance, the loop terminates and control of the program goes to the end of the loop. The last part is the increment or decrement operation. If one or more variables need to be increased or decreased, the statements are written here.
while loop
Here we will write the same program using the while loop. In a while loop, only a conditional statement can be written. Hence, initialization is performed before entering the loop, and increment or decrement operation is performed inside the loop.
The next program is written to calculate the factorial of a number using a while loop.
#include <stdio.h>
int main (void)
{
int n, i, result;
printf("This program is being used for factorial calculation\n");
result = 1;
printf("Enter value of n:");
scanf("%d", &n);
if(n < 0)
{
printf("Wrong input (n should be >= 0)\n");
}
else
{
i = 2;
while (i <= n)
{
result = result * i;
i++;
}
printf("Factorial of %d is %d\n", n, result);
}
return 0;
}
C does not support the boolean data type. So in C, we use 1 as true and 0 as false. In the following example, for and while loops are used simultaneously. In every run, the programs discussed above will execute and calculate the factorial of a number just once. If we want to run this program for a long time and in every iteration factorial is calculated by taking input from the user. We can rewrite the code as shown below.
#include <stdio.h>
int main (void)
{
int n, i, result;
printf("This program is being used for factorial calculation\n");
while(1)
{
result = 1;
printf("Enter value of n:");
scanf("%d", &n);
if(n < 0)
{
printf("Wrong input (n should be >= 0)\n");
}
else
{
for (i = 2; i <= n; i++)
{
result = result * i;
}
printf("Factorial of %d is %d\n", n, result);
}
}
return 0;
}
Similar to if-else, loops can be nested.
do-while loop
This loop is known as the exit control loop. If repetitive tasks need to be executed at least once, we prefer a do-while loop instead of a while loop.
At the time of entering a loop, if the condition is false program control will not enter in the while loop. If we use do-while, the program control will enter the loop without any condition checking. The condition will be checked at the end of the loop. If the condition is true, the loop will be repeated.
You may try the following code and look into the output carefully.
#include <stdio.h>
int main()
{
int j=0;
do
{
printf("Value of variable j is: %d\n", j);
j++;
}while (j<=3);
return 0;
}
#include <stdio.h>
int main()
{
int j=0;
while (j<=3)
{
printf("Value of variable j is: %d\n", j);
j++;
}
return 0;
}
Difference between for and while loop
If you want to write a program where an entry-controlled loop is required, you can choose either for or while loop. If the logic can be implemented using for loop, obviously the same logic can be implemented using a while. There is no difference.
While loop comes as a natural choice when the number of iterations that have to be performed is not known apriori and it depends on some dynamic conditions
But when you already know the number of iterations that you have to perform, in that case, you should use for loop.
Fibonacci Series
#include <stdio.h>
int main() {
int n, i;
long int n1, n2, n3;
printf("Enter the number:");
scanf("%d", &n);
n1 = n2 = 1;
if (n == 1)
printf("1");
else
{
printf("1\t1");
for (i = 1; i <= n - 2; i++)
{
n3 = n1 + n2;
printf("\t%ld", n3);
n1 = n2;
n2 = n3;
}
}
return 0;
}
Lattin Square
#include <stdio.h>
int main()
{
int i, j, k, n;
printf("Enter the order of Lattin Square:\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\n");
for (j = i; j < n; j++)
printf("%d", (j + 1));
for (k = 0; k < i; k++)
printf("%d", (k + 1));
}
return 0;
}
Number system conversion
#include <stdio.h>
int main() {
int x, y, i, j, a[30], h, u, m, k;
printf("\n Enter-1 for DECIMAL TO BINARY");
printf("\n Enter-2 for DECIMAL TO OCTAL");
printf("\n Enter-3 for DECIMAL TO HEXADECIMAL");
printf("\n\n Now Enter your choices:\n\n");
scanf("%d", &y);
switch (y) {
case 1:
printf("Enter a Decimal no:\n");
scanf("%d", &x);
i = 0;
while (x > 0) {
a[i] = x % 2;
x = x / 2;
i++;
}
printf("The corresponding Binary no is:\n");
for (j = i - 1; j >= 0; j--) {
printf("%d", a[j]);
}
break;
case 2:
printf("\n Enter a Decimal no:\n");
scanf("%d", &x);
k = 0;
while (x > 0) {
a[k] = x % 8;
x = x / 8;
k++;
}
printf("\n The corresponding Octal no is:\n");
for (m = k - 1; m >= 0; m--) {
printf("%d", a[m]);
}
break;
case 3:
printf("Enter a Decimal no:\n");
scanf("%d", &x);
h = 0;
while (x > 0) {
a[h] = x % 16;
x = x / 16;
h++;
}
printf("\n The corresponding Hexadecimal no is:\n");
for (u = h - 1; u >= 0; u--) {
printf("%d", a[u]);
}
break;
default:
printf("Wrong Entry");
}
return 0;
}
Prime number checking
#include <stdio.h>
int main (void)
{
int num, i, isprime = 1;
printf("Prime Checker!");
printf("Enter a number: ");
scanf("%d", &num);
if(num < 0)
{
printf("Invalid input");
exit(1);
}
for (i = 2; i <= num/2; i++)
{
if (num % i == 0)
{
isprime = 0;
}
}
if (isprime)
printf("prime\n");
else
printf("not prime\n");
return 0;
}
If you want to understand loop control with some example, you may go through Patterns in Sample Programs section.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.