Giter Site home page Giter Site logo

c-program's People

c-program's Issues

/打印1到n的阶乘

#include <stdio.h>//打印1到n的阶乘
#define size 10

int fact(int n)//打印n的阶乘,
{
if (n>0)//函数递归
{
return n*fact(n-1);
}
else
return 1;
}
int calnumber(int nums[],int n)
{
int i;
if (n>0 && n<size+1)
{
for ( i = 0; i < n; ++i)
{
nums[i]=fact(i+1);//调用fact 函数,将阶乘存在数组中
}
return 1;
}
else
return 0;
}
int main(int argc, char const *argv[])
{
int nums[size];
int i,n;
scanf("%d",&n);
if(!calnumber(nums,n))
{
printf("illegal number!\n");
return 1;
}
else
{
for ( i = 0; i < n; ++i)
{
printf("%d\n",nums[i] );
}
}
return 0;
}

十进制转化二进制

//十进制数转换成二进制数输出
#include <stdio.h>

void d2b(unsigned int n)
{
if (n==0 || n==1)
{
putchar('\n');
}
else

{
	d2b(n/2);//除以二
	printf("%d\n",n%2 );//除以二取余
}

}
int main()
{
int n;
printf("input number:\n");
scanf("%d",&n);
d2b(n);
return 0;
}

fibonaci数列前n项

//编程输出fibonaci数列前n项
#include <stdio.h>

void fib(int n,int i,int i1,int i2)
{
if (i==2)
{
printf("1,\t1");
}
if (i<n)
{
printf(",\t%d",i1+i2 );
fib(n,i+1,i1+i2,i1);

}
else
	printf("\n");

}
int main(int argc, char const *argv[])
{
int n;
printf("input numner:\n");
scanf("%d",&n);
fib(n,2,1,1);
return 0;
}

用递归方法,求数组元素的加和

#include <stdio.h>//#include <stdio.h>用递归方法,求数组元素的加和

int sum(int nums[],int n)
{
int sum2;
if(n>1)
sum2 = nums[n-1]+sum(nums,n-1);//n-1号值加到sum2,
else
sum2 = nums[0];
return sum2;
}
int main()
{
int nums[3]={1,2,3};
int sum1;
sum1 = sum(nums,3);//3指的是个数,不是标号!!!!!!!!
printf("%d\n",sum1 );
return 0;
}

int sum(int nums[],int n)
{
int sum2;
if(n>1)
sum2 = nums[n-1]+sum(nums,n-1);//n-1号值加到sum2,
else
sum2 = nums[0];
return sum2;
}
int main()
{
int nums[3]={1,2,3};
int sum1;
sum1 = sum(nums,3);//3指的是个数,不是标号!!!!!!!!
printf("%d\n",sum1 );
return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.