1

I am new to C# and I am learning to reverse integers (numbers). I have to use forloop and an array to complete the code but i really have no idea how to use those.
For example. If the input is: 1 2 3 4 5 6
Results should be : 6 5 4 3 2 1

Can someone please help me. Thank you :)

Edit: I am really sorry guys, I forgot to add the code. Here it is. Thanks again

static void Main(string[] args)
{
    Console.WriteLine("Enter 6 Numbers");
    int numb = int.Parse(Console.ReadLine());
    int reverse = 0;
    while (numb > 0)
    {
        int rem = numb % 10;
        reverse = (reverse * 10) + rem;
        numb = numb / 10;
    }

    Console.WriteLine("The reverse is = {0}", reverse);
    Console.ReadLine();
}

Edit

Here a string is taken as input and need to be reversed. Just due to the input is taken as numbers only so this is confusing little bit.

2
  • 1
    Are you trying to reverse integers in an array, or reverse digits in an integer? It seems to me that your words say one thing, yet your code says another entirely. Commented Aug 14, 2014 at 1:22
  • Unclear how title related to sample - there is arrays and no "integers"... You may need to look at existing question about reading array of integers first... To match title your code sample should show one method with signature: int[] Reverse(int[] source) or maybe void ReverseInPlace(int[] array). Commented Aug 14, 2014 at 1:43

7 Answers 7

4

Unless you need to use a loop, a much simpler solution is to use Array.Reverse:

int[] array = { 1, 2, 3, 4, 5, 6 };
Array.Reverse(array);

You could use a for loop and iterate half of the array, switching the positions with the ones on the opposite side of the array.

int holder = 0;
int size = array.Length;
for (int i = 0; i < size / 2; ++i)
{
    holder = array[i];
    array[i] = array[size - 1 - i];
    array[size - 1 - i] = holder;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a simple logic to Reverse Numbers/Integers using Loops and using Array.Reverse Functionality

Example : Input 12345 will give us an output of 54321

namespace PurushLogics
{
    class Purush_ReverseInteger
    {
        static void Main()
        {
            //Reverse a Number  
            int intstr;
            string intreverse = "";
            int intLength = 0;
            int? intj = null;
            Console.WriteLine("Enter a Number"); // Entering "12345" has input will return "54321" as output

            intstr = int.Parse(Console.ReadLine());//Getting integer from Console  

            char[] inta = intstr.ToString().ToCharArray();


            intLength = inta.Length - 1;
            for (int c = intLength; c >= 0; c--)
            {
                intreverse = intreverse + inta[c].ToString();
                intj = int.Parse(intreverse);
            }

            Console.WriteLine("Reverse Number is \"{0}\"", intj);//Displaying the reverse integer  
            Console.ReadLine();

            //Reverse integer using Array.Reverse
            Array.Reverse(inta);
            foreach (char reverse in inta)
            {
                Console.Write(reverse);
            }
            Console.ReadLine();
        }
    }
}

Comments

0
static void Main(string[] args)
        {
            int[] numbers = new int[6];
            for (int i = 0; i < 6; i++)
            {
                Console.Write("Number {0} : ", i + 1);
                numbers[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine();
            for (int i = 5; i >= 0; i--)
            {
                Console.Write("{0} ", numbers[i]);

            }

            Console.ReadLine();
        }

Comments

-1
   class Program
{
    static void Main(string[] args)
    {
        int[] array = new int[] { 1, 2, 3, 4, 5, 6,7};
        int temp = 0;
        for (int i = array.Length - 1; i >= (((array.Length - 1) % 2==0)? (((array.Length - 1) / 2)):((array.Length - 1) / 2)+1) ; i--)
        {
            temp = array[(array.Length - 1) - i];
            array[(array.Length - 1) - i] = array[i];
            array[i] = temp;
        }

        foreach (var p in array)
        {
            Console.WriteLine(p);
        }
        Console.ReadLine();
    }
}

Comments

-1
int[] numbers=new int[]{1,2,3,4,5};
    int[] reverse=new int[numbers.Length];
    int j=numbers.Length-1;
    for(int i=0;i<numbers.Length;i++)
    {

        reverse[i]=numbers[j];
        Console.WriteLine(reverse[i]);
        j--;
    }

1 Comment

Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
-1
    public static int[] Test( params Int32[] nums)
    {
        int[] ar = new int[nums.Length];
        var count = 0;
        for (int i = nums.Length-1; i >= 0 ; i--)
        {
            ar[count] = nums[i];
            count++;
        }
        return ar;
    }

1 Comment

Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
-1
This is Palindrom or not  string tmp=""; string values = "";Console.Write("Please Enter Number : "); string strnumber = Convert.ToString(Console.ReadLine());  tmp = strnumber; if (strnumber.Length > 0)  { foreach (var res in strnumber.Reverse())  { values += res; } }  if (values == strnumber)  { Console.WriteLine("Number is Palindrome."); }  else { Console.WriteLine("Number is not Palindrome")

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.