Lovepreet Brar
Lovepreet Brar

Reputation: 25

How to Reverse integers using 'for loop' and an 'array' in C#

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.

Upvotes: 0

Views: 18304

Answers (7)

Vignesh J
Vignesh J

Reputation: 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")

Upvotes: -1

Rahul
Rahul

Reputation: 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;
    }

Upvotes: -1

Richard Kedang
Richard Kedang

Reputation: 1

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();
        }

Upvotes: 0

user11956714
user11956714

Reputation: 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--;
    }

Upvotes: -1

Hash_S
Hash_S

Reputation: 79

   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();
    }
}

Upvotes: -1

Purushoth
Purushoth

Reputation: 69

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();
        }
    }
}

Upvotes: 0

Cyral
Cyral

Reputation: 14153

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;
}

Upvotes: 4

Related Questions