题目一:编程求s=1+2+3+4+......+99+100的和。
using System;
namespace zuoye21
{
class Program
{
static void Main(string[] args)
{
int n = 0;
int s = 0;
while(n<100)
{
n++;
s += n;
}
Console.WriteLine("s=1+2+3+4+......+99+100的和为{0}",s);
Console.ReadKey();
}
}
}
题目二:试编程,从键盘任意输入一个整数给变量x,计算并输出分段函数y的值:
y=2x+1 x≥1
y=3x/(x-1) x<1
using System;
namespace zuoye22
{
class Program
{
static void Main(string[] args)
{
double y;
Console.Write("请输入一个整数x:");
double x = double.Parse(Console.ReadLine());
if(x>=1)
{
y = 2 * x + 1;
Console.WriteLine("当x>=1时,y={0}", y);
}
else
{
y = 3 * x / (x - 1);
Console.WriteLine("当x<1时,y={0}", y);
}
Console.ReadLine();
}
}
}
题目三:从键盘输入一个三位数十进制整数,输出并打印该数的个位、十位、百位数。
using System;
namespace zuoye23
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个三位数十进制整数:");
int x = Convert.ToInt32(Console.ReadLine());
int a, b, c;
a = x % 10;
b = x /10%10;
c = x / 100;
Console.WriteLine("该数的个位为:{0},十位为{1},百位为{2}",a,b,c);
Console.ReadLine();
}
}
}
题目四:编程求s=1-2+3-4+......+99-100的和。
using System;
namespace zuoye24
{
class Program
{
static void Main(string[] args)
{
int s, x, y;
int s1 = 0;
int s2 = 0;
for (x=1;x<100;x+=2)
{
s1 += x;
}
for (y = 2; y < 101; y += 2)
{
s2 += y;
}
s = s1 - s2;
Console.WriteLine("s=1-2+3-4+......+99-100的和为{0}", s);
Console.ReadLine();
}
}
}