We have learned how to print any statement or number in the First C Program. Now we will print one to ten numbers using loops in c programming.
C Program To Display 1-10 Numbers Using While Loop
#include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("\n"); //for new line // While Loop n=1; //Initialize while(n<=10) // Condition { printf(" %d",n); n++; // Increment } getch(); }
There is a little bit of difference in the while and do while loop. In while loop, the condition is checked first and in do while loop the condition is checked after the first execution of the codeblock.
Using Do While Loop We can Print 1-10 Numbers as Follow:
#include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("\n"); //for new line // Do While Loop n=1; // Initialize do { printf(" %d",n); n++; // Increment } while(n<=10); // Condition getch(); }
In three of these loops, For Loop is bit easy to use as it has the all the code to be used in single line
C Program To Print Numbers From 1 To 10 Using For Loop
#include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("\n"); //for new line /* For loop (initialize; condition ; increment) */ for(n=1;n<=10;n++) { printf(" %d",n); } getch(); }
Output :
Dev C++ Source Code to Print 1-10 Numbers Using For Loop
#include<stdio.h> int main() { int n; printf("\n"); for(n=1;n<=10;n++) { printf(" %d",n); } getch(); return 0; }
In this way, you can print 1-10 Numbers in C. Also, by modifying the code a little bit you can print the range of numbers you want, just change the initial and last values in the condition and that range will get printed on the output screen.