C# How to display all elements of array as a single number?

I have an int array .

int[] numbers = new int[5] {1, 2, 3, 4, 5};

How can all the elements of this array displayed as a single number from last index to 0th index.In this case , it has to be 54321.

Print numbers without giving any spaces.

1 Like

public IntoSingleNumber()
{
double d=0;
double mul =10;

		int [] num = {1,2,3,4,5,6,7};
		int i=num.Length-1;
		while (i>=0) {
			d=d+num[i];
			d=d*10;
			i--;
		}
		Console.WriteLine(d/10);
	}

double m=0;
int i=numbers.length-1;
while(i>=0)
{ m=m+numbers[i];
m=m*10;
i–;
}
print m;

Here’s an alternative
Console.WriteLine(string.Join("\n", myArrayOfObjects));

use Array.Reverse function.

ex.:

        int[] numbers = new int[5] { 1, 2, 3, 4, 5 };
        Array.Reverse(numbers);
        Console.WriteLine(string.Join("", numbers));

o/p: 54321

int [5]={1,2,3,4,5};

for(i=0;i<5;i++)

printf("%d",int[i]);

LA BOUCLE FOR IS GOOD

Firstly, I will tell you there are many ways to do so.

One is, as in one of the answer above, converting array to number by-

while (i>-1)
{
d=d+num[i];
d=d*10;
i–;
}

However, in case the number is big (lets say, 60 digit number) , then this will fail due to overflow.

The most optimum method is, either convert it to string (especially if problem suggests something string related needs to be done) OR EVEN BETTER, print without space (if you just wanna print the number)

Code-

for(i=n-1;i>-1;i--)
{
System.out.print (a[i]);/*Sorry, its java. I don't know C#. Just wanted to make my algo clear, that's all :) */
}

I tried doing this using Linq as below and getting expected output.

int[] numArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int element = numArray.Length - 1;
numArray.ToList().ForEach(delegate { Console.Write(numArray[element--]); });

Thanks

import java.util.List;

public class two {
public static void main (String[] args)
{
int a[]={5,4,3,2,1};
List list=new ArrayList<>();

for(int i=0;i<a.length;i++)
{
	list.add(a[i]);
	
}


String res="";
for(int i=list.size()-1;i>=0;i--){
	String x=list.get(i).toString();
	res+=x;
}
System.out.println(res);

}
}

I think most of the above answers is to the point, but still TRY the following:

#include<stdio.h>
int main() {
 int a[]={1,2,3,4,5};
 for(int i=4; i>=0; i--)
 {
  printf("%d", a[i]);
 }
 return 0;
}

Here we did what others had already suggested, printing array n reverse order without providing new line character and so…
upvote if you found this helpful…

a=[1,2,3,4,5]
for i in range(len(a)):
d=a[i-1]
d=d*10
print d

help to complete my assignment
In this game, the rocky grid has K layers and each layer has N gems with hardness value ranging from 1 to 9. If a stone in any layer is broken by hammering, then all the corresponding gems of all layers will automatically break by magic.
You can start breaking gems from any layer initially and can switch to any other layers but once switched you will not be able to return back to previous layer. There is a dragon behind the grid and his life is associated with each layer of grid, so you need to break atleast one stone from each layer to kill the dragon.

Note:

  1. If you break a stone on ith
    position on layer a, then the gems on ith position of all the layers will be broken.
  2. You have to break all the gems of all the layers, either magically or by breaking the gems.
  3. If you are on layer a and you broke stone on the ith postion, and you are switching to layer b you can’t move to ith position again in layer b. As all the gems lying on the ith position on all the layers are already broken.
  4. You have to break at least one stone in each of the layer.

Energy required to break the stone of hardness say ‘a’ is ‘a’ calories itself.

Find the minimum energy required to break whole grid and killing the dragon.

Input:

Given N number of gems in each layer and K number of layers of grid.
Next K lines contain N values corresponding to hardness values of each layer.

Output:

Output the minimum energy required.

Constraints:

1 <= N <= 100000
1 <= K <= 5
1 <= hardness value <= 9

SAMPLE INPUT

5 3
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3

SAMPLE OUTPUT

8

Break stone-1,2,3 from layer-1 and then break stone-4 from layer-2 and stone-5 from layer-3

Total Energy required = 1 + 1 + 1 + 2 + 3 = 8