Write program which count sum of numbers divided by 4 [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 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

Related

make dictionary that will loop every 9 numbers. 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 10 months ago.
Improve this question
Im new to programming and sorry if I cant explain properly. I am trying to iterate through a list that has items in it in multiples of 9. So the list can have 9,18,27.. items.
I have the code working for it to read when there is 9 using this dictionary. But I would like it work for any amount in multiples of 9.
var alphabets = new Dictionary<int, string>()
{
{1,"A2"},{2,"B2"},{3,"C2"},{4"D2"},
{5,"E2"},{6,"F2"}, {7,"G2"},{8,"H2"},
{9,"I2"}
};
So for example if there was 18 items it would like this dictionary to have this function.
var alphabets2 = new Dictionary<int, string>()
{
{1,"A2"},{2,"B2"},{3,"C2"},{4"D2"},
{5,"E2"},{6,"F2"}, {7,"G2"},{8,"H2"},
{9,"I2"},
{10,"A3"},{11,"B3"},{12,"C3"},{13"D3"},
{14,"E3"},{15,"F3"}, {16,"G3"},{17,"H3"},
{18,"I3"}
};
Thank you
As #DiplomacyNotWar commented, it sounds as if you need to input int value to convert to a correlating string value which is uniformly based on multiples of 9. If this is the case, I agree with #DiplomacyNotWar that you don't need to store anything but create a function to output the needed string value based on an int value. Here is a function that will output the pattern in your examples.
// value should be 0
string ConvertIntToSpecificString(int value)
{
// this will give you an int value 0-8
var modValue = (value - 1) % 9;
// The unicode for 'A' is 65
var firstCharValue = (char)(65 + modValue);
// This will return a whole number giving the iteration count. FE: 19 / 9 = 2
// Adding 2 to fit the pattern stated in the examples.
var secondValue = ( value / 9 ) + 2 ;
return $"{firstCharValue}{secondValue}";
}

How do increment an integer that is part of a string at each iteration of loop [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 2 years ago.
Improve this question
i have a string like this S02E01 and now i want to run it through a loop like 20 times but at each iteration i want to increment the number after 'E' so that we have for example S02E01,S02E02,S02E03 ..S0210,...S02E20. Please help me.
Its really simple by using some string functions: Substring, PadLeft or ToString
string mystring = "S02E00";
var template = mystring.Substring(0, mystring.Length - 2);//template = "S02E"
for(int i = 0;i <= 20; i++)
{
var result = template + i.ToString("00");
Console.WriteLine(result);
}
you could use PadLeft too => var result = template + i.ToString().PadLeft(2, '0');

Hi there I want to know how this kind of conditional statement means? if (i == 0) lowest = highest = input; [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 2 years ago.
Improve this question
Hi there I want to know how this kind of conditional statement means?
if (i == 0)
{
lowest = highest = input;
}
When you come across questions like this, i would recommend that you test it yourself. For instance, this can be tested in a simple console app to see what happens
class Program
{
public static void Main(string[] args)
{
int lowest = 1;
int input = 2;
int highest = 3;
Console.WriteLine("Before equality operator");
Console.WriteLine($"lowest: {lowest}");
Console.WriteLine($"highest: {highest}");
Console.WriteLine($"input: {input}");
int i = 0;
if (i == 0) lowest = highest = input;
Console.WriteLine("After equality operator");
Console.WriteLine($"lowest: {lowest}");
Console.WriteLine($"highest: {highest}");
Console.WriteLine($"input: {input}");
}
}
Output:
Before equality operator
lowest: 1
highest: 3
input: 2
After equality operator
lowest: 2
highest: 2
input: 2

int permutations in C# [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 6 years ago.
Improve this question
I want to implement a method with the following signature:
public int Max(int number){ }
The largest number that can be created by a digits of a given number is obtained by ordering the digits from largest to smallest. See the following code for a possible implementation.
public int Max(int number)
{
var numberAsCharArray = number.ToString().OrderByDescending(c => c).ToArray();
var largestNumberAsString = new string(numberAsCharArray);
return Int32.Parse(str);
}
Maybe not most efficient way is to cast to string and order desc
var result = int.Parse(String.Join("", digit.ToString().OrderByDescending(x => x)))

C# Console split number to digit and do math [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 7 years ago.
Improve this question
I have a user input, lets say 3 digit number. I want to split the numbers and add them or multiply - it doesn't matter.
This is what I tried to do:
Console.WriteLine("enter a number: ");
int userInput = Convert.ToInt32(Console.ReadLine());
string temp = userInput.ToString();
int x = 0;
foreach(char c in temp)
{
x += c;
Console.WriteLine(x);
}
Console.ReadLine();
I'm not allowed to use %
Any suggestion?!
If you want to calculate sum of digits of a number, you can read characters of the string and convert them to numbers and then use them:
string number = Console.ReadLine();
var sum = number.ToArray().Sum(x => int.Parse(x.ToString()));
Console.WriteLine(sum);
Don't forget to add using System.Linq;
Also as an option without using Linq:
string number = Console.ReadLine();
int sum = 0;
foreach (char c in number)
{
sum += int.Parse(c.ToString());
}
Console.WriteLine(sum);

Categories

Resources