Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
My list is:
var list = new List<int>()
{
1, 2, 3, 4
};
Without doing
int sum = list[0]*list[0] + list[1]*list[1] + list[2]*list[2] + list[3]*list[3]
var result = list.Sum(o => o * o);
While the other answers work as well, you should learn how to use a loop. It is a fundamental building block of programming. For example, we can use a foreach loop to iterate over each element in the list. see:
int sum = 0;
foreach (int val in list)
sum += val * val;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Currently, I am trying to convert coffee script from there https://github.com/slang800/instagram-id-to-url-segment to C# but I am unable to convert because I have less exp in coffee script. What I need to do is simple but I am unable to guess the algo. If id is 1038059720608660215 then value should be 5n7dDmhTr3. The complete code of algo is available at github.
Try this
static void Main(string[] args)
{
string[] convert = {
"A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z","a","b","c","d",
"e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x",
"y","z","0","1","2","3","4","5","6","7",
"8","9","-","_"
};
Int64 input = 1038059720608660215;
string output = "";
for (int i = 9; i >= 0; i--)
{
long digit = (input >> (6 * i)) & 0x3F;
output += convert[digit];
}
Console.WriteLine(output);
Console.ReadLine();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this code that helps me getting information from texts files , the problem is that i cant get to add all the strings founds in the texts files into one int.
in the first Entrada.txt i have a 23 and in the second one i have 45.
How can i add those 2 numbers together?
foreach(var impressora in ListaImp)
{
var Entrada = File.ReadAllText(impressora + #"\Entrada.txt");
MessageBox.Show("Entrada : " + Entrada);
}
Output: 2345.
I want it to be 23 + 45 = 68
You can do this:
int sub = 0;
foreach(var impressora in ListaImp)
{
var Entrada = File.ReadAllText(impressora + #"\Entrada.txt");
sub += int.Parse(Entrada);
}
MessageBox.Show(sub.ToString());
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I enter the value in textbox:
1,2,3,4,5,6,7...
How to into to int array or list and show value display ???
And stop when a user enters the wrong consecutive 3 times or total error of 5
//textbox is your textbox controls' name
string[] content = textbox.Text.Split(',');
//use linq convert stringp[] to int[]
int[] nums = (from num in content
select int.Parse(num)).ToArray();
I don't understand the last part of your question (the bit about stopping when a user enters something wrong... I guess you'd have to define "wrong" in this context). Regardless, the following code should take values entered into a text box (let's call it txtNumbers) and put it in an array of ints.
string[] splitContent = txtNumbers.Text.Split(new char[] { ',' });
int[] nums = new int[splitContent.Length];
for (int i = 0; i < splitContent.Length; i++)
{
nums[i] = Int32.Parse(splitContent[i]);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Is it possible in c# to do math with file names that contain numbers? (i.e. change
file name : "c:\123.img" to "c:\127.img"?
This should be enough to get you started:
var files = Directory.GetFiles(path);
foreach (var f in files)
{
var info = new FileInfo(f);
var name = info.Name.Split('.')[0];
var extension = info.Name.Split('.')[1];
int i;
if (int.TryParse(name, out i))
{
File.Move(info.FullName, string.Format(#"{0}\{1}.{2}", path, i + 5, extension));
}
}