当前位置:网站首页>C # find perfect numbers, output daffodils and use of classes
C # find perfect numbers, output daffodils and use of classes
2022-07-18 23:00:00 【Nanpengyou】
Perfect number
If a positive integer is equal to the sum of all divisors except itself , Call it a perfect number .
Perfect number
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);
}
Code
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();
}
}
}
result

Previous perfect numbers within 6 28 496
Output all Narcissus
The number of daffodils is a three digit number , The cube sum of its digits is equal to the number itself
daffodils
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);
}
Code
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();
}
}
}
result

The number of daffodils is 153 370 371 407
The use of the class
Code
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(" My name is "+this.name+" I am a "+this.sex+" Child , I this year "+this.age+" year ");
}
}
class Program
{
static void Main(string[] args)
{
Student s1 = new Student(" Xiaohua "," male ",20);
Student s2 = new Student(" floret "," Woman ",18);
s1.Inderduce();
s2.Inderduce();
Console.ReadKey();
}
}
}
Output results

边栏推荐
- Mysql相关命令
- Huada 110 clock calibration
- What if the work of product evaluators is repetitive and cumbersome? Can it be automated?
- 华大110时钟校准
- Chapter 3 business function development (check the details of market activities)
- 北大&微软联合提出超强时间序列表示学习框架,显著提升多项时间序列任务效果
- [development of large e-commerce projects] cache distributed lock redisson concise & integration lock redisson resolve deadlock read / write lock lock lock semaphore-44
- NASA took the first clear picture of the moment after the big bang
- Remove the k-bit number [greedy thought & monotonic stack implementation]
- 10分钟自定义搭建行人分析系统,检测跟踪、行为识别、人体属性All-in-One
猜你喜欢
刘小乐教授:我与生物信息学的不解之缘

溢流阀力士乐ZDB10VP2-4X/315V

Leetcode48. rotate image

Parker Parker proportional valve d1fve50bcvlb

Pytorch installation (very detailed)

基于多数据源零代码同时生成多个数据库CRUD增删改查RESTful API接口——MySql,PostgreSql,Oracle,Microsoft SQL Server多数据源

Problem solving -- > online OJ (16)

Database design pattern of multi tenant SaaS

3D point cloud course (I) -- Introduction to point cloud Foundation

20220714 adapt openharmony-v3.1-beta to aio-3568j (compile builderoot)
随机推荐
Peking University and Microsoft jointly proposed a super time series representation learning framework, which significantly improved the effect of multiple time series tasks
溢流阀力士乐ZDB10VP2-4X/315V
日志收集方案EFK
Kaggle file download (weights, inference files...)
3D point cloud course (II) -- nearest neighbor problem
416.分割等和子集·背包问题·动态规划
(板子)AcWing841. 字符串哈希
Why is SaaS so important for enterprise digital transformation?
Transfer function
Detailed explanation of bean's life cycle
PMP每日一练 | 考试不迷路-7.16
NPM common commands use commands to delete nodes_ Modules package
JS binary search pseudocode
输入一个url全过程详解
Which securities company has a low handling fee for opening an account, and which stock is safe to open an account
Modify server password
Be diligent in chatting
PMP practice once a day | don't get lost in the exam -7.16
【无标题】
员工管理系统编写过程中摘要