Resources » Articles/Knowledge Sharing » Computer & Technology

Reverse a number using C programme


Posted Date: 20-Aug-2008  Last Updated:   Category: Computer & Technology    
Author: Member Level: Gold    Points: 1


Are you looking for reverse a number using C programme language, then find C programme project.



#include
main()
{
int num;
void reverse(int num);//The function for reversing is decleared.
clrscr();
printf("enter number");
scanf("%d",&num);
reverse(num);//The function is called.
getch();
}

void reverse(int num)
{
int temp=0;
int rev=0;
temp=num;//The number is stored in to another variable.
while(temp!=0)//We are using that dupicate.
{
rev=rev*10+temp%10;//By using temp%10, we will get the last digit of the number.
temp/=10;//Then the number is get reduced by one.
}
printf("%d",rev);\\After the end of loop reverse of number will stored in rev.
}


Example

consider n=123
we are assigned that number in to 'temp'.
in the loop the condition is "temp!=0".
now temp=123.
the step temp%10 means take it's reminder it will be 3.
we decleared rev=0.
so 'rev=rev*10+temp%10' means 3. [rev*10=0].
Now rev=3.
the next step is temp/10. Now temp=12.
The codition of the loop is true so it will cotinue.
Now temp%10 = 2.
so 'rev=rev*10+temp%10' means 32. [rev*10=30].
It will continue.


Did you like this resource? Share it with your friends and show your love!




Responses to "Reverse a number using C programme"
Author: Raghav    21 Aug 2008Member Level: Gold   Points : 1
Shoba,
you could have given some explanation of what each part of the code does. That is the normal practice so that everyone who reads the code will understand it easily. Kindly give a brief description of the code and what each function does.

Raghav



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.