Search C Program | nimishsoft@gmail.com

Reverse Number


/* Reverse a Number using while loop */
#include <stdio.h>
int main()
{
 int n, r = 0;
 
 printf("Please enter a number: ");
 scanf("%d",&n);
 
 while (n > 0)
 {
  r = r * 10;
  r = r + n%10;
  n = n/10;
 }
 
 printf("Reversed number is = %d: ", r);
 return 0;
}

--------------------------------------------------------------

/* Reverse a Number using for loop */
#include <stdio.h>
int main()
{
 int n, r;
 
 printf("Please enter a number: ");
 scanf("%d",&n);
 
 
 for(r=0;n>0;n=n/10)
 {
  r = r * 10;
  r = r + n%10;
 }
 
 printf("Reversed number is = %d: ", r);
 return 0;
}

Related Links:
Palindrome Number
http://cbasicprogram.blogspot.in/2012/05/palindrome-number.html

Sum of digits
http://cbasicprogram.blogspot.in/2012/05/sum-of-digits.html

2 comments:

  1. //Easy way
    #include
    int main()
    {
    int n,s,k,i;
    printf("Enter a number: ");
    scanf("%d",&n);

    for(i=1;i<=4;i++)
    {
    s=n%10;
    k=n/10;
    n=k;
    printf("%d ",s);

    }
    return 0;
    }

    ReplyDelete