Pages

Showing posts with label How to make Methods in C#. Show all posts
Showing posts with label How to make Methods in C#. Show all posts

Write a program in C# and find the value of final velocity by using S= (〖Vf〗^2-〖Vi〗^2)/2a.

INPUT:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication20
{   
class myclass
    {
        public double visquare(double vi)
        {
            double s = vi *vi;
            return s;
        }
        public double vfsquare(double vf)
        {
            double s = vf * vf;
            return s;
        }
        public double  s(double  vf,double vi)
    {
        double s = (vfsquare(vf) + visquare(vi)) / (2 * 9.98);
        return s;
    }
        static void Main(string[] args)
        {
            double vf,vi;
            Console.WriteLine("plz enter the final  velocity");
            vf=Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("plz enter the initial velocity ");
            vi= Convert.ToDouble(Console.ReadLine());
            myclass m = new myclass();
         
          Console.WriteLine("vf = (vi2+vf2)/2a = {0}",m.s(vf,vi));
        }  
    }
}

OUTPUT:


Write a program in C# and find the value of final velocity by using Vf=Vi+at using methods.

Solution:

Pseudo Code:

  1. Initialize variable "vi"  (this will hold initial velocity value) 
  2. Initialize variable "t" to (this will hold time value) 
  3. Print message on console to ask user to input Initial velocity.
  4. Read user's input and convert to Double and store that input into variable "vi" that is necessary for velocity formula.
  5. Print message on console to ask user to input Time.
  6. Read user's input and convert to Double and store that input into variable "t" that is necessary for velocity formula.
  7. create a separate function "calculateFinalVelocity" that will calculate velocity on the basis of initial velocity and time values and will return the final velocity, then  call "calculateFinalVelocity(vi,t)" function and pass "vi" and "t".  
  8. Print the return value of  on console to show the final velocity...

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication20
 {
class myclass
    {
        public double  calculateFinalVelocity(double  vi,double t)
        {
            double vf =  vi+t*9.98;
            return vf;
        }
        static void Main(string[] args)
        {
            double vi,t;
            Console.WriteLine("plz enter the initial velocity");
            vi=Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("plz enter the time ");
            t= Convert.ToDouble(Console.ReadLine());
            myclass m = new myclass();
         
          Console.WriteLine("the the final velocity is {0}",m.calculateFinalVelocity(vi,t));
        }
        
    }

}

Output:


Write a program that Calculate areas of rectangle using methods.

INPUT:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication20
{
class myclass
    {
        public double  method(double h,double w)
    {
        double Ar = h * w;
        return Ar;
    }
        static void Main(string[] args)
        {
            double h,w;
            Console.WriteLine("plz enter the height");
            h=Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("plz enter the wdth");
            w= Convert.ToDouble(Console.ReadLine());
            myclass m = new myclass();
         
          Console.WriteLine("the Area of the circle is {0}",m.method(h,w));
        }
        
    }
}


OUTPUT:


Write a program that Calculates areas of circle by using methods.

INPUT:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication20
{
class myclass
    {
        public double  method(double  r)
    {
        double  A = 3.14 * r * r;
        return A;
    }
        static void Main(string[] args)
        {
            double radius;
            Console.WriteLine("plz enter the radius");
            radius=Convert.ToDouble(Console.ReadLine());
            myclass m = new myclass();
         
          Console.WriteLine("the Area of the circle is {0}",m.method(radius));
        }
        
    }
}


OUTPUT:


E A PROGRAMME TO FND THE VALUE OF X BY QUARDIC FORMULA USING METHODS:

INPUT:

using System;
using System.Collections.Generic;
using System.Text;

namespace a
{
    class quadraticformula
    {
        double squareofb(double b)
        {
            double value = b * b;
            return value;
        }
        double x1(double a, double b, double c)
        {
            double ac4=4*a*c;
            double b1 =Math.Sqrt(squareofb(b)-ac4);
            double x = ((-b+b1 )/(2*a));
            return x;
        }
        double x2(double a, double b, double c)
        {
            double ac4=4*a*c;
            double b1= Math.Sqrt(squareofb(b)-ac4);
            double x =((-b-b1)/(2*a));
            return x;
        }
        static void Main(string[] args)
        {
            double a, b, c;
            Console.WriteLine("plz input the value of a");
            a = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("plz input the value of b");
            b = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("plz input the value of c");
            c = Convert.ToDouble(Console.ReadLine());
            quadraticformula q = new quadraticformula();
            Console.WriteLine("x1={0}", q.x1(a,b,c));
            Console.WriteLine("x2={0}", q.x2(a, b, c));
        }
    }
}

OUTPUT:


MAKE A PROGRAMME TO FIND THE SiDES LENGTH OF TRIANGLE BY METHODS

INPUT:

