Search This Blog

Monday, 14 December 2015

How to arrange numbers numerically









Hello guys you can arrange numbers numerically from an array using bubble sort method.



Its Simple and easy. Just Watch the video and view the codes below









namespace Bubble_Sort

    {

    class Program

        {

        static void Main(string[] args)

            {

            int[] save = { 2, 1, 6, 7, 8, 9 };

            int hold;

            for (int i = 0; i < save.Length; i++)

                {

                for (int j = 0; j < save.Length; j++)

                    {

                    if (save[i]<save[j])

                        {

                        hold = save[i];

                        save[i] = save[j];

                        save[j] = hold;

                        }

                    }

                }

            foreach (var item in save)

                {

                Console.Write(item+"\t");

                //Displays the arranged numbers

                }

            Console.ReadLine();

            }

        }

    }



Thanks for watching, visit csharpprogrammingisfun.blogspot.com

Monday, 10 August 2015

How to create a Bisection Method Scheme of any power in C#

 
BISECTION METHOD USING C#


Here's the Code
using System;
namespace BisectionMethod
{
    class Program
    {
        static void Main(string[] args)
        {  string retry = "";
            do{ try
                {  float y1 = 0, y2 = 0;
                    //collect inputs
                    Console.Write("\n1st Interval: ");
                    float x1 = float.Parse(Console.ReadLine());
                    Console.Write("2nd Interval: ");
                    float x2 = float.Parse(Console.ReadLine());
                    float x0 = 0, y0 = 0;
                    Console.Write("Highest power in equation: ");
                    int pwr = int.Parse(Console.ReadLine());
                    //store in an array
                    int[] coe = new int[pwr + 1];
                    //calculate values of y1 and y2 using for loop
                for (int i = 0; i < coe.Length; i++)
             { if (i == 0) { Console.Write("Value of constant: "); }
          else{Console.Write("Coefficient of variable with power {0}: ", i);}
                        coe[i] = int.Parse(Console.ReadLine());
                        y1 += (float)(coe[i] * Math.Pow(x1, i));
                        y2 += (float)(coe[i] * Math.Pow(x2, i));
                    } //determine negative
                    if ((y1 * y2) < 0)
                    {Console.WriteLine("There is a root between intervals");
                        while (true)
                        {//midpoint (x0)
                            x0 = (x1 + x2) / 2;
                            for (int i = 0; i < coe.Length; i++)
                            { y0 += (float)(coe[i] * Math.Pow(x0, i)); }
                            if (y0 * y1 < 0)//check for -ve product
                            { x2 = x0; }
                            else{ x1 = x0;}
                            //approximate y to 6 decimal places
                            y0 = (float)Math.Round(y0, 6);
                            //if y0=0 therefore root is x0
       if (y0 == 0) { Console.WriteLine("\n The Root is {0}", x0); break; }
                           y0 = 0;
//reset y0 back to zero in order to avoid accumulation of result
                } }
               else{ Console.WriteLine("There is NO root between intervals"); }
                }

                catch { Console.WriteLine("\a\a\a Error \nInvalid Input"); }
      Console.Write("\nDo you wish to try again, type \"Yes\" or otherwise to quit: ");
     retry = Console.ReadLine().ToUpper();
            } while (retry == "YES");
}
}


}
VIDEO ON YOUTUBE



Happy Programming!!!

Create a Regular Falsi Method Scheme in C#


REGULAR FALSI METHOD




using System;

namespace RegularFalsiMethod
{
    class Program
    {  static void Main(string[] args)
        {
            string retry = "";
            do {
                try
                { float y1 = 0, y2 = 0;
                    //collect inputs
                    Console.Write("\n1st Interval: ");
                    float x1 = float.Parse(Console.ReadLine());
                    Console.Write("2nd Interval: ");
                    float x2 = float.Parse(Console.ReadLine());
                    float x0 = 0, y0 = 0;
                    Console.Write("Highest power in equation: ");
                    int pwr = int.Parse(Console.ReadLine());
                    //store in an array
                    int[] coe = new int[pwr + 1];
                    //calculate values of y1 and y2 using for loop
                    for (int i = 0; i < coe.Length; i++)
                    { Console.Write("Value with power {0}: ", i);
                        coe[i] = int.Parse(Console.ReadLine());
                        y1 += (float)(coe[i] * Math.Pow(x1, i));
                        y2 += (float)(coe[i] * Math.Pow(x2, i));
                    }
                    //determine negative
                    if ((y1 * y2) < 0)
                    {  Console.WriteLine("There is a root btw intervals");
                        while (true)
                        {//midpoint of line
                            x0 = ((x1 * y2) - (x2 * y1)) / (y2 - y1);
                            for (int i = 0; i < coe.Length; i++)
                            { y0 += (float)(coe[i] * Math.Pow(x0, i)); }
                            if (y0 * y1 < 0)//check for -ve product
                            {  x2 = x0; }
                            else {x1 = x0; }
                            //approximate y to 6 decimal places
                            y0 = (float)Math.Round(y0, 6);
                            //if y0=0 therefore root is x0
            if (y0 == 0) { Console.WriteLine("Root is {0}", x0); break; }
      y0 = 0;//reset y0 back to zero in order to avoid accumulation of result
                        }
                    }
        else  { Console.WriteLine("There is NO root btw intervals"); }
                }
                catch { Console.WriteLine("\a\a\a Error \nInvalid Input"); } Console.Write("\nDo you wish to try again, type \"Yes\" or otherwise to quit: ");
                retry = Console.ReadLine().ToUpper();
            } while (retry == "YES");
        }
}

}


Happy CODING!!!

Thursday, 6 August 2015

CREATE A SIMPLE SIMULTANEOUS EQUATION CALCULATOR WITH C#


Hello guys first what is a simultaneous equation: This involves the calculation of more than one equation with unknowns simultaneously.








Back to c-sharp programming, first you need to solve the equation in terms of its constants as shown below

































Just like a pseudocode…..


Here is the code


using System;

namespace Simultaneous_Equation
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Simultaneous Equation Solver");
//prints the message in the quote
            string choice;
            do
            {

/*(multi line comment) a do loop= runs the code in the block and then checks if the condition in the while loop is met to run again else it breaks out of the loop*/

                try
                {
//try and catch looks for exception and the catches it, the prints the //message in the catch block.
                    Console.WriteLine("Input in the format Ax + By = C");
                    Console.Write("x1= ");
                    int A = int.Parse(Console.ReadLine());
                    Console.Write("y1= ");
                    int B = int.Parse(Console.ReadLine());
                    Console.Write("c1= ");
                    int C = int.Parse(Console.ReadLine());
                    Console.Write("x2= ");
                    int D = int.Parse(Console.ReadLine());
                    Console.Write("y2= ");
                    int E = int.Parse(Console.ReadLine());
                    Console.Write("c2= ");
                    int F = int.Parse(Console.ReadLine());
                    float x, y;
// equate the variable in terms of the constant
                    y = ((A * F) - (C * D)) / ((A * E) - (B * D));
                    if (A == 0) { x = (F - (E * y)) / D; }
                    else { x = (C - (B * y)) / A; }
                    Console.WriteLine("The value of x = {0}\nThe value of y = {1}", x, y);
                }
                catch { Console.WriteLine("Input is either wrong or not a simultaneous equation"); }
                Console.WriteLine(" TRY AGAIN?? Type yes else quit");
                choice = Console.ReadLine().ToLower();
            } while (choice == "yes");
            Console.ReadKey(true);
        }
    }
}





Try with many other equations or visit me  here or on youtube for more