经典C#例子.doc
文本预览下载声明
1
//(2)编写程序,输出100以内个位数为6且能被3整除的所有的数。
using System;
using System.Collections.Generic;
using System.Text;
namespace exercise
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i 100; i++)
if ((i % 10 == 6) (i % 3 == 0))
Console.WriteLine(i);
}
}
}
7
//(3)编写一个简单的计算器程序,能够根据用户从键盘输入的运算指令和整数,进行简单的加减乘除运算。
using System;
using System.Collections.Generic;
using System.Text;
namespace exercise3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(请输入两个运算数字:);
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(请输入运算符号:);
char s = Convert .ToChar (Console .ReadLine());
switch (s)
{
case +: Console.WriteLine(a + + + b + ={0}, a + b); break;
case -: Console.WriteLine(a + - + b + ={0}, a - b); break;
case *: Console.WriteLine(a + * + b + ={0}, a * b); break;
case /: Console.WriteLine(a + / + b + ={0}, (Single )a / b); break;
default: Console.WriteLine(输入的运算符有误!); break;
}
}
}
}
8//(4)合数就是非素数,即除了1和它本身之外还有其他约数的正整数。
//编写一个程序求出指定数据范围(假设10~100)内的所有合数。
using System;
using System.Collections.Generic;
using System.Text;
namespace exercise4
{
class Program
{
static void Main(string[] args)
{
for(int i =10;i=100;i++)
for(int j =2;ji/2;j++)
if (i % j == 0)
{
Console.Write(i);
Console.Write( );
break;
}
}
}
}
9
//(1)定义两个方法,分别求两个正整数的最大公约数和最小公倍数。
//其中,最大公约数的计算采用辗转相除法;最小公倍数的计算采用先计算最大公约数,
//然后再用两个数的积去除最大公约数来求得。
//在Main方法中实现两个待求正整数的输入及结果的输出。
using System;
using System.Collections.Generic;
using System.Text;
namespace exerc
显示全部