Double the alternative digits in a number and add each digit - c#

I am using the following code to double alternative digits from a number and add every digit but its not working in c#
int sum=0,r,number;
Console.WriteLine("Enter the number");
number = int.Parse(Console.ReadLine());
if (number % 2!= 0)
{
number = number * 2;
number++;
}
Console.WriteLine("numbers:" + number);
Console.ReadLine();
while (number!= 0)
{
r = number % 10;
number= number/ 10;
sum = sum + r;
}
Console.WriteLine("sum of digits of the number:" + sum);
Console.ReadLine();
Kindly help me. Thank you in advance.

As StevieB said "You're not doubling alternative digits. You're doubling the number and adding 1 if the original number is odd. Then you're summing the digits of the number."
The following code should work for you. I have not tested it for all possible scenarios so please do so.
Also tweek it a little as per your requirements.
Console.WriteLine("Enter a number:");
char[] enteredNumber = Console.ReadLine().ToArray();
int finalNumber = 0;
for (int i = 0; i < enteredNumber.Count(); i++)
{
if (i % 2 != 0)//This condition might need tweeking
{
finalNumber = finalNumber + (Convert.ToInt32(enteredNumber[i].ToString()) * 2);
}
else
{
finalNumber = finalNumber + Convert.ToInt32(enteredNumber[i].ToString());
}
}
Console.WriteLine(finalNumber);

What you are looking for is Luhn Algorithm which I have reproduced here for you.
Luhn Algorithm - C#
Luhn Algorithm uses the last digit (rightmost digit) as a checksum digit. Hence in my first iteration I skip it since it's not required in the first parts of the function.
Since the length of a given number can vary, I have used int a to check my position and know which number I should double and which I should skip.
Read more about the Luhn Algorithm here.
using System;
public class Program {
public void Main()
{
int sum=0,d;
string oNum = "79927398713";
int a = 0;
for(int i = oNum.Length-2; i>=0;i--)
{
d = Convert.ToInt32(oNum.Substring(i,1));
if (a % 2 == 0) d = d*2;
if (d > 9) d -= 9;
sum += d;
a++;
}
if ((10 - (sum%10)) == Convert.ToInt32(oNum.Substring(oNum.Length-1)))
{
Console.WriteLine("Valid");
}
else
{
Console.WriteLine("Invalid");
}
Console.WriteLine("sum of digits of the number:" + sum);
}
}
There are three places where the Console writes a line. And at least two of those are visible for any given number. The line that you wanted, "sum of digits of the number:" will always show.

Related

Get all n-digit numbers whose sum of digits equals to given sum

