Printing numbers 1 to 100 C# [closed] - c#

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 4 months ago.
Improve this question
I am new here and in C# and I dont't know how to figure out that example..Printing numbers 1 to 100 that way "100, 1, 2, 99, 98, 3, 4, 97,....52, 49, 50, 51". I have a course work and should write it befoure 25.10..Thank you all!

for (int i = 1; i < 51; i+=2)
{
Console.WriteLine(101-i);
Console.WriteLine(i);
Console.WriteLine(1+i);
Console.WriteLine(100-i);
}
EDIT: just i want to add that i use (1+i) // (100-i) because if I use i-- or i++ inside the writeline, it will execute the ++ or -- after the print message

By using a lower and a upper variable and a while-loop, you can do it stright forward without any calculations:
int lower = 1, upper = 100;
while (lower < upper) {
Console.WriteLine(upper--);
Console.WriteLine(lower++);
Console.WriteLine(lower++);
Console.WriteLine(upper--);
}

Related

Write program which count sum of numbers divided by 4 [closed]

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
I need to write a program which count sum of numbers divided by 4. I'm really beginner in C#.
Could you lead me to solve this problem?
I write something like this but I don't know what next and there is one problem because I put only two numbers but numbers could be a lot of.
Console.WriteLine("number a");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("number b");
int b = int.Parse(Console.ReadLine());
int sum = a + b;
int result = sum / 4 ;
Console.WriteLine ( "result is="+ result ) ;
use
double sum = Console.ReadLine().Split().Sum(x => int.Parse(x)) / 4.0;
Than you can enter any space separated array like
1 3 5 7

Heliocentic in c# [closed]

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
So perhaps, you've generated some fancy text, and you're content that you can now copy and paste your fancy text in the comments section of funny cat videos, but perhaps you're wondering how it's even possible to change the font of your text? Is it some sort of hack? Are you copying and pasting an actual font?
days++;
earth++;
mars++;
earth %= 365;
mars %= 687;
}
Console.Write(days);
Console.Write("\n");
Console.ReadLine();
n++;
}
}
}
It appears you're only missing a way to process the sample input. It sounds like it's coming in "lines", but that's not really defined, so in this example I'm just using an array. I'm ignoring the fact that the first item specifies the number of testcases, since in this implementation the number is calculated.
Basically I'm just walking through the array, grabbing two items at a time (skipping the first one), and using those values for earth and mars. The rest is mostly your code (except some changes to outputting to the console):
int[] sampleInput = {5, 0, 0, 364, 686, 360, 682, 0, 1, 1, 0};
int n = 1;
for (var i = 1; i < sampleInput.Length - 1; i += 2)
{
int earth = sampleInput[i];
int mars = sampleInput[i + 1];
int days = 0;
while (earth != 0 || mars != 0)
{
days++;
earth++;
mars++;
earth %= 365;
mars %= 687;
}
Console.WriteLine($"Case {n}: {days}");
n++;
}
Console.ReadLine();
Output

Sort C# array while adding numbers [closed]

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 3 years ago.
Improve this question
I have this array and I want that every time the user inputs a number, the program immediately makes the sort. How can I do that? For example, if I input 6 and then 3, immediately takes the 3 to the first position. And then if I put 2, immediately take it to the first position and sort the others (2,3,6). Then if I put 1, takes it to the first position, sorting the others(1,2,3,6) and so on
int[] a = new int[5] ;
for (int i = 0; i < a.Length; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
As #juharr suggest you can use System.Collections.Generic.SortedSet:
static void Main(string[] args)
{
var list = new SortedSet<int>();
int count = 5;
for ( int i = 0; i < count; i++ )
list.Add(Convert.ToInt32(Console.ReadLine()));
foreach (int item in list)
Console.WriteLine(item);
Console.ReadKey();
}
add the numbers to a list and call list.sort()
List<int> list = new List<int> { 6, 3 };
list.Sort();

How to write arrays to tab delimited text file? [closed]

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
I have two arrays -
int[] array1 = { 1, 2};
int[] array2 = { 6, 7};
I want to write them in text file delimited by tab.
Final Result -
1 6
2 7
Please suggest how to do this?
Assuming both arrays are of the same length:
File.WriteAllLines(filename,
Enumerable.Range(0, arr1.Length)
.Select(i => string.Format("{0}\t{1}", arr1[i], arr2[i]));
You could do something like this:
public void writeFile(int[] array){
using (streamWriter sw = new StreamWriter(filename, true){
for(int i = 0; i < array.Length; i++){
sw.WriteLine(array[i] + "\t");
}
}
}
Then you would just call the write method like this:
writeFile(array1);
writeFile(array2);

Making an array ints and replace the order of number I write [closed]

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 8 years ago.
Improve this question
What I try to do, is for example if I write five numbers 1,2,3,4,5 after this it should print 5,4,3,2,1
int[] numbers = new int[5];
for (int i = 0; i < numbers.Length ; i++)
{
Console.Write("");
numbers[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < numbers.Length; i++)
{
}
If you need just to print them in a reverse order, but you want to keep them stored as you inserted them, change the second loop like this:
for (int i=numbers.Length-1; i >= 0; i--){
Console.WriteLine(numbers[i]);
}
You can print the array with a reverse loop as in the previous answer, or you can do that in a single line:
Console.WriteLine(string.Join(",", numbers.Reverse()));

Categories

Resources