Thứ Năm, 14 tháng 7, 2016

Yêu cầu: Viết chương trình Console Application. Tính tổng của n số nhập từ bàn phím. Tìm giá trị lớn nhất, giá trị nhỏ nhất trong n số đó.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int n, s = 0;
            int[] a;
            Console.Write("Nhap so can tinh tong: ");
            n = int.Parse(Console.ReadLine());
            a = new int[n];
            for (int i = 0; i < n; i++)
            {
                Console.Write("Nhap phan tu thu {0}: ", i + 1);
                a[i] = int.Parse(Console.ReadLine());
            }
            for (int i = 0; i < n; i++)
            {
                s += a[i];
            }
            for (int i = 0; i < n - 1; i++)
                for (int j = i; j < n; j++)
                    if (a[i] <= a[j])
                    {
                        int tg = a[i];
                        a[i] = a[j];
                        a[j] = tg;
                    }
            Console.WriteLine("Tong cua {0} so nhap tu ban phim la: {1}", n, s);
            Console.WriteLine("Gia tri lon nhat: {0}", a[0]);
            Console.WriteLine("Gia tri nho nhat: {0}", a[n - 1]);
            Console.ReadLine();

        }
    }
}

Không có nhận xét nào:

Đăng nhận xét