LATEST

While loop in C / C++ with examples

In this tutorial, we will learn about the C / C++ while loop and its working with the help of some examples. This is one of the basic knowledge in C / C++ programming language, so please learn it carefully.

Top 20 while loop in C / C++ with examples

Here, we will implement examples using while loop in C / C++ from basic to advanced. For each example, we will implement with two different languages C and C++.

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

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int i = 1;

  cout<<"Numbers from 1 to 200:\n ";
  while (i <= 200) {
    cout << i << " ";
    i++;
  } 
  
  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 200: \n");
  while (i <= 200) {
    printf("%d ", i);
    i++;
  }

  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 while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int i = 1;
  cout<<"Displaying a text 10 times with for while in C / C++. \n\n";
  while (i <= 10) {
    cout <<"Hello, I'm learnc.net\n";
    i++;
  }
  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 for while in C / C++. \n\n");
  while (i <= 10) {
    printf("Hello, I'm learnc.net\n");
    i++;
  }

  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 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;
  
  while(i <= num) {
    sum += i;
    i++;
  }
  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);
  while (j <= num) {
    printf("%d ", j);
    j++;
  }
  
  while (i <= num) {
    sum += i;
    i++;
  }
  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 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: ";
   while(i<6){
      cout<<arr[i]<<" ";
      i++;
   }
  
  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: ");
   while(i<6){
      printf("%d ",arr[i]);
      i++;
   }
  
  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 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";
  while(i <= n){
    if(i % 2 == 0)
      cout<<i<<" ";
    i++;
  }
  
  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);
  while(i <= n){
    if(i % 2 == 0)
      printf("%d ", i);
    i++;
  }
  
  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 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";
  while(i <= n){
    if(i % 2 == 1)
      cout<<i<<" ";
    i++;
  }
  
  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);
  while(i <= n){
    if(i % 2 == 1)
      printf("%d ", i);
    i++;
  }
  
  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 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";
  while(i <= n){
    if(isPrimes(i))
      cout<<i<<" ";
    i++;
  }
  
  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);
  while(i <= n){
    if(isPrimes(i))
      printf("%d ", i);
    i++;
  }
  
  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 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: ";
   while(i<9){
      cout<<arr[i]<<" ";
      i++;
   }
   cout<<"\nEven numbers in array: ";
   while(j<9){
      if(arr[j] % 2 == 0)
        cout<<arr[j]<<" ";
      j++;
   }
  
  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: ");
   while(i<9){
      printf("%d ",arr[i]);
      i++;
   }
   printf("\nEven numbers in array: ");
   while(j<9){
      if(arr[j] % 2 == 0)
        printf("%d ",arr[j]);
      j++;
   }
  
  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 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: ";
   while(i<9){
      cout<<arr[i]<<" ";
      i++;
   }
   cout<<"\nOdd numbers in array: ";
   while(j<9){
      if(arr[j] % 2 == 1)
        cout<<arr[j]<<" ";
      j++;
   }
  
  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: ");
   while(i<9){
      printf("%d ",arr[i]);
      i++;
   }
   printf("\nOdd numbers in array: ");
   while(j<9){
      if(arr[j] % 2 == 1)
        printf("%d ",arr[j]);
      j++;
   }
  
  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 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: ";
   while(i<5){
      cout<<arr[i]<<" ";
      i++;
   }
   cout<<"\nPrimes in array: ";
   while(j<5){
      if(isPrimes(arr[j]))
        cout<<arr[j]<<" ";
      j++;
   }
  
  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: ");
   while(i<5){
      printf("%d ",arr[i]);
      i++;
   }
   printf("\nPrimes in array: ");
   while(j<5){
      if(isPrimes(arr[j]))
        printf("%d ",arr[j]);
      j++;
   }
  
  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 while loop 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: ";
   while(i<9){
      cout<<arr[i]<<" ";
      i++;
   }
   
   while(j<9){
      sum += arr[j];
      j++;
   }
   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: ");
   while(i<9){
      printf("%d ",arr[i]);
      i++;
   }
   while(j<9){
      sum += arr[j];
      j++;
   }
   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 while loop 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: ";
   while(i<9){
      cout<<arr[i]<<" ";
      i++;
   }
   
   while(j<9){
     if(arr[j] % 2 == 0) sum += arr[j];
     j++;
   }
  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: ");
   while(i<9){
      printf("%d ",arr[i]);
      i++;
   }
   while(j<9){
      if(arr[j] % 2 == 0) sum += arr[j];
      j++;
   }
   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 while loop 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: ";
   while(i<9){
      cout<<arr[i]<<" ";
      i++;
   }
   
   while(j<9){
     if(arr[j] % 2 == 1) sum += arr[j];
     j++;
   }
  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: ");
   while(i<9){
      printf("%d ",arr[i]);
      i++;
   }
   while(j<9){
      if(arr[j] % 2 == 1) sum += arr[j];
      j++;
   }
   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 while loop C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int n, i = 1;
  cout<<"Enter number n: ";
  cin>>n;
  while(i<= 9){
    cout<<n<<" x " <<i<<" = "<<n*i<<"\n";
    i++;
  }
  
  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);
  while(i<= 9){
    printf("%d x %d = %d\n",n,i,n*i);
    i++;
  }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 14 PNG

