skip to Main Content
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


  1. 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.

    Login or Signup to reply.
  2. What you want in Result and result are the results of Math.Pow (num, 2) and Math.Pow (num2, 2) but instead your code is trying to store the results of the console writes – this will not work since the results is void not an int.

    Instead store the results first and then do the console writes.

    int result = Math.Pow (num, 2);
    int result2 = Math.Pow (num2, 2); 
    Console.WriteLine(result);
    Console.WriteLine(result2);
    

    Note: I’ve renamed the "result" variables for readability.

    Login or Signup to reply.
  3. 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

    double Result = Math.Pow(num, 2);
    Console.WriteLine(Result);
    double Result2 = Math.Pow(num2, 2);
    
    Console.WriteLine(Result2);
    
    Login or Signup to reply.
  4. Your problem is in this part of code.

    int Result = Console.WriteLine(Math.Pow (num, 2));

    Happening because Console.WriteLine return type is void not int.

    You need to change it like

    int Result = Math.Pow(num,2);
    Console.WriteLine(Result);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search