使用 C 语言判断给定的数字是否是完全数的程序
完全数是指其因数之和等于该数的两倍的数。
算法
算法解释如下:-
START Step 1: declare int variables and initialized result=0. Step 2: read number at runtime. Step 3: for loop i=1;i<=number;i++ Condition satisfies i. if(number%i==0) ii. result=result+i; Step 4: checking the sum of factors. i. if(result==2*number) ii. print perfect number iii. else print not perfect number STOP
程序
以下是用于判断给定数字是否是完全数的 C 语言程序:-
#include<stdio.h> int main(){ int number,i,result=0;//declare variables and initialize result to 0 printf("enter the number:"); scanf("%d",&number); for(i=1;i<=number;i++){ if(number%i==0) result=result+i; } if(result==2*number) //checking the sum of factors==2*number printf("perfect number"); else printf("not perfect number"); }
输出
输出如下:-
enter the number:28 perfect number enter the number:46 not perfect number
广告