int num;
int num2;
Console.Write("insert one number: ");
num = Convert.ToInt32(Console.ReadLine());
Console.Write("insert another number: ");
num2 = Convert.ToInt32(Console.ReadLine());
int Result = Console.WriteLine(Math.Pow (num, 2));
int result = Console.WriteLine(Math.Pow (num2, 2));
Console.Write("the square roots of the given numbers are: "+ num +", "+ num2 + "and the greatest number is: ");
Console.ReadKey();
the interface of the new vs code studio is exactly like how i wrote here, there seems to be no main function so am confused while checking old solutions from others
4
Answers
In order for you to be able to assign the return type of a method to a variable then that method needs to have a return type.
Console.WriteLine()
does not have a return type, that is why the compiler generated the error your saw.What you want in
Result
andresult
are the results ofMath.Pow (num, 2)
andMath.Pow (num2, 2)
but instead your code is trying to store the results of the console writes – this will not work since the results isvoid
not anint
.Instead store the results first and then do the console writes.
Note: I’ve renamed the "result" variables for readability.
Console.WriteLine is void and doesn’t return a result.if you want to return a data and full this variable Result ,you must use this code
Your problem is in this part of code.
int Result = Console.WriteLine(Math.Pow (num, 2));
Happening because Console.WriteLine return type is
void
notint
.You need to change it like