Shortcut syntax for doing var = var + n? [duplicate] - c#

This question already has answers here:
How to increase value of an integer higher than 1 in one command?
(2 answers)
Closed 2 years ago.
I'd like to increment my var value of n, using a similar structure of the post-increment solution like var++.
The post-increment is like this:
int var = 0;
var++; // var = var + 1
I'd like to increment var of n, with n = 4 for example. What's the correct syntax ?
int var = 0;
var++4; // var = var + 4 but obviously not working;

what you are looking for is actually a Compound assignment with arithmetic operators
int a = 5;
a += 9;
Console.WriteLine(a); // output: 14
a -= 4;
Console.WriteLine(a); // output: 10
a *= 2;
Console.WriteLine(a); // output: 20
a /= 4;
Console.WriteLine(a); // output: 5
a %= 3;
Console.WriteLine(a); // output: 2

Related

How can I calculate numbers (for example) from 1 - 10 with the next number. (1+2+3+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 1 year ago.
Improve this question
Does it have a shorter way than doing something like this:
int[] Summe = new int[6];
Summe[0] = 1;
Summe[1] = 2;
Summe[2] = 3;
Summe[3] = 4;
Summe[4] = 5;
Summe[5] = 6;
Console.WriteLine(Summe[0] + Summe[1] + Summe[2] + Summe[3] + Summe[4] + Summe[5]);
Console.ReadKey();
Using the Enumerable.Sum() method which computes the sum of a sequence of numeric values and Enumerable.Take() which returns a specified number of contiguous elements from the start of a sequence.
Console.WriteLine(Summe.Take(10).Sum());
OR from high school
// sum of integers from 1 to n
int SumNaturalNumbers(int n)
{
return n * (n + 1) / 2;
}
Formula for the general algebraic series starting from a and difference between terms d (Arithmetic progression and series)
sum = n / 2 * (2 * a + (n - 1) * d)
I think using a foreach loop may be helpful here, since you're dealing with an array. Also, you can define your array on one line.
int[] Summe = { 1, 2, 3, 4, 5, 6 };
int total = 0;
foreach (int number in Summe)
{
total += number;
}
Console.WriteLine(total);
Console.ReadKey();
You can simplify this process by simply putting this operation into a while loop.
int i = 0; // tell your program what 'i' should be upon first running the code
while (i < 10) // if 'i' is less than 10, run this block of code. You can change 10 to anything you want
{
Console.WriteLine("i = {0}", i);
i++; // increment
}
This will print each number individually, but you want to calculate the sum of every number, so you could do something like this:
{
public static void Main()
{
int j, sum = 0;
Console.Write("\n\n");
Console.Write("Find the sum of first 10 natural numbers:\n");
Console.Write("----------------------------------------------");
Console.Write("\n\n");
Console.Write("The first 10 natural number are :\n");
for (j = 1; j <= 10; j++)
{
sum = sum + j; // add the previous number to the current one
Console.Write("{0} ",j);
}
Console.Write("\nThe Sum is : {0}\n", sum);
}
}
The above code prints the sum of the first 10 natural numbers. I added some additional lines simply to make the program more legible. Again, you can change the number 10 to whatever number you want.

Factorial function for 100 results 0 in c# even though using long type [duplicate]

This question already has answers here:
I'm using ulong for the factorial of 100, but it still overflows
(2 answers)
Closed 3 years ago.
I have this code to determine factorial of 100. But after fifties it starts giving result of 0. I looked for answers for this problem and the common saying was using long variable. But even though using it, it still says that the answer is 0. Can you tell me where I am making mistake ?
static void Main(string[] args)
{
long c;
int a = 100;
c = a * (a - 1);
for (int i=a-2; i>=1 ; i--)
{
c = c * i;
}
Console.WriteLine(c);
Console.ReadLine();
}`
long type isn't long enough to store the factorial of 100.
Use BigInteger instead:
BigInteger c = new BigInteger(0);
int a = 100;
c = a * (a - 1);
for (int i = a - 2; i >= 1; i--)
{
c = c * i;
}
Console.WriteLine(c);
Console.ReadLine();
In order to use BigInteger you have to add a reference to the System.Numerics assembly to your project. (link)

Change Integer value to Roundoff by the nearest integer [duplicate]

This question already has answers here:
How to round up value C# to the nearest integer?
(10 answers)
Closed 6 years ago.
int a = 4;
int b = 3;
int c = a/b;
Here the c value as 1.33 . now i need to change the value as whole number like 2.
If,
int a =1;
int b = 2;
int c = a/b means the value is 0.5
now i need to change the value as next whole number like 1
Please try this:
int a = 4;
int b = 3;
int c = Convert.ToInt32(Math.Ceiling(a/b));

Converting an integer to an array of digits [duplicate]

This question already has answers here:
Is there an easy way to turn an int into an array of ints of each digit?
(11 answers)
Closed 7 years ago.
I want to know if there is a way in C# to convert an integer to an array of digits so that I can perform (Mathematical) operations on each digit alone.
Example: I need the user to input an integer i.e 123, 456
then the program creates two arrays of three elements {1,2,3}, {4,5,6}.
Off the top of my head:
int i = 123;
var digits = i.ToString().Select(t=>int.Parse(t.ToString())).ToArray();
You could create such array (or List) avoiding string operations as follows:
int x = 123;
List<int> digits = new List<int>();
while(x > 0)
{
int digit;
x = Math.DivRem(x, 10, out digit);
digits.Add(digit);
}
digits.Reverse();
Alternative without using the List and the List.Reverse:
int x = 456;
int[] digits = new int[1 + (int)Math.Log10(x)];
for (int i = digits.Length - 1; i >= 0; i--)
{
int digit;
x = Math.DivRem(x, 10, out digit);
digits[i] = digit;
}
And one more way using ToString:
int x = 123;
int[] digits = Array.ConvertAll(x.ToString("0").ToCharArray(), ch => ch - '0');
You can use this and not convert to a string:
var digits = new List<int>();
var integer = 123456;
while (integer > 0)
{
digits.Add(integer % 10);
integer /= 10;
}
digits.Reverse();

Preference for C# in adding, multiplication, and division

How does C# execute this?
static void Main(string[] args)
{
int i = 4;
i *= 4 + 8 / 2;
Console.WriteLine(i);
}
This was asked in one of the interview Questions. And I applied BODMAS to it.
But it was wrong. Please Explain.
It will be executed equivalent to the following code:
int i = 4;
int temp = 8 / 2;
temp = 4 + temp;
i = i * temp;
The compiler will shorten it down because it can calculate the constant that is on the right of i *=, so in reality it will compile to this:
int i = 4;
i *= 8;
i *= 4 + 8 / 2 is executed as:
i = i * (4 + (8 / 2))
That's the correct way of reading it.
Operator precedence is very clear on this: The / is a multiplicative operator and is applied first, then the +. The *= is an assignment operator and is applied last.
So:
8 / 2 = 4
4 + 4 = 8
i *= 8;
so i will be 4 * 8 = 32;

Categories

Resources