LATEST

Do while loop in C / C++ with examples

In last tutorial, we discussed for loop in C / C++ with examples and while loop in C / C++ with examples. In this tutorial. We will learn about the C / C++ do while loop and its working with the help of some examples.

Top 15 do while loop in C / C++ with examples

Here, we will give 10 examples of do while loop in C/C++. These examples are written in two different languages, C and C++.

Example 1: Printing numbers from 1 to 100 with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int i = 1;

  cout<<"Numbers from 1 to 100:\n ";
  do{
    cout << i << " ";
    i++;
  } 
  while (i <= 100);
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int i = 1;
  printf("Numbers from 1 to 100: \n");
  do{
    printf("%d ", i);
    i++;
  }
  while (i <= 100);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 01 PNG

Example 2: Displaying a text 10 times with for do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int i = 1;
  cout<<"Displaying a text 10 times with do while loop in C / C++. \n\n";
  do{
    cout <<"Hello, I'm learnc.net\n";
    i++;
  }
  while (i <= 10);
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int i = 1;
  printf("Displaying a text 10 times with do while loop in C / C++. \n\n");
  do{
    printf("Hello, I'm learnc.net\n");
    i++;
  }
  while (i <= 10);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 02 PNG

Example 3: Find the sum of first n Natural Numbers with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int num, sum, i = 1;
  sum = 0;
  cout << "Enter a positive integer: ";
  cin >> num;
  do{
    sum += i;
    i++;
  }
  while(i <= num);
  cout << "Sum = " << sum << endl;
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int num, sum, i = 1, j = 1;
  sum = 0;
  printf("Enter a positive integer: ");
  scanf("%d", &num);

  printf("Numbers from 1 to %d: ",num);
  do{
    printf("%d ", j);
    j++;
  }
  while (j <= num);
  do{
    sum += i;
    i++;
  }
  while (i <= num);
  printf("\nSum = %d\n", sum);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 03 PNG

Example 4: Display elements of array using do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int arr[]={19, 2, 17, 1, 2000, 2003};
  int i = 0;
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   cout<<"Elements of array: ";
   do{
      cout<<arr[i]<<" ";
      i++;
   }
   while(i<6);
  
  cout<<"\n\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int arr[]={19, 2, 17, 1, 2000, 2003};
  int i = 0;
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   printf("Elements of array: ");
   do{
      printf("%d ",arr[i]);
      i++;
   }
   while(i<6);
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 04 PNG

Example 5: Displaying even numbers from 1 to n with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int n, i = 1;
  cout<<"Enter number n: ";
  cin>>n;
  cout<<"Even numbers from 1 to "<< n<<" is: \n";
  do{
    if(i % 2 == 0)
      cout<<i<<" ";
    i++;
  }
  while(i <= n);
  
  cout<<"\n\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