Example 15: Import and export elements in an array with while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;
 
int main()
{
    int n, i = 0, j = 0;
    cout<<"Enter the number of elements in the array: ";
    cin>>n;
    int numbers[n];
    while(i < n){
        cout << "Enter elements, i = : ";
        cin >> numbers[i];
      i++;
    }
    cout<<"\nElements in array: ";
    while (j < n){
        cout  << numbers[j] << " ";
      j++;
    }

    cout<<"\n-------------------------------\n";
    cout<<"This program is posted at learnnc.com";
    return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int n, i = 0, j = 0;
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);
    int numbers[n];
    while (i < n){
        printf("Enter elements, i = : ");
        scanf("%d",&numbers[i]);
      i++;
    }
    printf("\nElements in array: ");
    while (j < n){
        printf("%d ",numbers[j]);
      j++;
    }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 15 PNG

Example 16: Sort array ascending with while loop in C / C++

C++ programming:

#include <iostream>
using namespace std;

void sort_numbers_ascending(int number[], int count)
{
   int temp, i, j, k;
   for (j = 0; j < count; ++j)
   {
      for (k = j + 1; k < count; ++k)
      {
         if (number[j] > number[k])
         {
            temp = number[j];
            number[j] = number[k];
            number[k] = temp;
         }
      }
   }
   cout<<"\n\nArray after sorting: ";
   for (i = 0; i < count; ++i)
      cout<<" "<< number[i];
}

int main()
{
    int n, i = 0, j = 0, k = 0;
    int sum = 0;
    cout<<"Enter the number of elements in the array: ";
    cin>>n;
    int numbers[n];
    while(i < n){
        cout << "Enter elements, i = : ";
        cin >> numbers[i];
      i++;
    }
    cout<<"\nElements in array: ";
    while (j < n){
        cout  << numbers[j] << " ";
        j++;
    }
    sort_numbers_ascending(numbers,n);
  
    cout<<"\n-------------------------------\n";
    cout<<"This program is posted at learnnc.com";
    return 0;
}

C programming:

#include <stdio.h>

void sort_numbers_ascending(int number[], int count)
{
   int temp, i, j, k;
   for (j = 0; j < count; ++j)
   {
      for (k = j + 1; k < count; ++k)
      {
         if (number[j] > number[k])
         {
            temp = number[j];
            number[j] = number[k];
            number[k] = temp;
         }
      }
   }
   printf("\n\nArray after sorting: ");
   for (i = 0; i < count; ++i)
      printf("%d ",number[i]);
}

int main(void) {
  int n, i = 0, j = 0, k = 0;
    int sum = 0;
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);
    int numbers[n];
    while (i < n){
        printf("Enter elements, i = : ");
        scanf("%d",&numbers[i]);
      i++;
    }
    printf("\nElements in array: ");
    while (j < n){
        printf("%d ",numbers[j]);
      j++;
    }

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

Output:

exam 16 PNG

Example 17: Sort array descending with while loop in C/C++

C++ programming:

#include <iostream>
using namespace std;

void sort_numbers_descending(int number[], int count)
{
   int temp, i, j, k;
   for (j = 0; j < count; ++j)
   {
      for (k = j + 1; k < count; ++k)
      {
         if (number[j] < number[k])
         {
            temp = number[j];
            number[j] = number[k];
            number[k] = temp;
         }
      }
   }
   cout<<"\n\nArray after sorting: ";
   for (i = 0; i < count; ++i)
      cout<<" "<< number[i];
}

int main()
{
    int n, i = 0, j = 0, k = 0;
    int sum = 0;
    cout<<"Enter the number of elements in the array: ";
    cin>>n;
    int numbers[n];
    while(i < n){
        cout << "Enter elements, i = : ";
        cin >> numbers[i];
      i++;
    }
    cout<<"\nElements in array: ";
    while (j < n){
        cout  << numbers[j] << " ";
        j++;
    }
    sort_numbers_descending(numbers,n);
  
    cout<<"\n-------------------------------\n";
    cout<<"This program is posted at learnnc.com";
    return 0;
}

C programming:

#include <stdio.h>

void sort_numbers_descending(int number[], int count)
{
   int temp, i, j, k;
   for (j = 0; j < count; ++j)
   {
      for (k = j + 1; k < count; ++k)
      {
         if (number[j] < number[k])
         {
            temp = number[j];
            number[j] = number[k];
            number[k] = temp;
         }
      }
   }
   printf("\n\nArray after sorting: ");
   for (i = 0; i < count; ++i)
      printf("%d ",number[i]);
}

int main(void) {
  int n, i = 0, j = 0, k = 0;
    int sum = 0;
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);
    int numbers[n];
    while (i < n){
        printf("Enter elements, i = : ");
        scanf("%d",&numbers[i]);
      i++;
    }
    printf("\nElements in array: ");
    while (j < n){
        printf("%d ",numbers[j]);
      j++;
    }

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

Output:

exam 17 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

Do while loop in C / C++ with examples

Do while loop in C / C++ with examples

For loop in C / C++ with examples

For loop in C / C++ with examples

Top