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();

        }
    }
}

输出结果

在这里插入图片描述

目录
相关文章
|
5月前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
1月前
|
存储 C# 索引
C# 一分钟浅谈:数组与集合类的基本操作
【9月更文挑战第1天】本文详细介绍了C#中数组和集合类的基本操作,包括创建、访问、遍历及常见问题的解决方法。数组适用于固定长度的数据存储,而集合类如`List<T>`则提供了动态扩展的能力。文章通过示例代码展示了如何处理索引越界、数组长度不可变及集合容量不足等问题,并提供了解决方案。掌握这些基础知识可使程序更加高效和清晰。
61 2
|
1月前
|
C# 数据安全/隐私保护
C# 一分钟浅谈:类与对象的概念理解
【9月更文挑战第2天】本文从零开始详细介绍了C#中的类与对象概念。类作为一种自定义数据类型,定义了对象的属性和方法;对象则是类的实例,拥有独立的状态。通过具体代码示例,如定义 `Person` 类及其实例化过程,帮助读者更好地理解和应用这两个核心概念。此外,还总结了常见的问题及解决方法,为编写高质量的面向对象程序奠定基础。
16 2
|
2月前
|
C#
C#中的类和继承
C#中的类和继承
36 6
|
2月前
|
Java C# 索引
C# 面向对象编程(一)——类
C# 面向对象编程(一)——类
29 0
|
2月前
|
开发框架 .NET 编译器
C# 中的记录(record)类型和类(class)类型对比总结
C# 中的记录(record)类型和类(class)类型对比总结
|
4月前
|
开发框架 .NET 编译器
程序与技术分享:C#基础知识梳理系列三:C#类成员:常量、字段、属性
程序与技术分享:C#基础知识梳理系列三:C#类成员:常量、字段、属性
28 2
|
4月前
|
C#
C# 版本的 计时器类 精确到微秒 秒后保留一位小数 支持年月日时分秒带单位的输出
这篇2010年的文章是从别处搬运过来的,主要包含一个C#类`TimeCount`,该类有多个方法用于处理时间相关的计算。例如,`GetMaxYearCount`计算以毫秒为单位的最大年数,`GetCurrentTimeByMiliSec`将当前时间转换为毫秒,还有`SecondsToYYMMDDhhmmss`将秒数转换为年月日时分秒的字符串。此外,类中还包括一些辅助方法,如处理小数点后保留一位数字的`RemainOneFigureAfterDot`。
|
4月前
|
存储 安全 C#
C# 类的深入指南
C# 类的深入指南
|
5月前
|
C#
C#的类和对象的概念学习案例刨析
【5月更文挑战第17天】C#是一种面向对象的语言,以类和对象为核心。类作为对象的模板,定义了属性(如Name, Age)和行为(如Greet)。对象是类的实例,可设置属性值。封装通过访问修饰符隐藏实现细节,如Customer类的私有name字段通过Name属性访问。继承允许新类(如Employee)从现有类(Person)继承并扩展。多态让不同对象(如Circle, Square)共享相同接口(Shape),实现抽象方法Area,提供灵活的代码设计。
63 1