int main(void) {
  int n, i = 1;
  printf("Enter number n: ");
  scanf("%d", &n);
  printf("Even numbers from 1 to %d is: ", n);
  do{
    if(i % 2 == 0)
      printf("%d ", i);
    i++;
  }
  while(i <= n);
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 05 PNG

Example 6: Displaying odd numbers from 1 to n with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int n, i = 1;
  cout<<"Enter number n: ";
  cin>>n;
  cout<<"Odd numbers from 1 to "<< n<<" is: \n";
  do{
    if(i % 2 == 1)
      cout<<i<<" ";
    i++;
  }
  while(i <= n);
  
  cout<<"\n\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

int main(void) {
  int n, i = 1;
  printf("Enter number n: ");
  scanf("%d", &n);
  printf("Odd numbers from 1 to %d is: ", n);
  do{
    if(i % 2 == 1)
      printf("%d ", i);
    i++;
  }
  while(i <= n);
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 06 PNG

Example 7: Displaying primes from 1 to n with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;

bool isPrimes(int n)
{
    if (n < 2){
        return false;
    }       
    if (n == 2){
        return true;
    }
    if (n % 2 == 0){
        return false;   
    }
    for (int i = 3; i < (n - 1); i += 2){
        if (n % i == 0){
            return false;
        }   
    }
    return true;
}

int main() {
  int n, i = 1;
  cout<<"Enter number n: ";
  cin>>n;
  cout<<"Primes from 1 to "<< n<<" is: \n";
  do{
    if(isPrimes(i))
      cout<<i<<" ";
    i++;
  }
  while(i <= n);
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

bool isPrimes(int n)
{
    if (n < 2){
        return false;
    }       
    if (n == 2){
        return true;
    }
    if (n % 2 == 0){
        return false;   
    }
    for (int i = 3; i < (n - 1); i += 2){
        if (n % i == 0){
            return false;
        }   
    }
    return true;
}

int main(void) {
  int n, i = 1;
  printf("Enter number n: ");
  scanf("%d", &n);
  printf("Primes from 1 to %d is: ", n);
  do{
    if(isPrimes(i))
      printf("%d ", i);
    i++;
  }
  while(i <= n);
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 07 PNG

Example 8: Displaying even numbers in array with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7};
  int i = 0, j = 0;
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   cout<<"Elements of array: ";
   do{
      cout<<arr[i]<<" ";
      i++;
   }
   while(i<9);
   cout<<"\nEven numbers in array: ";
   do{
      if(arr[j] % 2 == 0)
        cout<<arr[j]<<" ";
      j++;
   }
   while(j<9);
  
  cout<<"\n\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

int main(void) {
  int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7};
  int i = 0, j = 0;
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   printf("Elements of array: ");
   do{
      printf("%d ",arr[i]);
      i++;
   }
   while(i<9);
   printf("\nEven numbers in array: ");
   do{
      if(arr[j] % 2 == 0)
        printf("%d ",arr[j]);
      j++;
   }
   while(j<9);
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 08 PNG

Example 9: Displaying odd numbers in array with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7};
  int i = 0, j = 0;
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   cout<<"Elements of array: ";
   do{
      cout<<arr[i]<<" ";
      i++;
   }
   while(i<9);
   cout<<"\nOdd numbers in array: ";
   do{
      if(arr[j] % 2 == 1)
        cout<<arr[j]<<" ";
      j++;
   }
   while(j<9);
  
  cout<<"\n\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

int main(void) {
  int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7};
  int i = 0, j = 0;
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   printf("Elements of array: ");
   do{
      printf("%d ",arr[i]);
      i++;
   }
   while(i<9);
   printf("\nOdd numbers in array: ");
   do{
      if(arr[j] % 2 == 1)
        printf("%d ",arr[j]);
      j++;
   }
   while(j<9);
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 09 PNG

Example 10: Displaying primes numbers in array with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;

bool isPrimes(int n)
{
    if (n < 2){
        return false;
    }       
    if (n == 2){
        return true;
    }
    if (n % 2 == 0){
        return false;   
    }
    for (int i = 3; i < (n - 1); i += 2){
        if (n % i == 0){
            return false;
        }   
    }
    return true;
}

int main() {
  int arr[]={1, 5, 9, 7, 11};
  int i = 0, j = 0;
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   cout<<"Elements of array: ";
   do{
      cout<<arr[i]<<" ";
      i++;
   }
   while(i<5);
   cout<<"\nPrimes in array: ";
   do{
      if(isPrimes(arr[j]))
        cout<<arr[j]<<" ";
      j++;
   }
   while(j<5);
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

bool isPrimes(int n)
{
    if (n < 2){
        return false;
    }       
    if (n == 2){
        return true;
    }
    if (n % 2 == 0){
        return false;   
    }
    for (int i = 3; i < (n - 1); i += 2){
        if (n % i == 0){
            return false;
        }   
    }
    return true;
}

int main(void) {
  int arr[]={1, 5, 9, 7, 11};
  int i = 0, j = 0;
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   printf("Elements of array: ");
   do{
      printf("%d ",arr[i]);
      i++;
   }
   while(i<5);
   printf("\nPrimes in array: ");
   do{
      if(isPrimes(arr[j]))
        printf("%d ",arr[j]);
      j++;
   }
   while(j<5);
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 10 PNG

Example 11: Find the sum of elements in array with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;
 
int main() {
  int sum = 0, i = 0, j = 0;
  int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7};
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   cout<<"Elements of array: ";
   do{
      cout<<arr[i]<<" ";
      i++;
   }
   while(i<9);

   do{
      sum += arr[j];
      j++;
   }
   while(j<9);
   cout<<"\nSum of elements in array: "<<sum;
   
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>
 
int main(void) {
  int sum = 0, i = 0, j = 0;
  int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7};
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   printf("Elements of array: ");
   do{
      printf("%d ",arr[i]);
      i++;
   }
   while(i<9);
   do{
      sum += arr[j];
      j++;
   }
   while(j<9);
   printf("\nSum of elements in array: %d",sum);
   
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 11 PNG

Example 12: Find the sum of even elements in array with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;
 
int main() {
  int sum = 0, i = 0, j = 0;;
  int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7};
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   cout<<"Elements of array: ";
   do{
      cout<<arr[i]<<" ";
      i++;
   }
   while(i<9);

   do{
     if(arr[j] % 2 == 0) sum += arr[j];
     j++;
   }
   while(j<9);
  cout<<"\nSum of even elements in array: "<<sum;
   
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>
 
int main(void) {
  int sum = 0, i = 0, j = 0;
  int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7};
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   printf("Elements of array: ");
  do{
      printf("%d ",arr[i]);
      i++;
   }
   while(i<9);
  do{
      if(arr[j] % 2 == 0) sum += arr[j];
      j++;
   }
   while(j<9);
   printf("\nSum of even elements in array: %d",sum);
   
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 12 PNG

Example 13: Find the sum of odd elements in array with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;
 
int main() {
  int sum = 0, i = 0, j = 0;;
  int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7};
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   cout<<"Elements of array: ";
   do{
      cout<<arr[i]<<" ";
      i++;
   }
   while(i<9);

   do{
     if(arr[j] % 2 == 1) sum += arr[j];
     j++;
   }
   while(j<9);
  cout<<"\nSum of odd elements in array: "<<sum;
   
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>
 
int main(void) {
  int sum = 0, i = 0, j = 0;
  int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7};
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   printf("Elements of array: ");
  do{
      printf("%d ",arr[i]);
      i++;
   }
   while(i<9);
  do{
      if(arr[j] % 2 == 1) sum += arr[j];
      j++;
   }
   while(j<9);
   printf("\nSum of odd elements in array: %d",sum);
   
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 13 PNG

Example 14: Dislaying the multiplication table with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;
 
int main() {
  int n, i = 1;
  cout<<"Enter number n: ";
  cin>>n;
  do{
    cout<<n<<" x " <<i<<" = "<<n*i<<"\n";
    i++;
  }
  while(i<= 9);
   
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>
 
int main(void) {
  int n, i = 0;
  printf("Enter number n: ");
  scanf("%d", &n);
  do{
    printf("%d x %d = %d\n",n,i,n*i);
    i++;
  }
  while(i<= 9);
   
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 14 PNG

Example 15: Display a menu with do while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int selection;
    do
    {
      cout << "\n------------Menu------------- \n";
      cout << "1) Withdraw money\n";
      cout << "2) Transfers\n";
      cout << "3) Recharge\n";
      cout << "4) Check account\n";
      cout << "Your choice is: ";
      cin >> selection;
      if(selection < 1 || selection > 4)
        cout<<"\nYou can only choose from 1 to 4, please choose again!\n";
    } while (selection < 1 || selection > 4);
 
    cout << "You have selected option: #" << selection << "\n";
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
  
int main(void) {
  int selection;
    do
    {
      printf("\n------------Menu------------- \n");
      printf("1) Withdraw money\n");
      printf("2) Transfers\n");
      printf("3) Recharge\n");
      printf("4) Check account\n");
      printf("Your choice is: ");
      scanf("%d", &selection);
      if(selection < 1 || selection > 4)
        printf("\nYou can only choose from 1 to 4, please choose again!\n");
    } while (selection < 1 || selection > 4);
 
    printf("You have selected option: #%d\n",selection);
    
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 15 PNG

Cùng chuyên mục:

Strings in C/C++ with examples

Strings in C/C++ with examples

Arrays in C / C++ with examples

Arrays in C / C++ with examples

Switch...case statement in C / C++ with example

Switch...case statement in C / C++ with example

Multiple conditions for if statement in C / C++

Multiple conditions for if statement in C / C++

Can if statement have 2 conditions? Can you have 3 conditions in an if statement?…

if - else statement in C / C++ with example

if - else statement in C / C++ with example

While loop in C / C++ with examples

While loop in C / C++ with examples

For loop in C / C++ with examples

For loop in C / C++ with examples

Top