How can I get all n-digit numbers whose sum of digits equals to given sum? I need the fastest solution because n can be equal with 9 and sum can be equal with 1000.
I have implemented the solution below but it's too slow...
List<int> l = new List<int>();
void findNDigitNumsUtil(int n, int sum, char[] ou, int index)
{
if (index > n || sum < 0)
return;
if (index == n)
{
if (sum == 0)
{
ou[index] = '\0';
string s = new string(ou);
l.Add(Int32.Parse(s));
}
return;
}
for (int i = 0; i <= 9; i++)
{
ou[index] = (char)(i + '0');
findNDigitNumsUtil(n, sum - i, ou,
index + 1);
}
}
void findNDigitNums(int n, int sum)
{
char[] ou = new char[n + 1];
for (int i = 1; i <= 9; i++)
{
ou[0] = (char)(i + '0');
findNDigitNumsUtil(n, sum - i, ou, 1);
}
}
I need the fastest solution
No, you need a fast-enough solution. You are probably unwilling to spend even a million dollars on custom hardware to get the fastest possible solution.
How can I get all n-digit numbers whose sum of digits equals to given sum?
Here, I'll give you the solution for a slightly different problem:
What are all the sequences of n digits drawn from 0-9 that sum to sum?
This is different because this counts 01 and 10 as sequences of length two that sum to 1, but 01 is not a two-digit number.
I'll give you a hint for how to solve this easier problem. You then take that solution and adapt it to your harder problem.
First, can you solve the problem for one-digit numbers? That's pretty easy. The one-digit numbers whose digits sum to n are the digit n if n is 0 through 9, and there is no solution otherwise.
Second: Suppose n > 1. Then the n-digit numbers that sum to sum are:
0 followed by all the n-1 digit numbers that sum to sum
1 followed by all the n-1 digit numbers that sum to sum-1
2 followed by all the n-1 digit numbers that sum to sum-2
...
9 followed by all the n-1 digit numbers that sum to sum-9
Write an implementation that solves that problem, and then adapt it to solve your problem.
You can treat n-digit number as an array of n digits. Then you can increment a particular number to the next number that also adds up to the sum. Stepping through all the next answers, you have generated all possible combinations.
Using a generator to yield each n-digit combination as an IEnumerable<int> (in fact, an int[]), you start with the "smallest" n-digit combination that yields the sum, and go through each one.
IEnumerable<IEnumerable<int>> DigitsToSum(int n, int sum) {
if (sum > 9 * n)
yield return Enumerable.Empty<int>();
else {
var ans = new int[n];
void distribute(int wsum, int downto) {
for (var j1 = n - 1; j1 > downto; --j1) {
if (wsum > 9) {
ans[j1] = 9;
wsum -= 9;
}
else {
ans[j1] = wsum;
wsum = 0;
}
}
}
ans[0] = Math.Max(1, sum-9*(n-1));
distribute(sum-ans[0], 0);
bool nextAns() {
var wsum = ans[n-1];
for (var j1 = n - 2; j1 >= 0; --j1) {
wsum += ans[j1];
if (ans[j1] < Math.Min(9, wsum)) {
++ans[j1];
distribute(wsum - ans[j1], j1);
return true;
}
}
return false;
}
do {
yield return ans;
} while (nextAns());
}
}
This is tremendously faster than my recursive double generator solution (somewhat like #EricLippert's suggestion) to iterate over all possibilities (e.g. using Count()).
You can put the digits back together to get a final numeric string for each number:
var ans = DigitsToSum(n, sum).Select(p => String.Join("", p));

count odd and even digits in a number

I try to write program that check the ratio between odd and even
digits in a given number. I've had some problems with this code:
static void Main(string[] args)
{
int countEven = 0 ;
int countOdd = 0 ;
Console.WriteLine("insert a number");
int num = int.Parse(Console.ReadLine());
int length = num.GetLength;
for (int i = 0;i<length ; i++)
{
if((num/10)%2) == 0)
int countEven++;
}
}
any ideas?
The problem is that int does not have a length, only the string representation of it has one.As an alternative to m.rogalski answer, you can treat the input as a string to get all the digits one by one. Once you have a digit, then parsing it to int and checking if it is even or odd is trivial.Would be something like this:
int countEven = 0;
int countOdd = 0;
Console.WriteLine("insert a number");
string inputString = Console.ReadLine();
for (int i = 0; i < inputString.Length; i++)
{
if ((int.Parse(inputString[i].ToString()) % 2) == 0)
countEven++;
else
countOdd++;
}
Linq approach
Console.WriteLine("insert a number");
string num = Console.ReadLine(); // check for valid number here?
int countEven = num.Select(x => x - '0').Count(x => x % 2 == 0);
int countOdd = num.Select(x => x - '0').Count(x => x % 2 != 0);
Let's assume your input is : 123456
Now all you have to do is to get the modulo from the division by ten : int m = num % 10;
After that just check if bool isEven = m % 2 == 0;
On the end you have to just divide your input number by 10 and repeat the whole process till the end of numbers.
int a = 123456, oddCounter = 0, evenCounter = 0;
do
{
int m = a % 10;
switch(m % 2)
{
case 0:
evenCounter++;
break;
default: // case 1:
oddCounter++;
break;
}
//bool isEven = m % 2 == 0;
}while( ( a /= 10 ) != 0 );
Online example
Made a small change to your code and it works perfectly
int countEven = 0;
int countOdd = 0;
Console.WriteLine( "insert a number" );
char[] nums = Console.ReadLine().ToCharArray();
for ( int i = 0; i < nums.Length; i++ )
{
if ( int.Parse( nums[i].ToString() ) % 2 == 0 )
{
countEven++;
}
else
{
countOdd++;
}
}
Console.WriteLine($"{countEven} even numbers \n{countOdd} odd numbers");
Console.ReadKey();
What I do is get each number as a a character in an array char[] and I loop through this array and check if its even or not.
If the Input number is a 32-bit integer (user pick the length of the number)
if asked:
The number of even digits in the input number
Product of odd digits in the input number
The sum of all digits of the input number
private void button1_Click(object sender, EventArgs e) {
int num = ConvertToInt32(textBox1.Text);
int len_num = textBox1.Text.Length;
int[] arn = new int[len_num];
int cEv = 0; pOd = 0; s = 0;
for (int i = len_num-1; i >= 0; i--) { // loop until integer length is got down to 1
arn[i] = broj % 10; //using the mod we put the last digit into a declared array
if (arn[i] % 2 == 0) { // then check, is current digit even or odd
cEv++; // count even digits
} else { // or odd
if (pOd == 0) pOd++; // avoid product with zero
pOd *= arn [i]; // and multiply odd digits
}
num /= 10; // we divide by 10 until it's length is get to 1(len_num-1)
s += arn [i]; // sum of all digits
}
// and at last showing it in labels...
label2.Text = "a) The even digits count is: " + Convert.ToString(cEv);
label3.Text = "b) The product of odd digits is: " + Convert.ToString(pOd);
label4.Text = "c) The sum of all digits in this number is: " + Convert.ToString(s);
}
All we need in the interface is the textbox for entering the number, the button for the tasks, and labels to show obtained results. Of course, we have the same result if we use a classic form for the for loop like for (int i = 0; and <= len_num-1; i++) - because the essence is to count the even or odd digits rather than the sequence of the digits entry into the array
static void Main(string args[]) {
WriteLine("Please enter a number...");
var num = ReadLine();
// Check if input is a number
if (!long.TryParse(num, out _)) {
WriteLine("NaN!");
return;
}
var evenChars = 0;
var oddChars = 0;
// Convert string to char array, rid of any non-numeric characters (e.g.: -)
num.ToCharArray().Where(c => char.IsDigit(c)).ToList().ForEach(c => {
byte.TryParse(c.ToString(), out var b);
if (b % 2 == 0)
evenChars++;
else
oddChars++;
});
// Continue with code
}
EDIT:
You could also do this with a helper (local) function within the method body:
static void Main(string args[]) {
WriteLine("Please enter a number...");
var num = ReadLine();
// Check if input is a number
if (!long.TryParse(num, out _)) {
WriteLine("NaN!");
return;
}
var evenChars = 0;
var oddChars = 0;
// Convert string to char array, rid of any non-numeric characters (e.g.: -)
num.ToCharArray().Where(c => char.IsDigit(c)).ToList().ForEach(c => {
byte.TryParse(c.ToString(), out var b);
if (b % 2 == 0)
evenChars++;
else
oddChars++;
// Alternative method:
IsEven(b) ? evenChars++ : oddChars++;
});
// Continue with code
bool IsEven(byte b) => b % 2 == 0;
}
Why am I using a byte?
Dealing with numbers, it is ideal to use datatypes that don't take up as much RAM.
Granted, not as much an issue nowadays with multiple 100s of gigabytes possible, however, it is something not to be neglected.
An integer takes up 32 bits (4 bytes) of RAM, whereas a byte takes up a single byte (8 bits).
Imagine you're processing 1 mio. single-digit numbers, and assigning them each to integers. You're using 4 MiB of RAM, whereas the byte would only use up 1 MiB for 1 mio. numbers.
And seeming as a single-digit number (as is used in this case) can only go up to 9 (0-9), you're wasting a potential of 28 bits of memory (2^28) - whereas a byte can only go up to 255 (0-255), you're only wasting a measly four bits (2^4) of memory.

