Write a C# Sharp program to reverse the digits of a given signed 32-bit integer
- برمجة سي شارب
- برمجة
- 2021-05-31
- ahmadghneem
الأجوبة
using System;
namespace exercises {
class Program {
static void Main(string[] args) {
int n;
n = 123456;
Console.WriteLine("Original Integer value: " + n);
Console.WriteLine("Reverse the digits of the said signed integer value:");
Console.WriteLine(reverse_integer(n));
n = -7654;
Console.WriteLine("Original Integer value: " + n);
Console.WriteLine("Reverse the digits of the said signed integer value:");
Console.WriteLine(reverse_integer(n));
n = 100;
Console.WriteLine("Original Integer value: " + n);
Console.WriteLine("Reverse the digits of the said signed integer value:");
Console.WriteLine(reverse_integer(n));
}
public static int reverse_integer(int x)
{
var result = 0;
var max_val = int.MaxValue / 10;
var min_val = int.MinValue / 10;
for (; x != 0; x /= 10)
{
if (result > max_val || result < min_val)
{
return 0;
}
result = result * 10 + x % 10;
}
return result;
}
}
}
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال