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);
Related
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 1 year ago.
Improve this question
var input = Convert.ToInt32(Console.ReadLine());
for (var i = 1; i <= input; i++)
{
for (var tr = 1; tr <= input; tr++)
{
for (var ch = 1; ch <= input; ch++)
{
if (ch <= i) Console.Write("+");
else Console.Write(" ");
}
}
Console.WriteLine();
}
The program outputs triangles. The code uses three loops. How to make it so that there are only two loops and the program works the same as before?
Notice in the pattern of triangles that on the first line you print 1 + then print n-1 blanks repeatedly. On the second line you print two + then print n-2 blanks repeatedly.
To decide if you need to print a plus or a blank you can use modulo arithmetics. if c % n < lineno print + else print a blank.
You print n * n characters in each line.
That's not the complete code but a good boost to implement the solution.
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 1 year ago.
Improve this question
Hi everyone I need to generate all strings from aa0000 to zz9999; where the first two position are only chars from a to z and the last four positions are from 0000 to 9999.
I tried everything I could but I can't find a way to do this.
Thanks in advance!
You can try nested loops, e.g.
Code:
public static IEnumerable<string> Generator() {
for (char a = 'a'; a <= 'z'; ++a)
for (char b = 'a'; b <= 'z'; ++b)
for (int i = 0; i <= 9999; ++i)
yield return $"{a}{b}{i:d4}";
}
...
foreach (string s in Generator()) {
//TODO: Put relevant code here
}
Demo:
Console.WriteLine(string.Join(Environment.NewLine, Generator()
.Skip(1_000_000 - 5) // skip some items
.Take(10))); // then take some items
Console.WriteLine();
Console.Write(Generator().Count()); // how many items do we have?
Outcome:
dv9995
dv9996
dv9997
dv9998
dv9999
dw0000
dw0001
dw0002
dw0003
dw0004
6760000
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
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 count sum of the numbers found in a string, not digits. For example, there are string = "abc12df34", and the answer must be 46 (12+34), not 10. Also maybe negative numbers for string = "abc10gf-5h1" answer must be 6. I can not understand how to implement this.
RegEx approach:
string input = "abc10gf-5h1";
int result = Regex.Matches(input, "-?[0-9]+").Cast<Match>().Sum(x =>int.Parse(x.Value));
While the above answer is elegant, it's not really something to understand what to do or how to approach the problem. Here is another, more explicit solution.
In words, you iterate through your string, collect digits as long as there are any, if you find a nondigit, your number is finished, you convert the number to integer and sum it up, clear the number string. The same you do if you find the end of the string. On the next found digit you start collecting digits again.
This algorithm will fail on any number larger than than 10 digits in multiple ways (as the other answer will also), but this is just for demonstration anyway.
string input = "abc10gf-5-1h1";
var number = new char[10];
int numberlength = 0;
int pos = 0;
int sum = 0;
while (pos < input.Length)
{
char c = input[pos++];
if (char.IsDigit(c))
{
number[numberlength++]=c;
}
else
{
if (numberlength > 0)
{
sum += int.Parse(new String(number, 0, numberlength));
numberlength = 0;
}
if (c=='-')
number[numberlength++]=c;
}
}
if (numberlength > 0)
sum += int.Parse(new String(number, 0, numberlength));
Console.WriteLine(sum);
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 6 years ago.
Improve this question
I am trying to divide BigIntegers.
this is my code so far:
BigInteger x = BigInteger.Parse("2697841949839130393229424953460693359375000000");
BigInteger y = BigInteger.Parse("2");
for (int i = 0; i < 300; i++) {
double result = Math.Exp(BigInteger.Log(x) / BigInteger.Log(y));
x = result;
Console.WriteLine(result);
}
It does the divide. it does it only once. I want to divide the code to minimum it has. it should be 6.
I know that this:
for (int i = 0; i < 300; i++){}
is not the right way to do it. does any one know any other way how to?
I remember from your other question that you are trying to decode Godel numbers.
You shouldn't stick the result in a double. It's too big. You're looking for BigInteger's Divide method. For your application DivRem is better.
Here's some sample code. It computes 6. This should get you going for the first character. You'll also need to build a list of primes beyond '2' to continue on to the rest of the string.
using System;
using System.Numerics;
public class Program
{
public static void Main()
{
BigInteger x = BigInteger.Parse("2697841949839130393229424953460693359375000000");
BigInteger y = BigInteger.Parse("2");
int counter = 0;
BigInteger remainder;
do{
BigInteger result = BigInteger.DivRem(x, y, out remainder);
if(!remainder.IsZero)
break;
x = result;
counter++;
} while (true);
Console.WriteLine(counter);
}
}