Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Tìm ước số chung của 2 số trong lập trình C# đây

    Mã:
    namespace ConsoleApplication{    class program    {        static void Main(string[] args)        {            tinh bt=new tinh();            bt.nhap();            Console.writeLine("uoc chung lon nhat cua 2 so la {0}",bt.tinh_bt());            Console.ReadLine();        }    }    class tinh    {        private int a,b;        public void nhap()        {            Console.Write("nhap a:");            a=Convert.ToInt32(Console.ReadLine());            Console.write("nhap b:");            b=Convert.ToInt32(Console.ReadLine());        }        public int tinh_bt()        {            while(a!=b)            {                if(a>b)                    a=a-b;                else                     b=b-a;            }            return a;        }    }}

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Kia là tìm ước số chung lớn nhất.
    dưới đây là đọn code tìm tất cả ước số chung
    ----------------------------------------------------------------------------------------------------


    Mã:
    using System;using System.Collections.Generic; namespace Mathematics{    class Program    {        static void Main(string[ ] args)        {             TimUSC bt = new TimUSC();            List<int> lisReturn = new List<int>();            lisReturn = bt.timUSC( lisReturn );                Console.WriteLine( "USC là: " );                foreach (int i in lisReturn)                {                    Console.Write( "   {0}", i );                }                Console.ReadLine();        }        class TimUSC        {            private int inta, intb;            public void nhap()            {                Console.Write( "nhap a:" );                inta = Convert.ToInt32( Console.ReadLine() );                Console.Write( "nhap b:" );                intb = Convert.ToInt32( Console.ReadLine() );            }            public List<int> timUSC(List<int> lisReturn)            {                nhap();                int intc = inta < intb ? intb : inta;                 for (int i = 1; i <= intc; i++)                {                    if (( inta % i == 0 ) && ( intb % i == 0 ))                        lisReturn.Add( i );                }                return lisReturn;            }        }     }}
    ----------------------------------------------------------------------------------------------------

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •