Categories: C

do-while loop examples

Here are a few examples of the do-while loop.

Printing table of a number using do-while:
#include<stdio.h>  
int main(){    
 int i=1;      
 do{    
    printf("6 * %d = %d \n",i,i*6);    
    i++;    
  }while(i<=10);   
 return 0;  
}
output:
6 * 1 = 6 
6 * 2 = 12 
6 * 3 = 18 
6 * 4 = 24 
6 * 5 = 30 
6 * 6 = 36 
6 * 7 = 42 
6 * 8 = 48 
6 * 9 = 54 
6 * 10 = 60 
  • using the do-while loop, the value of ‘i’ is multiplied by 6.
  • The loop continues till the value of ‘i’ is less than or equal to 10.
  • Finally, the table of the given number is printed.
Calculating factorial of a number using a do-while loop:
#include<stdio.h>

void main()
{
    int n,i=1,f=1;
    
     
    printf("\n Enter The Number:");
    scanf("%d",&n);
     
    //LOOP TO CALCULATE THE FACTORIAL OF A NUMBER
    do
    {
        f=f*i;
        i++;
    }while(i<=n);
     
    printf("\n The Factorial of %d is %d",n,f);
   
}
output:
Enter The Number:5
The Factorial of 5 is 120
  • first, the computer reads the number to find the factorial of the number from the user.
  • Then, using the do-while loop, the value of ‘i’ is multiplied by the value of ‘f’.
  • The loop continues till the value of ‘i’ is less than or equal to ‘n’.
  • Finally, the factorial value of the given number is printed.
printing reverse of a number:
#include<stdio.h>

void main()
{
    int num,reverse=0,rem;
    printf("Enter a number: ");
    scanf("%d",&num);
    
    do{
        rem=num%10;
        reverse=reverse*10+rem;
        num/=10;
    }while(num!=0);
    printf("Reverse number %d\n",reverse);
    
}
output:
Enter a number: 12345
Reverse number 54321
  • first, the computer reads the number to find the reverse of the number from the user.
  • Then, using the do-while loop, the digit of the number is extracted and added in the ‘reverse’.
  • The loop continues till the value of ‘num’ is not equal to 0.
  • Finally, the reverse value of the given number is printed.

In the above-given do-while loop examples, we could clearly see that it is an exit-controlled loop.

Note: also read about do-while loop in c & while loop examples in c

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago