C#编程练习

本文档展示了几个C#代码示例,涉及数字处理的不同方面。包括检查水仙花数、数字拆解、数字求和、数字倒序输出及验证回文数。这些示例涵盖了数字的计算、条件判断以及输入输出操作,是C#初学者理解基本编程概念的良好实践。
摘要由CSDN通过智能技术生成

 编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {
            //求水仙花数 m=x*100+y*10z=x*x*x+y*y*y+z*z*z  999>=m>=100 x=m/100 y=(m%100)/10  z=((m%100)%10)
            Console.WriteLine("请输入数值:");
            int m = Convert.ToInt32(Console.ReadLine());
            if (m >= 100 && m <= 999)
            {
                int x = m / 100;
                int y = (m % 100) / 10;
                int z = ((m % 100) % 10);
                int temp = x * x * x + y * y * y + z * z * z;
                if (m == temp)
                    Console.WriteLine(m + "是水仙花数");
                else
                    Console.WriteLine(m + "不是水仙花数");
            }
            else
                Console.WriteLine("请输入一个有效的三位数!");

            Console.ReadKey(true);
        }
    }
}

程序运行结果如下:

 

 编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {
            //拆解数字 从个位数开始输出

            //求水仙花数 m=x*100+y*10z=x*x*x+y*y*y+z*z*z  999>=m>=100 x=m/100 y=(m%100)/10  z=((m%100)%10)
            Console.WriteLine("请输入一个整数:");
            int m = Convert.ToInt32(Console.ReadLine());
          while (m>=10)
            {
               int k= m % 10;

                Console.WriteLine(k);
                m /= 10;
            }
            Console.WriteLine(m);

            Console.ReadKey(true);
        }
    }
}

程序运行结果如下:

 

 编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {
          

            int c = 1,sum=0;
            Console.WriteLine("请输入一个整数:");
            int m = Convert.ToInt32(Console.ReadLine());
          while (m!=0)
            {
               int k= m % 10;
                if(k!=0)
                {
                    k *= c;
                    sum += k;
                    c *= 10;
                }
                m = m / 10;
            }
          
            Console.WriteLine(sum);

            Console.ReadKey(true);
        }
    }
}

程序运行结果如下:

 编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {
          

            int c = 1,sum=0;
            Console.WriteLine("请输入一个整数:");
            int m = Convert.ToInt32(Console.ReadLine());
          while (m!=0)
            {
               int k= m % 10;
                if(k!=0)
                {
                    Console.Write(k);
                }
                m = m / 10;
            }
          
           

            Console.ReadKey(true);
        }
    }
}

程序运行结果如下:

 编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {
          

          
            Console.WriteLine("请输入一个整数:");
            int m = Convert.ToInt32(Console.ReadLine());
            int t = m;
            int num=0;//采用倒序相加 然后每次循环乘以10的办法
          while (m!=0)
            {
               int k= m % 10;
                num *= 10;
                num += k;
               
               
                m /= 10;
            }
          
            Console.WriteLine("倒数为:"+num);

            if (t == num)
                Console.WriteLine("是回文数");
            else
                Console.WriteLine("不是回文数");
            Console.ReadKey(true);
        }
    }
}

程序运行结果如下:

 编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {
            char c;
            int sum = 0;
            do
            {
                c = (char)Console.Read();
                if (c >= '0' && c <= '9')
                {
                    int num = c - '0';
                    sum += num;
                }
            }
            while (c != '@');
            Console.WriteLine("结果为:" + sum);
            Console.ReadKey(true);
        }
    }
}

程序运行结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值