Saturday, July 19, 2014

C#: Enter the three numbers. Check to see that there is 3 sides of a triangle are not. If that is right, then see what kind of triangle.

//Enter the number 3. Check to see that there is 3 sides of a triangle are not. If that is right, then see what kind of triangle.
Source code:
class Program{
        static void Main() {
            float x, y, z;
            Console.Write("Input x: ");
            x = float.Parse(Console.ReadLine());
            Console.Write("Input y: ");
            y = float.Parse(Console.ReadLine());
            Console.Write("Input z: ");
            z = float.Parse(Console.ReadLine());
            if (x <= 0 || y <= 0 || z <= 0 || x + y <= z || y + z <= x || x + z <= y)
                Console.WriteLine("Input wrong !");
            else {
                if ((x == y && x * x * 2 == z * z) || (x == z && x * x * 2 == y * y) || (y == z && y * y * 2 == x * x))
                    Console.WriteLine("Triangle Square Scales");
                else {
                    if (x == y && y == z && z == x)
                        Console.WriteLine("Equilateral triangle");
                    else {
                        if (x == y || y == z || z == x)
                            Console.WriteLine("Isosceles triangle");
                        else
                            Console.WriteLine("Triangle");
                    }
                }
            }
            Console.ReadLine();
        }
}

Sunday, July 13, 2014

C#: Enter the integer n has 4 digits. Looking for big numbers second.

//Enter the integer n has 4 digits. Looking for big numbers second.
//Example: n=2071. The number big number second is 2
Source code:
class Program{
        static void Main(){           
            int n;
            int[] arr = new int[4];
            do
            {
                Console.Write("Input n has 4 digits: ");
                n = int.Parse(Console.ReadLine());
            } while (n < 1000 || n > 9999);           
            for (int i = 0; i < 4; i++){
                arr[i] = n % 10;
                n = n / 10;
            }
            for (int i = 0; i < 3; i++)
                for (int j = i+1; j < 4;j++)
                    if (arr[i] => arr[j]){
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }            
            Console.WriteLine("The second big number is: {0}", arr[2]);   
            Console.ReadLine();
        }
}

C#: Calculate the sum of digits of N have 8 digits

/*  Enter an integer n has 8 digits. Take a look: 
- The total value of the digits 
- The value of the total unit 
- The average of the total   */
Source code:
class Program{
static void Main(){
            int n;
            Console.Write("Input n has 8 digits: ");
            n = int.Parse(Console.ReadLine());
            //The total value of the digits
            int s = 0;
            int balance;
            int first = n;
            while (n != 0) {
                balance = n % 10;
                s += balance;
                n /= 10;
            }
            //The value of the total unit
            int unit;
            unit = s % 10;
            //The average of the total
            double avg;
            avg = (double) s / 8;
            Console.WriteLine("The total value of the digits {0} is: {1}",first,s);
            Console.WriteLine("The value of the total unit {0} is: {1}",s,unit);
            Console.WriteLine("The average of the total {0} is: {1}",s,avg);
            Console.ReadLine();
        }
}

C#: Check Prime Number !

//Write program with Console Application type, enter the number N from the keyboard. Check whether n is prime no.
Source code:
class Program{
        static void Main(string[] args)
        {
            int n;
            bool prime = true;
            Console.Write("Input n: ");
            n = int.Parse(Console.ReadLine());           
            if (n <= 1) prime = false;
            else {
                if (n == 2) prime = true;
                for (int i = 2; i <= Math.Sqrt(n); i++)
                    if (n % i == 0) { prime = false; break; }
                    else prime = true;
            }
            if (prime == true) Console.WriteLine("{0} is prime no",n);
            else Console.WriteLine("{0} is not prime number", n);
            Console.ReadLine();
        }

    }