.net作业2

题目一:编程求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();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值