C# Questions Answers-C# function (PartII)


C# function (PartII)

1. Find any errors in the following function definition:
static void myfunc(int x,int y){
  Cosole.WriteLine(x*y);
  return(x*y);
}
Check answer 2. Find any errors in the following function definition:
static int myfunc(int x,y){
    return(x*y);
}
Check answer 3. What would be the output from the following C# program when run using 3 and 4 as input data?
using System;
namespace ConsoleApplication1{
  class Program{
 
      static void Main(string[] args){
        int x,y,res;
        Consle.Write("Enter x:");
        x=Int32.Parse(Console.ReadLine());
        Consle.Write("Enter y:");
        y=Int32.Parse(Console.ReadLine());
        res=subtr(x,y);
        Console.WriteLine("Result={0}",res);
        Console.ReadLine();//wait for keypress
}

static int subtr(int x,int y){
  return(x-y);
      }
   }
}  Check answer 4.What would be the output from the following C# program when run using 2, 4, and 1 as input data?
using System;
namespace ConsoleApplication1{
  class Program{
 
      static void Main(string[] args){
          int x,y,z,max;
          Console.Write("Enter x, y and z separated by enter keys:");
          x=Int32.Parse(Console.ReadLine());
          y=Int32.Parse(Console.ReadLine());
          z=Int32.Parse(Console.ReadLine());
          max=max3(x,y,z);
          Console.WriteLine("The max value is {0}.",max);

}

static int max3(int x,int y,int z){
  if(x>y)
     if(x>z) return x;
     else return z;
  else if(y>z) return y;
  else return z;
      }
   }
}
Check answer 5.What would be the output from the following C# program when run a.using 3 and 2 as input data?      b. using 3 and -2 as input data?
using System;
namespace ConsoleApplication1{
  class Program{
      static void Main(string[] args){
           int b,p;
           double res;
           Console.Write("Enter base value:");
           b=Int32.Parse(Console.ReadLine());
           Console.Write("Enter power:");
           p=Int32.Parse(Console.ReadLine());
           res=mypower(b,p);
          Console.WriteLine("Result={0}",res);
 

}

static double mypower(int base,int pow){
    if(pow>=0){ //positive power
       if(pow==0) return 1;
       else if(pow==1) return base;
    else return (base*mypower(base,pow-1));
}
    else{ //negative power
        if(pow==-1) return(1/(double)base);
        else return((1/(double)base)*mypower(base,pow+1));
         }
     }
  }
}
Check answer


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.