Hello everyone, in this article, you are going to learn about the "for" loop in "c" programming language with easy and simple examples. The table of contents given below.
What is the "for" loop in "c" programming language?
The for loop is used to execute a section of code or sequence of statements repeatedly as long as the given condition is true. A for loop in c is divided into two parts, the first one is the control statement and the second one is the body of the loop. The control statement is a combination of initialization, condition, and increment or decrement. It directs the body of the loop to execute until the condition becomes false. The for loop in c programming is an entry control loop. In an entry control loop, the condition is checked, first and then the body of the loop is executed.
for (init-statement ; condition ; expression)
{
//Body of the loop
}
here the init-statement is the initialization part and the expression is the increment or decrement part.
for (initialization ; condition ; increment or decrement)
{
//Body of the loop
}
for loop flowchart:
Let's understand the above flow chart:
step 1: Start
step 2: initialize the counter variable, suppose we have an integer variable named "i" then we can write i=any integer value. For example, i=0;
step 3: Test condition can be a specific value or can be another value. For example "i<10" is a specific value while "i<a" is another value.
step 4: If the condition is true then it will execute the loop body
step 5: After executing the loop body, the counter will be updated, for example, "++i" or "--i". Then it will again go to test condition.
step 6: If the condition becomes false the loop will be stopped.
Basic things to remember when using for loop:
When you are using for loop in c programming, some basic things need to be remembered.
The control statement of the for loop is must be well defined otherwise it will execute infinite times. This situation occurs when either the termination condition is not defined or the termination condition is never meet.
The following Examples illustrates the for loop in C programming examples:
Example 1: Write a program in C to display the first 10 natural numbers using for loop
#include <stdio.h>
int main() {
int n;
printf("The first 10 natural numbers are: ");
for (n=1;n<=10;++n) // for loop to print the natural numbers
{
printf("%d ",n); // print the numbers
}
return 0;
}
Output:
The first 10 natural numbers are: 1 2 3 4 5 6 7 8 9 10
Example 2: Write a C program to find the sum of the first 10 natural numbers using for loop
#include <stdio.h>
int main() {
int n,sum=0;
for (n=1;n<=10;++n) //for loop to add first 10 netural numbers
{
sum=sum+n; // sum of natural numbers
}
printf("The sum of first 10 netural numbers is: %d",sum);
return 0;
}
Output:
The sum of the first 10 natural numbers is: 55
1. Here we have declared two int variables among them we initialize value 0 to one variable. Example 3: Write a program in C to display n terms of natural number and their sum. using for loop
#include <stdio.h>
int main() {
int n, sum=0, input;
printf("Enter the value of terms: ");
scanf("%d",&input);
printf("The first %d natural numbers are: ",input);
for (n=1;n<=input;++n)
{
printf("%d ",n);
sum=sum+n;
}
printf("\nThe sum of the first %d natural numbers are: %d",input,sum);
return 0;
}
Output:
Enter the value terms: 7
The first 7 natural numbers are: 1 2 3 4 5 6 7
The sum of the first 7 natural numbers is: 28
Example 4: Write a program in C to read 10 numbers from the keyboard and find their sum and average using for loop
#include <stdio.h>
int main() {
int n, sum=0, input;
float avg;
printf("Enter 10 numbers: ");
for (n=1;n<=10;++n)
{
printf("\nEnter number - %d ",n);
scanf("%d",&input);
sum=sum+input;
}
avg=sum/10.0;
printf("\nThe sum of the given numbers are: %d",sum);
printf("\nThe average of the given numbers is: %f",avg);
return 0;
}
Output:
Enter 10 numbers:
Enter number - 1 10
Enter number - 2 9
Enter number - 3 8
Enter number - 4 7
Enter number - 5 6
Enter number - 6 5
Enter number - 7 4
Enter number - 8 3
Enter number - 9 2
Enter number - 10 1
The sum of the given numbers are: 55
The average of the given numbers is: 5.500000
1. Here we have declared three int variables and one float variable among them we initialize value 0 to one variable.
Example 5: Write a program in C to display the cube of the number up to give an integer by the user using for loop
#include <stdio.h>
int main() {
int n, input,result;
printf("Enter the value of terms: ");
scanf("%d",&input);
for (n=1;n<=input;++n)
{
result=(n*n*n);
printf("\nThe Qube of %d is %d",n,result);
}
return 0;
}
Output:
Enter the value of terms: 3
The Qube of 1 is 1
The Qube of 2 is 8
The Qube of 3 is 2
1. Here we have declared three int variables and one float variable among them we initialize value 0 to one variable.
Explanation:
The value entered by the user is stored in the input variable. Suppose the user entered 3.The n
is initialized to 1 and test expression is evaluated. Since the test expression is true the body of the for loop executed, in the body first we perform multiplication three times between the n and stored it in the result variable. After executing the body of the loop the update statement is executed and n will equal 2. Again the test expression is evaluated.
This process goes on until the n reaches 4. When the n is 4, the test expression is evaluated to false, and the loop terminates.
The value entered by the user is stored in the input variable. Suppose the user entered 3.The n
is initialized to 1 and test expression is evaluated. Since the test expression is true the body of the for loop executed, in the body first we perform multiplication three times between the n and stored it in the result variable. After executing the body of the loop the update statement is executed and n will equal 2. Again the test expression is evaluated.
This process goes on until the n reaches 4. When the n is 4, the test expression is evaluated to false, and the loop terminates.
Example 6: Write a program in C to display the multiplication a given integer, using for loop
#include <stdio.h>
int main() {
int n, input,result;
printf("Enter the table number to be calculated: ");
scanf("%d",&input);
for (n=1;n<=10;++n)
{
result=input*n;
printf("\n%d X %d = %d",input,n,result);
}
return 0;
}
Output:
Enter the table number to be calculated: 12
12 X 1 = 12
12 X 2 = 24
12 X 3 = 36
12 X 4 = 48
12 X 5 = 60
12 X 6 = 72
12 X 7 = 84
12 X 8 = 96
12 X 9 = 108
12 X 10 = 120
1. Here we have declared three int variables and one float variable among them we initialize value 0 to one variable.
2. We take the input Table number from the user and stored it into an integer variable.
3. In for loop, we initialize 'n' with value 1. In the condition part, we specified our condition and then the incremental part.
4. In the body of the loop we specified our condition to calculate the Table of a given number and print the values one by one.
Explanation:
The value entered by the user is stored in the input variable. Suppose the user entered 12.The n
is initialized to 1 and test expression is evaluated. Since the test expression is true the body of the for loop executed, in the body first we perform multiplication between the result and n and stored it in the result variable. After executing the body of the loop the update statement is executed and n will equal 2. Again the test expression is evaluated.
This process goes on until the n reaches 11. When the n is 11, the test expression is evaluated to false, and the loop terminates.
The value entered by the user is stored in the input variable. Suppose the user entered 12.The n
is initialized to 1 and test expression is evaluated. Since the test expression is true the body of the for loop executed, in the body first we perform multiplication between the result and n and stored it in the result variable. After executing the body of the loop the update statement is executed and n will equal 2. Again the test expression is evaluated.
This process goes on until the n reaches 11. When the n is 11, the test expression is evaluated to false, and the loop terminates.
Example 7: Write a program in C to display the n terms of odd natural number and their sum using for loop
#include <stdio.h>
int main() {
int n, input,sum=0,result;
printf("Enter the value of terms: ");
scanf("%d",&input);
printf("The first %d odd natural numbers are: ",input);
for (n=1;n<=input;++n)
{
result=2*n-1;
printf("%d ",result);
sum=sum+result;
}
printf("\nThe sum of the first %d odd natural numbers is %d",input,sum);
return 0;
}
Output:
Enter the value of terms: 5
The first 5 odd natural numbers are: 1 3 5 7 9
The sum of the first 5 odd natural numbers is 25
1. Here we have declared four int variables and one float variable among them we initialize value 0 to one variable.
2. We take input the nth term from the user and stored it into an integer variable.
3. In for loop, we initialize 'n' with value 1. In the condition part, we specified our condition and then the incremental part.
4. In the body of the loop we specified our condition to calculate the Odd natural numbers and print them one by one.
Explanation:
The value entered by the user is stored in the input
variable. Suppose the user entered 5.
The n
is initialized to 1 and test expression is evaluated. Since the test expression is true the body of the for loop executed, in the body first we calculate the odd natural number and then stored it in the result variable after that we print the result in the console then we perform addition between the result and sum and stored in the sum variable. After executing the body of the loop the update statement is executed and n will equal 2. Again the test expression is evaluated.
This process goes on until the n reaches 6. When the n is 6, the test expression is evaluated to false, and the loop terminates.
0 Comments