In this article we are going to learn how to reverse a integer or numeric number in c#.
With help of for loop i have shown the program to reverse the given number.
Don't ignore comments.
int temp = 0; //took temporary variable
int val =13298765; //passed integer value to the integer type variable val
for (int i=0; val>0 ;i++) // now increment of i
{
temp =val % 10; // storing all the single digit in temp variable
val =val/10; //taking remaining value after mod
Response.Write(temp);
}
In this program we don't need to take length because it won't works.
it works at time of sring and array.
mod [%] will give you last value on number.
and / will store remaining one in val variable.
output :-56789231
another example
/int count = 34;
int temp = 0;
while (count > 0)
{
temp = count % 10;
count = count / 10;
Response.Write(temp);
With help of for loop i have shown the program to reverse the given number.
Don't ignore comments.
int temp = 0; //took temporary variable
int val =13298765; //passed integer value to the integer type variable val
for (int i=0; val>0 ;i++) // now increment of i
{
temp =val % 10; // storing all the single digit in temp variable
val =val/10; //taking remaining value after mod
Response.Write(temp);
}
In this program we don't need to take length because it won't works.
it works at time of sring and array.
mod [%] will give you last value on number.
and / will store remaining one in val variable.
output :-56789231
another example
/int count = 34;
int temp = 0;
while (count > 0)
{
temp = count % 10;
count = count / 10;
Response.Write(temp);
0 comments: