對于C語言這門計算機語言,算是老生常談了,現(xiàn)在就由我來給大家分享幾個關于C語言的幾個代碼吧!(如果有錯的地方還望見諒)
1.入門級別hello趴久,world
#include<stdio.h>
int main()
{
printf("Hello,world!\n");
return 0;
}
2.求素數(shù)
#include <stdio.h>
#include <math.h>
int isprime( int n) //n是否是素數(shù)
{ int i,k=sqrt(n);
if (n<=1) return 0;
for ( i=2 ; i<=k; i++ )
if ( a%i == 0) return 0 ;
return 1 ;
}
void main( )
{ int x=35;
if ( isprime(x))
printf("%d 是素數(shù)。\n", x );
else printf("%d 不是素數(shù)搔确。\n", x );
}
3.求大數(shù)
#include <stdio.h> //形參和實參所占內(nèi)存空間不同
int maxnum (int a, int b)
{int max;
if (a>b) max= a;
else max= b;
return max;
}
void main( )
{int x,y,z;
printf("input two numbers:\n");
scanf("%d%d",&x,&y);
z=maxnum(x,y);
printf("maxmum=%d",z);
}
4.求最大公約數(shù)
#include<stdio.h>
int gongyue(int m,int n) /*輾轉(zhuǎn)相除法求最大公約數(shù)*/
{ int r;
if(m==n) return m;
else
while((r=m%n)!=0)
{ m=n;
n=r;
}
return n;
}
5.2個字符串的連接
#include<stdio.h>
int strccat(char a[],char b[])
{
? int i,j;
? for(i=0;a[i];i++);
? for(j=0;a[j+i]=b[j];j++);
}
int main()
{
? char a[100],b[100];
? gets(a);
? gets(b);?
? strccat(a,b);
? puts(a);
?
}
好了彼棍,以上的分享就到此為止了。