Skip to content

Commit 4620949

Browse files
Add Palindromic Number in C# (#4513)
1 parent e0988a3 commit 4620949

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
public class PalindromicNumber
3+
{
4+
public static void Main(string[] args)
5+
{
6+
7+
try
8+
{
9+
long verifyInput = long.Parse(args[0]);
10+
11+
if (verifyInput >= 0)
12+
{
13+
Console.WriteLine(palindrome(args[0]));
14+
}
15+
else
16+
{
17+
Console.WriteLine("Usage: please input a non-negative integer");
18+
}
19+
20+
}
21+
catch
22+
{
23+
Console.WriteLine("Usage: please input a non-negative integer");
24+
}
25+
26+
}
27+
28+
public static string palindrome(string numString)
29+
{
30+
char[] digits = numString.ToCharArray();
31+
32+
int backCount = digits.Length - 1;
33+
34+
for (int i = 0; i < digits.Length; i++)
35+
{
36+
if (digits[i] != digits[backCount])
37+
{
38+
return "false";
39+
}
40+
else
41+
{
42+
backCount--;
43+
}
44+
45+
}
46+
47+
return "true";
48+
49+
}
50+
51+
}

0 commit comments

Comments
 (0)