Efficient way to count the number of appearances of a digit in a number

I need to count the number of times a single digit (not 0) appears in a number (positive integer) of varying length.
The obvious solution is to convert the number to a string, the digit to a character and iterate over the string to count the number of times the character appears in the string.
static int CountDigitInString(string searchString, char digit)
{
int sum = 0;
for (int i = 0; i < searchString.Length; i++)
{
if (searchString[i] == digit)
sum++;
}
return sum;
}
The problem with this method, however, is that it is too slow for my purposes as I am running it many times.
public static void Run()
{
for (int i = 0; i < 1000000; i++)
{
CountDigitInString(i.ToString(), (char)j);
}
}
After I noted that the process took too much time, the CPU sampling profiler showed me that the problem was with the conversion to string.
So, how do I efficiently count the number of times a digit (single digit only, not a number) appears in a number (of any length)?
Here is more optimized version of #shaitibber solution. It replaces one division with multiplying and returns 1 for 0,0. It is about 20% faster.
static int CountDigitsInString2(int number, int digit)
{
int sum = 0;
do
{
int n2 = number / 10;
if (number - n2 * 10 == digit)
sum++;
number = n2;
} while (number != 0);
return sum;
}
And here is solution about three times faster than that (but does not work for 0 digit, which is not required). It precalculates results for numbers 0..9999.
private static int[][] cache = new int[10][];
private const int cacheSize = 10000;//or 100000
private static int[] initCache(int digit)
{
var ca = cache[digit] = new int[cacheSize];
for (int i = 0; i < ca.Length; ++i)
{
ca[i] = CountDigitsInString2(i, digit);
}
return ca;
}
static int CountDigitsInString3(int number, int digit)
{
var ca = cache[digit] ?? initCache(digit);
int sum = 0;
while (number != 0)
{
int n2 = number / cacheSize;
sum += ca[number - n2 * cacheSize];
number = n2;
};
return sum;
}
I found a way which turned out to be about 3 times as fast on average (checked using a Stopwatch):
static int CountDigitsInString(int number, int digit)
{
int sum = 0;
while (number != 0)
{
if (number % 10 == digit)
sum++;
number /= 10;
}
return sum;
}
EDIT:
I found a way which is over 4 times as fast as the one above. Before I start, note that this solution is valid only for cases in which you are counting appearances of a digit in consecutive numbers.
It occurred to me that if you counted the number of times the digit "d" appeared in a number "A", then you don't neccessarily have to recount the number of times "d" appears in "A + 1" to know what it is.
For example, if I know that the digit 3 appears 4 times in the number 35312336, I can know for a fact that it will still appear 4 times in the next consecutive number 35312337, without actually counting.
The reason I can do this is that the count would only change in one of three cases:
1) When the last digit of "A - 1" was a 9, "A" can change entirely due to numbers being carried over. This is the only case in which we actually have to count (although you could, theoretically, optimize this further by checking the numbers carried over to see if they affect the total but this strikes me as overly complicated).
2) When the last digit of "A - 1" was "d - 1", we know that the number of times "d" appears in "A" has increased by one.
3) When the last digit of "A - 1" was "d", we know that the number of times "d" appears in "A" has decreased by one.
This means that you only have to count the appearances of "d" in "A" using arithmetical operations in one out of 10 cases!
public static void Run()
{
int digit = 1;
int count = 0;
for (int i = 0; i < 100000; i++)
{
int previousLastDigit = (i - 1) % 10;
if (previousLastDigit == (digit - 1))
count++;
else if (previousLastDigit == 9)
count = CountDigitsInString(i, digit);
else if (previousLastDigit == digit)
count--;
Console.WriteLine(digit + " appears " + count + " times in the number " + i);
}
}
The CountDigitsInString function is the one above.
Here is a little snip with LINQ to give you another way to do it (didn't ran a stopwatch)
var number = 11334511;
var digit = 1;
var digitAsChar = Convert.ToChar(digit.ToString().ToLower());
// Occurence will be 4
var occurence = number.ToString().ToLower().Count(s => s == digitAsChar);
I suggest you use LINQ on string.
Source: https://msdn.microsoft.com/en-us/library/mt693025.aspx
static int CountDigitInString(string number, char digit)
{
int count = number.Count(ns => ns == digit);
return count;
}

C# number format: display n significant number of digits?

is there a way to format a double value such that it shows the n siginifacant digits only?
for example I have a double with value 123456, can we have a format string such that it displays the first 3 digits only.
double x=12346;
string s=x.ToString("Some format"); //display 123 only
is that possible?
Although there are formats that would let you drop some or all of the fraction, no format would let you drop some of the significant digits of the whole part of the number.
If you would like to keep the first three digits of the whole number, you need to divide the value so that its whole part has only three digits.
One approach to computing the divisor is taking log10N, checking if it is above 2, and dividing by 10 to the corresponding power:
private static void Print(double x) {
int n = (int)Math.Log10(x);
if (n > 2) {
x /= Math.Pow(10, n-2);
}
Console.WriteLine((int)x);
}
Demo.
You can't format the largest part of the double to only be to 3 sig. fig. but you can split the string. Try:
String s = x.ToString().Substring(0, n);
Where n is the number of significant figures you wish to keep.
I made a console application for this example. I know you need a double or int data type for the number, but I didn't know how manipulate the decimal numbers after the point, so I used a string (if you don't mind, store the number value inside string data type):
string number = "";
string digits = "";
int n = 0;
int count = 0;
number = "45.6";
//number = "456";
n = 3;
if (number.Contains('.')) //If the number has decimals...
{
if (n < number.Length)
{
if (number.IndexOf('.') < n)
{
while (count <= n) //... we will count the number in a different way.
{
if (number[count] != '.')
{
digits = digits + number[count];
}
count++;
}
}
else
{
while (count < n)
{
if (number[count] != '.')
{
digits = digits + number[count];
}
count++;
}
}
}
}
else
{
if (n <= number.Length)
{
while (count < n) //If not, we count without the decimal point.
{
digits = digits + number[count];
count++;
}
}
}
Console.WriteLine("N significant digits: " + digits);
You can try with decimal and integer numbers, but in the code, they both are strings. As I said before, if you don't mind using this data type, this example will help you, if not, you can try with "Substring" function from the String class.

