Print 1 to 10 using do while loop in C

Program example to print 1 to 10 using do while loop in C.

Steps are

  • Initialize start number with 1
  • Initialize target number to 10
  • Enter the do while loop
  • print the number
  • increment the number
  • put condition in while, so that if the value of num exceeds 10, then do while loop will be terminated.

C Example – Printing 1 to 10 number using do while loop

/*--------------------------------------------------------
* Print 1 to 10 using do while loop in C - program example
*/

#include<stdio.h>

int main(){

	/* Initialize starting number */
	int num = 1;

	/* Initialize target number */
	int target = 10;

	//Start do while loop
	 do{
		 /*
		* Print numbers on console
		* use escape sequence \n to print
		* Next number in new line
		*/

		printf("%d\n", num);

		//increment the number by 1
		++num;

		//once number is > target in while condition
		//then control will be out of loop
	 }while (num <= target);

	return 0;
}

Output:
1
2
3
4
5
6
7
8
9
10

Related Posts