0
C# Practice 12: Code Lesson
In this practice i have to write code for solving the area of a circle. The problem is, there is a bug. The Input of the same variable is not the same as the output. Anyone had this problem too?
16 Answers
+ 7
Anton Lieske, console.Read() returns an integer. And there is a problem with it converting to double like that. It could be done using Parse. Or in this case it is easier to just use ReadLine instead:
radius=Convert.ToDouble(Console.ReadLine());
+ 4
Anton Lieske
Aleksei Radchenkov
Console.Read() returns as int and it reads only single character so when you enter 5 it considered as a character which print ASCII value.
53 is a ASCII value of 5 so
Console.Write(Console.Read()) //print 53 when you enter 5
@Anton - This is not a bug.
+ 4
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟, Oh, I didn't know that about read() returning character code))) Thanks :)
+ 2
Nice👍
+ 2
Anton Lieske
This program will print ASCII value of 0-9 digits
https://code.sololearn.com/cZMu8YBbQu7H/?ref=app
+ 1
Anton Lieske
Where is your code? And which language?
+ 1
Anton Lieske, mate he meant which programming language. And we can't help you unless you show us your code.
+ 1
Ah sorry.. i forget to tell that it is c#
+ 1
Anton Lieske
I thought you were comparing input with test cases output.
Now I understood you wanted to check your input is right or not.
+ 1
Ok i didnt know that the int value of the string "5" is "53". nice to know, if something like this happens again.
I forget to convert and he gives numerical values back, so i didnt doubt my code. That was the problem.
Now it works fine. 👌
+ 1
Thanks ✌️
0
Thats the code:
const double pi = 3.14;
double radius;
double solution;
radius=Console.Read();
Console.Write(radius);
Input: 5
Output:53
0
Anton Lieske
1 - Convert radius input to double
2 - you don't have to print input, you have to print area of circle so use formula pi * r * r
0
I know how to solve it. But the problem is that
Input isnt the same as output.
0
Please.. understand, that the output with a value of 53 doesnt makes sense when input was 5.
There was no calculation in this code. So input has to be output.
0
Ok now it works:
3.14;
double solution;
double radius = Convert.ToDouble(Console.ReadLine());
solution = radius * radius * pi;
Console.Write(solution);