Find the number of factors of a number n (simple arithmetic) C language

Since the number n of the square root of both sides of the distribution factor of n, the number n of the square root can be simply, it should be noted that when the square root of n is an integer of the case.
Code is as follows: `

#include<stdio.h>
#include<math.h>
int slove(int n)
{
 int i,flag=0;
 int sum;
 for(i=1;i<=sqrt(n);i++)    /*对数字n开平方*/
 {
  flag=0;
  if(i*i==n)            /*开平方为整数时只能算一个因子*/ 
  {
   sum++;
   flag=1;
  }
  if(flag==0&&n%i==0)
  {
   sum+=2;
  }
 }
 return sum;
}
int main()
{
 int n;
 scanf("%d",&n);
 printf("%d\n",slove(n));
 return 0;
}
Released three original articles · won praise 0 · Views 97

Guess you like

Origin blog.csdn.net/m0_46070659/article/details/104265602