ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Program in C To Find Reverse a Given Integer Number using do while loop

Updated on September 25, 2013

How it works

This is a simple program in which a user can enter an Integer value and the program will reverse the number (if user enters 123 the program will display it as 321).

when user give a value it will be stored in a variable and the program will store each digit in an array and will display it in the reverse order. It is implemented using a do while loop

Source Code

#include<stdio.h>
void main()
{
	int var,i=0,j,ar[10];
	printf("Enter the number :");
	scanf("%d",&var);
	
	do
	{
		ar[i]=var%10;
		var=var/10;
		i++;
	}while(var>0);
	
	printf("\nThe reversed number is :");
	
	for(j=0;j<i;j++)
		printf("%d",ar[j]);
	
}
The program was tested on Sourcelair
The program was tested on Sourcelair | Source

Further explanation

  • In the line 6 an integer is stored in the variable var (let it be 456)
  • In the do loop at first the number 6 ( 456%10=6 ) is stored in ar[0].
  • In line 11 var is replaced whith 45 ( 456/10=46 since the variable is of int type)
  • i is incremented in line 12
  • the do while loop continues till the value of var becomes 0 or less than 0
  • ie by the end of the loop ar[0]=6 ar[1]=5 and ar[2]=4
  • in the last step the array ar is printed using a for loop which gives an output 654



If you find this hub interesting you can follow this hub and you can follow the author on twitter @AthulExe. And if you have any doubts or contribution to this hub do write it down in the comments column.

Was this program easy to under stand ?

See results

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article