Print Number Using Do While Loop


Explanation

The do while loop is used to execute a block of statements repeatedly until the given condition becomes false. In this program, the loop starts from number 1 and prints numbers up to 10 and vice versa. The statements inside the do block are executed first, and then the condition is checked.

How It Works?

  • Step 1: Declare an integer variable k and initialize it with 1.
  • Step 2: The do block starts execution.
  • Step 3: Display the value of k.
  • Step 4: Increase the value of k by 1.
  • Step 5: Check the condition k <= 10.
  • Step 6: Repeat the loop until the condition becomes false.

Example Program

Print Number 1 To 10


#include <stdio.h>
#include <conio.h>


main()
{


    int k = 1;


    do

    {


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


        k = k + 1;


    }

    while(k <= 10);


    getch();


}


Output

1

2

3

4

5

6

7

8

9

10


Print Number 10 To 1



#include <stdio.h>
#include <conio.h>


main()
{


    int k = 10;


    do

    {


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


        k = k - 1;


    }

    while(k >= 1);


    getch();


}


Output
10

9

8

7

6

5

4

3

2

1

Practice Exercises

Task 1: Write a C program using the do while loop to print numbers from 1 to 20.

Goal: Learn how to use the do while loop for repeating statements and printing a sequence of numbers.

💡 Hint: Initialize variable k = 1. Print the value of k inside the do block and increase it by 1. Continue the loop while k <= 20.
💡 Show Solution


#include <stdio.h>
#include <conio.h>


main()
{

    int k = 1;


    do

    {

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

        k = k + 1;

    }

    while(k <= 20);


    getch();


}


Output
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
Explanation: The program uses a do while loop to print numbers from 1 to 20. The loop executes the statements first and then checks the condition. The value of k increases by 1 after every iteration until it reaches 20.