C#求完全数,输出水仙花以及类的使用

简介: 求完全数,输出水仙花以及类的使用

完全数

如果一个正整数等于除它本身之外其他所有除数之和,就称之为完全数。

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

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            for(int i = 1; i < 1000; i++)
            {
                int sum = 0;
                for(int j = 1; j < i; j++)
                {
                    if (i % j == 0)
                    {
                        sum += j;
                    }
                 }
                if (sum == i)
                {
                    Console.WriteLine(i);
                }
               
            }
            Console.ReadKey();
        }
    }
}

结果

在这里插入图片描述
以前以内的完全数 6 28 496

输出所有水仙花数

水仙花数是三位数的数,其各位数字的立方和等于该数的本身

代码

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

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            for(int i = 100; i < 1000; i++)
            {
                a = i / 100;
                b = i / 10 %10;
                c = i % 10;
                if (a * a * a + b * b * b + c * c * c == i)
                {
                    Console.WriteLine(i);
                }
            }
            Console.ReadKey();
        }
    }
}

结果

在这里插入图片描述
水仙花数为 153 370 371 407

类的使用

代码

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

namespace ConsoleApp6
{
    class Student
    {
        public string name;
        public string sex;
        public int age;
        public Student(string name, string sex)
        {

        }
        public Student(string name, string sex,int age)
        {
            this.name = name;
            this.sex = sex;
            this.age = age;

        }
        public void Inderduce()
        {
            Console.WriteLine("我叫"+this.name+"我是一个"+this.sex+"孩,我今年"+this.age+"岁");

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student s1 = new Student("小华","男",20);
            Student s2 = new Student("小花","女",18);
            s1.Inderduce();
            s2.Inderduce();
            Console.ReadKey();

        }
    }
}

输出结果

在这里插入图片描述

目录
相关文章
|
1月前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
1月前
|
监控 C#
55.c#:file类
55.c#:file类
82 1
|
1月前
|
算法 C#
54.c#:random类
54.c#:random类
96 1
|
1月前
|
C#
58.c#:directory类
58.c#:directory类
90 0
|
1月前
|
C#
57.c#:directorylnfo类
57.c#:directorylnfo类
22 0
|
15天前
|
C#
C# 版本的 计时器类 精确到微秒 秒后保留一位小数 支持年月日时分秒带单位的输出
这篇2010年的文章是从别处搬运过来的,主要包含一个C#类`TimeCount`,该类有多个方法用于处理时间相关的计算。例如,`GetMaxYearCount`计算以毫秒为单位的最大年数,`GetCurrentTimeByMiliSec`将当前时间转换为毫秒,还有`SecondsToYYMMDDhhmmss`将秒数转换为年月日时分秒的字符串。此外,类中还包括一些辅助方法,如处理小数点后保留一位数字的`RemainOneFigureAfterDot`。
|
1月前
|
C#
C#的类和对象的概念学习案例刨析
【5月更文挑战第17天】C#是一种面向对象的语言,以类和对象为核心。类作为对象的模板,定义了属性(如Name, Age)和行为(如Greet)。对象是类的实例,可设置属性值。封装通过访问修饰符隐藏实现细节,如Customer类的私有name字段通过Name属性访问。继承允许新类(如Employee)从现有类(Person)继承并扩展。多态让不同对象(如Circle, Square)共享相同接口(Shape),实现抽象方法Area,提供灵活的代码设计。
45 1
|
1月前
|
SQL 存储 Oracle
C# Web控件与数据感应之 Control 类
C# Web控件与数据感应之 Control 类
|
1月前
|
SQL 存储 C#
C# Web控件与数据感应之 TreeView 类
C# Web控件与数据感应之 TreeView 类
|
1月前
|
SQL 存储 Oracle
C# Web控件与数据感应之 CheckBoxList 类
C# Web控件与数据感应之 CheckBoxList 类