The do-while Loop - C Statement

C examples for Statement:do while

Introduction

The general representation of the do-while loop is:

do
{
  /* Statements for the loop body */
}
while(expression);

The following program reverses the digits of a positive number.

Demo Code

#include <stdio.h>
int main(void)
{
  unsigned int number = 0;                 // The number to be reversed
  unsigned int rebmun = 0;                 // The reversed number
  unsigned int temp = 0;                   // Working storage

  printf("\nEnter a positive integer: ");
  scanf(" %u", &number);

  temp = number;                           // Copy to working storage

  // Reverse the number stored in temp
  do// w  ww .j  a  v a 2s  . c o m
  {
    rebmun = 10*rebmun + temp % 10;        // Add rightmost digit of temp to rebmun
    temp = temp/10;                        // and remove it from temp
  } while(temp);                           // Continue as long as temp is not 0

  printf("\nThe number %u reversed is  %u rebmun ehT\n", number, rebmun );
  return 0;
}

Result


Related Tutorials