Write a C Program To Get Reverse A Number
C program to reverse a number and to print it on the screen. For example, if the input is 456, the output will be 654. In the program, we use the modulus operator (%) to obtain digits of the number. To invert the number write its digits from right to left.
C program to find reverse of a number
#include<stdio.h>
#include<conio.h>
void main()
{
int n, r = 0;
{
int n, r = 0;
printf("Enter a number to reverse\n");
scanf("%d", &n);
scanf("%d", &n);
while (n != 0)
{
r = r * 10;
r = r + n%10;
n = n/10;
}
{
r = r * 10;
r = r + n%10;
n = n/10;
}
printf("Reverse of the number = %d\n", r);
getch();
}
}
Reverse number C program using recursion
#include<stdio.h>
#include<conio.h>
long reverse(long);
void main()
{
long n, r;
scanf("%ld", &n);
r = reverse(n);
printf("%ld\n", r);
getch();
}
void main()
{
long n, r;
scanf("%ld", &n);
r = reverse(n);
printf("%ld\n", r);
getch();
}
long reverse(long n) {
static long r = 0;
if (n == 0)
return 0;
r = r * 10;
r = r + n % 10;
reverse(n/10);
return r;
}
static long r = 0;
if (n == 0)
return 0;
r = r * 10;
r = r + n % 10;
reverse(n/10);
return r;
}
No comments:
Post a Comment