C# Number 12 | Sololearn: Learn to code for FREE!


C# Number 12

When I run the code it says that * can’t be applied to a string but I thought I converted it. What do I do? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { const double pi = 3.14; double radius = Convert.ToDouble(Console.ReadLine()); Console.Write("+pi+"*"+radius+"*"+radius+"); } } }


5/2/2021 11:07:23 PM


6 Answers

New Answer


You don't need quotes or plus signs in the last line, just pi*radius*radius. Your code right now says multiply these 3 strings (which just happen to contain the names of your variables, enclosed in plus signs)


Just as Benjamin said this code tries to multiply 3 strings it's like ("+pi+") * ("+radius+") * ("+radius+") Since it can't multiply strings it throws an error. The Write function can take numbers as an output so just type Console.Write(pi*radius*radius);


Print only the result . Either directly or give it in a variable and print


Reconsider that last Console.Write command. I think the quotes don’t line up the way you want them too. It looks like you are trying to multiply three strings together. I can’t tell if those are single quotes or double quotes, but either way it’s a little off.


Math.PI https://docs.microsoft.com/en-us/dotnet/api/system.math.pi?view=net-5.0 Math.Pow https://docs.microsoft.com/en-us/dotnet/api/system.math.pow?view=net-5.0 If you want to use .net framework. You can use Math Class. You'll get. double input = Convert.ToDouble(Console.ReadLine()); double radius = Math.Pow(input,2); Console.Write(Math.Round((Math.PI * radius),2)); Console.ReadKey(); 🌻😊🌼


Bad code 🐷 moon a. 🐽 ; confusing because it doesn't distinguish between the actual radius and it's power.