How can I calculate divide and modulo for integers in C#?
Here's an answer from the MSDN documentation.
When you divide two integers, the result is always an integer. For example, the result of 7 / 3 is 2. To determine the remainder of 7 / 3, use the remainder operator (%).
int a = 5;
int b = 3;
int div = a / b; //quotient is 1
int mod = a % b; //remainder is 2
C# modulus operator
Because the remainder of 3 / 4 = 3.
http://en.wikipedia.org/wiki/Modulo_operator
remainder (%) operator c#
Check Output you can understand every steps and what's happening in every step
using System;
class MainClass {
public static void Main (string[] args) {
Console.WriteLine("Please enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int sum = 0;
while (number> 0)
{
Console.WriteLine("Steps");
Console.WriteLine("--------------");
Console.WriteLine("Digits: "+ (number % 10));
sum = sum + (number % 10);
Console.WriteLine("sum: "+ sum);
number = number / 10;
Console.WriteLine("number after number/10: "+ (number).ToString());
Console.WriteLine("--------------");
}
Console.WriteLine("{0}{1}", "Sum of digits is: ", sum.ToString());
}
}
Output:
Please enter a number:
9874
Steps
--------------
Digits: 4
sum: 4
number after number/10: 987
--------------
Steps
--------------
Digits: 7
sum: 11
number after number/10: 98
--------------
Steps
--------------
Digits: 8
sum: 19
number after number/10: 9
--------------
Steps
--------------
Digits: 9
sum: 28
number after number/10: 0
--------------
Sum of digits is: 28
C# - Separate integers/strings with division and remainder
The first question is really more about maths than programming. You know what the division and modulus operators do. Think about how you could use them to get the last (least significant) digit. Once you've done that you can apply a similar technique for the 2nd digit (tens) and then the same for hundreds and thousands and so on.
You've already found Char.IsLetter
, but there is also Char.IsDigit
, which does what you want more directly. You can do this check for one character and what you have is a string of characters. Have a look at a foreach
loop.
Mathematical modulus in c#
Try (a % b) * Math.Sign(a)
Try this; it works correctly.
static int MathMod(int a, int b) {
return (Math.Abs(a * b) + a) % b;
}
C# Operator Modulus?
Suppose you introduce 123.
Step 1: 123 % 10 = 3;
Sum = 3
Step 2: 123 / 10 = 12;
12 % 10 = 2;
Sum = 5
Step 3: 123 / 100 = 1;
Sum = 6;
C# modulo operator
There is no modulo operator in c#.
The %
operator, is the remainder operator:https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#remainder-operator-
The remainder operator % computes the remainder after dividing its left-hand operand by its right-hand operand.
Purpose of using modulus to find a numbers?
712 / 100
IS NOT 7.12
. It's 7
. If you'll divide 7
by 10
, then you'll get 0
. If you'll take modulus of 7 % 10
, you'll get 7
, because it's the rest of dividing 7/10
. Dividing ints always rounds down to the full decimal value, there is no rest. That's what the modulus operator is for and you need to evaluate them separately.
Example:
Your input is 12345
. 12345/100
gives you 123
, because you have 123 full hundrets
in the input. Then 123/10
would give you 12
, because there are 12 full tens
in 123
and 123%10
would give you 3
(which is the number you're looking for), because it's the rest from dividing 123/10
.
And if you'll ever need division in the school-math type, use floating point types, like float
or double
. F.e. 12345f/100
would give you approximately 123.45f
.
% (mod) explanation
As explained in the comments, the different behaviour is by design. The different languages just ascribe different meanings to the %
operator.
You ask:
How can I use modulus operator in C#?
You can define a modulus operator yourself that behaves the same way as the Python %
operator:
int mod(int a, int n)
{
int result = a % n;
if ((result<0 && n>0) || (result>0 && n<0)) {
result += n;
}
return result;
}
Related Topics
C# MACro Definitions in Preprocessor
Piping in a File on the Command-Line Using System.Diagnostics.Process
Entity Framework Creates a Plural Table Name, But the View Expects a Singular Table Name
Linq: from a List of Type T, Retrieve Only Objects of a Certain Subclass S
Why Gettype Returns System.Int32 Instead of Nullable<Int32>
When Is Using the C# Ref Keyword Ever a Good Idea
Setting the Initial Directory of an Savefiledialog
Httpwebrequest Times Out on Second Call
Formatting Literal Parameters of a C# Code Snippet
Wpf Binding Not Updating the View
How to Add My New User Control to the Toolbox or a New Winform
Error "This Stream Does Not Support Seek Operations" in C#
How to Sort a List<T> by Multiple T.Attributes
Preventing Jit Inlining on a Method
How to Log All Thrown Exceptions
C# Controlling a Transaction Across Multiple Databases
Does Anyone Know of a Good C# API for Subversion
Failed to Update .Mdf Database Because the Database Is Read-Only (Windows Application)