What number is a specified digit and how many of them are there in a number?

Ok, so at first, I'm very beginner in programming. It's my school homework and I cannot use conversion to string. Just if,else,for,while.
On input there is the number and the digit.
I know how to get information what number is a specified digit in a number but I have no idea how to find out how many of these numbers are there.
Let's say I have number 123 467 (it has to be less than 999 999) and I want the third number. I know it's bigger than 100 000, so I do the math - (int) 123 467 / 100 = 123 and then 123%10 = 3. Now I need to know if there are any more 3's in the number - but here is the point - I'm not sure what cycle should I use.
And I also have to create some code which determines how large is the number (bigger than 100/1000/10000/...).
I'm not asking for a full solution but little help would be appreciated. Even in a pseudolanguage.
Current code (almost nothing):
double digit, number;
try
{
digit = Convert.ToInt32(poledigit.Text);
number = Convert.ToInt32(polenumber.Text);
}
catch
{
MessageBox.Show("Zadejte číslo ve správném formátu");
return;
}
if (digit > 6 & number > 999999)
{
MessageBox.Show("Číslo musí být menší než 999 999 a digit musí být menší než 6.");
return;
}
while(number >= 100000)
{
number /= Math.Pow(10, digit);
number %= 10;
}
I would create an int array counting the number of digits
int[] digitCount = new int[10]; // Range: digitCount[0..9]
Then determine the digits one by one by eliminating the last one, until the number is zero. The loop would repeat the following code:
int digit = number % 10;
number /= 10;
digitCount[digit]++;
Now digitCount contains the count of each digit
int countOfDigit3 = digitCount[3];
If you cannot use arrays, count only the occurences of the desired digit
int digit = ...;
int digitCount = 0;
while (number != 0) {
int d = number % 10;
number /= 10;
if (d == digit) {
digitCount++;
}
}
You can iterate through the digits as follows:
int digitToSearch = 3;
int count = 0;
while (number != 0)
{
int digit = number % 10;
if (digit == digitToSearch)
count++;
number /= 10;
}

Categories

Resources