Ticker

6/recent/ticker-posts

How to write a C program to Reversal of Number

How to write a C program to Reversal of Number
How to write a C program to Reversal of Number



#include<stdio.h>
int main()
{
	int n,copy,rev = 0;
	
	printf("Enter the number 		:	"); // If n is 162
	scanf("%d",&n);
	copy = n;
	while(copy>0)
	{
		rev = rev*10;
		rev = rev + (copy%10);
		copy = copy/10;
	}
	printf("The reversed number is 	:%d ",rev);
	
	return 0;
}
	/*
	Entered 162
	1).check 162>0 	yes;
		rev		= 0*10 = 0;
		rev		= 0 + (162%10) = 0+2	= 2;
		copy	= 162/10= 16;
	2).check 16>0 	yes;
		rev		= 2*10	= 20;
		rev		= 20 + (16%10) = 20+6	= 26;
		copy	= 16/10	= 1;
	3).check 1>0 	yes;
		rev		= 26*10 = 260;
		rev		= 260 + (1%10) = 260+1	= 261;
		copy	= 1/10	=0;
	4).check 0>0 no ;
	loop ends

	print rev = 261
	
	
	Syntax
	
	while(condition)
	{
		statement;
	}
	
	*/
    
    

Post a Comment

0 Comments