using System;
using System.Collections.Generic;
using System.Text;

namespace a
{
    class hypotenous
    {
        double square1(double a1)
        {
            double a2 = a1 * a1;
            return a2;
        }
        double square2(double b1)
        {
            double a2 = b1*b1;
            return a2;
        }
        double hyp(double a, double b)
        {
            double a1=square1(a);
           double b1= square2(b);
            double c =Math.Sqrt( a1 + b1);
            return c;
        }
        double perpendicular(double a, double b)
        {
            double a1 = square1(a);
            double b1 = square2(b);
            double c = Math.Sqrt(a1 - b1);
            return c;
        }
        double bas(double a, double b)
        {
            double a1 = square1(a);
            double b1 = square2(b);
            double c = Math.Sqrt(a1 - b1);
            return c;
        }
        static void Main(string[] args)
        {
            double a, b;
            Console.WriteLine("plz type the option base, perpendicular, hypotenouos");
            string option = Console.ReadLine();
            hypotenous h = new hypotenous();
            if (option == "base")
            {
                Console.WriteLine("plz enter the hypotenous ");
                a = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("plz enter the perpendicular");
                b = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("B={0}", h.bas(a, b));
            }
            else if (option == "perpendicular")
            {
                Console.WriteLine("plz enter the hypotenous ");
                a = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("plz enter the base");
                b = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("P={0}", h.perpendicular(a, b));
            }
            else if (option == "hypotenouos")
            {
                Console.WriteLine("plz enter the perpendicular");
                a = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("plz enter the base");
                b = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("H={0}", h.hyp(a, b));
            }
        }
    }
}

OUTPUT:


Write a program that find value from "pythagoras theorem" by using Methods

INPUT:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("~~~~~PYTHAGORAS THEOREM~~~~~");
            int p, b;
            Console.WriteLine("Please enter Perpendicular");
            p = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter Base");
            b = Convert.ToInt32(Console.ReadLine());
            double BSQR = bsqr(b);
            double PSQR = psqr(p);
            double SUMSQR = sumsqr(b, p);
            Console.WriteLine("Hyp {0}", SUMSQR);

        }
        public static double bsqr(int b)
        {
            double s;
            s = b * b;
            return s;
        }

        public static double psqr(int p)
        {
            double s;
            s = p * p;
            return s;
        }

        public static double sumsqr(int b, int p)
        {
            double s;
            s = b + p;
            s = Math.Sqrt(s);
            return s;
        }
    }

}
OUTPUT:


a C# Program ,Calculator by Making Simple And Easiest methods

INPUT:


class calculator
    {
        double  sum(double n1, double  n2)
        {
            return n1 + n2;
        }
        double  subtract(double n1, double  n2)
        {
            return n1 - n2;
        }
        double multiply(double  n1, double n2)
        {
            return n1 * n2;
        }
        double division(double  n1, double n2)
        {
            return n1 / n2;
        }
        static void Main(string[] args)
        {
            calculator c = new calculator();
            double input1, input2;
            Console.WriteLine("enter first no");
            input1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("enter second no");
            input2 = Convert.ToDouble(Console.ReadLine());
            double x = c.sum(input1, input2);
            double y = c.multiply(input1, input2);
            double z = c.division(input1, input2);
            double w = c.subtract(input1, input2);

               Console.WriteLine("subtraction {0}",w);
               Console.WriteLine("sum {0}", x);
               Console.WriteLine("multiply {0}", y);
               Console.WriteLine("division {0}", z);
        }  
    }
}



OUTPUT:


WRITE A PROGRAMME HAVING METHODS TO CALCULATE SUM.MULTIPLE.ETC:

INPUT:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Methods_Function
{
    class Program
    {
        static void Main(string[] args)
        {
                    Console.WriteLine("\t\t\t\tCalculator");
            int num1, num2;
            Console.WriteLine("Enter first number");
            num1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter second number");
            num2 = Convert.ToInt32(Console.ReadLine());
            int res1 = Sum(num1, num2);
            Console.WriteLine(res1);
            int res2 = Subtract(num1, num2);
            Console.WriteLine(res2);
            int res3 = Multiple(num1, num2);
            Console.WriteLine(res3);
            int res4 = Divide(num1, num2);
            Console.WriteLine(res4);

        }

        static int Sum(int Num1, int Num2)
        {

            int r;
            r = Num1 + Num2;
            return r;

        }

        static int Subtract(int Num1, int Num2)
        {

            int r;
            r = Num1 - Num2;
            return r;

        }

        static int Multiple(int Num1, int Num2)
        {

            int r;
            r = Num1 * Num2;
            return r;

        }

       
        static int Divide(int Num1, int Num2)
        {

            int r;
            r = Num1 / Num2;
            return r;
        }
            
        }
    }




Output: