Answer the question
In order to leave comments, you need to log in
Why does the mathematical calculation in this code come out incorrectly, for example, with n = 301?
Could this be a conversion issue?
string n = Console.ReadLine();
string m = "";
for (int i = n.Length - 1; i >= 0; i--)
{
m = m + n[i];
}
Console.WriteLine("Введенное число в обратном порядке: " + m + "\n");
Console.WriteLine("Результат должно было бы так если n = 103(т.е обратный вид 301):");
Console.WriteLine("1 * (8^0) = " + (1 * Math.Pow(8, 0)));
Console.WriteLine("0 * (8^1) = " + (0 * Math.Pow(8, 1)));
Console.WriteLine("3 * (8^2) = " + (3 * Math.Pow(8, 2)));
Console.WriteLine("Cумма = 1 + 0 + 192 = 193 - корректный ответ");
double s = 0, s1 = 0;
Console.WriteLine("\nНачало цикла:\n");
for (int i = 0; i < m.Length; i++)
{
Console.Write(i + " цикл = ");
s1 = Convert.ToInt64(m[i]) * Math.Pow(8, i);
Console.WriteLine(s1);
s = s + (Convert.ToInt32(m[i]) * Math.Pow(8, i));
}
Console.WriteLine("Cумма = " + s +" - ответ некорректно");
Console.ReadKey();
Answer the question
In order to leave comments, you need to log in
The entered number is written according to the decimal number system .
In order for the new number to be in reverse order, you need to not only add, but also multiply by the base in the degree, taking into account the position in the number. Thus, we need an additional counter variable for the position.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question