Please disable your adblock and script blockers to view this page
 

What is For loop in C language? Program to print counting up to given limit

Introduction: In this article i am going to explain the definition of For loop in C Language and How to print the counting up to specified limit e.g. 1 to 5 or 5 to 1 using for loop with example program.
In previous articles i explained What is goto statement,its advantages and disadvantages with program and Find greatest of three numbers using multiple if statement and check vowel or consonant using switch case and Get number of days in month and year using else if ladder and Calculate sum,average and division of marks using else-if ladder

What is For loop in C language?

Description: For loop is an iterative statement that allows the code to be executed repeatedly specified number of times.
The statements written within the For loop repeat continuously for a specific number of times. For loop repeats the statement or group of statements until the specific count is met.  For loop can be used in the situation where the number of repetition is known i.e. how many times statement or the group of statements is to be executed is known in advance.

Syntax:
for (initialize counter variable ; condition ; increment/decrement the counter variable)
{
                code to be executed;
}

 Implementation: Let's create a C language program to print the counting up to specified limit.

Here i have created two programs:
  • Printing counting from 1 to given limit
  • Printing counting from given limit to 1 

//Printing counting from 1 to given limit
#include<conio.h>
#include<stdio.h>
void main()
{
 int limit,i;
 clrscr();
 printf("Enter the limit: ");
 scanf("%d",&limit);
 printf("Counting from 1 to %d\n",limit);
  for(i=1;i<=limit;i++)
   {
    printf("%d\n",i);
   }
 getch();
}
  •  Run the program using Ctrl+F9 

Output:  Enter the limit: 5
Counting from 1 to 5
1
2
3
4
5

//Printing counting from given limit to 1

#include<conio.h>
#include<stdio.h>
void main()
{
 int limit,i;
 clrscr();
 printf("Enter the limit: ");
 scanf("%d",&limit);
 printf("Counting from %d to 1\n",limit);
  for(i=limit;i>=1;i--)
  {
   printf("%d\n",i);
  }
 getch();
} 
  • Run the program using Ctrl+F9 

Output:  Enter the limit: 5
Counting from 5 to 1
5
4
3
2
1

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon