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

编程入门 行业动态 更新时间:2024-10-07 14:21:44

C#求完全数,输出<a href=https://www.elefans.com/category/jswz/34/1765352.html style=水仙花以及类的使用"/>

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

完全数

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

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

代码

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

输出所有水仙花数

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

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

代码

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

输出结果

更多推荐

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

本文发布于:2024-02-05 11:53:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1744996.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:水仙花

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!