初学编程100个代码大全c语言(C语言初学者代码)

http://www.itjxue.com  2023-02-12 15:27  来源:未知  点击次数: 

最简单的c语言编程

很多程序都是非常简单的:

1.输入2个正整数m和n,求其最大公约数和最小公倍数

#includestdio.h

#includemath.h

int main()

{

int m,n,p,q,s,r;

printf("请输入两个正整数;m,n\n");

scanf("%d,%d",m,n);

s=m*n;

if(mn)

{

p=m;

m=n;

n=p;

}

printf("%d\t%d\n",m,n);/*按从大到小输出m,n */

while(n!=0)

{

q=m%n;

m=n;

n=q;

}

/*是不是m才是最大公约数啊*/

r=s/m;

printf("m和n最大公约数为 %d\n",m);

printf("m和n最小公倍数为 %d\n",r);/*m和n的积除以m和n的最大公约数即为最小公倍数*/

return 0;

}

2.输出100以内能被3整除且个位数为6的所有整数。

#includestdio.h

int main(void)

{

int i;

for(i=1;i=100;i++)

if(i%3==0i%10==6)printf("%d ",i);

return 0;

}

3. 编程计算从1到10各数阶乘的和,即1! + 2! + 3! + …… + 9! + 10! 的和

#includestdio.h

int main(void)

{

int sum,i,term;

sum=0;

term=1;

for(i=1;i=10;i++)

{

term=term*i;

sum=sum+term;

printf("%d的阶乘 %d\n",i,term);

}

printf("1到10各数阶乘的和 %d\n",sum);

return 0;

}

4.使用嵌套循环产生下列由大写字母组成的图

#includestdio.h

int main(void)

{

char ch1,ch2;

for(ch1='A';ch1='Z';ch1++)

{

for(ch2='A';ch2=ch1;ch2++)

printf("%c",ch2);

printf("\n");

}

return 0;

}

5. 编程输出所有三位数中的素数。

#includestdio.h

int main(void)

{

int x,i;

for(x=100;x=999;x++)

{

for(i=2;ix/2;i++)

if(x%i==0)break;

if(i==x/2)printf("%d ",x);

}

return 0;

}

6. 定义一个函数even(),判断一个整数是否是偶数。如果是偶数返回1,否则返回0。(要求包括能使程序正常运行的主函数)

#includestdio.h

int main(void)

{

int even(int);

int x,y;

scanf("%d",x);

y=even(x);

if(y==1)

printf("%d 是偶数\n",x);

else

printf("%d 是奇数\n",x);

return 0;

}

int even(int x)

{

if(x%2==0)

return 1;

else

return 0;

}

7. 编写函数mypow,求整型变量x的y次方。(要求包括能使程序正常运行的主函数)

#includestdio.h

int main(void)

{

int mypow(int,int);

int x,y,s;

scanf("%d%d",x,y);

s=mypow(x,y);

printf("%d的%d次方是 %d\n",x,y,s);

return 0;

}

int mypow(int x,int y)

{

int sum,i;

sum=1;

for(i=1;i=y;i++)

sum=sum*x;

return sum;

}

8.输入一个3位整数,输出它的逆序数。例如,输入127,输出应该是721。

#includestdio.h

int main(void)

{

int x,y;

scanf("%d",x);

y=x/100+x/10%10*10+x%10*100;

printf("%d的逆序数 %d\n",x,y);

return 0;

}

C语言编程代码

楼主的这个程序是想输入一个字符串,然后算出其中的小写字母个数、大写字母个数以及数字个数,对吧?修改如下:

#include"stdio.h"/*你这里差了头文件了,不过你如果是在WIN_TC下编译得就可以不用,不过还是养成习惯把头文件写进去的好*/

#define N 100/*定义字符串的最大长度,你可以自己定义别的大小*/

main()

{

int date=0,da=0,xiao=0;

int i=0;

char c[N]; /*字符串嘛,用数组最好了!*/

scanf("%s",c);/*键盘输入字符串,记得以“!”结尾哦*/

while(c[i]!='!')

{ if(c[i]='a'c[i]='z') /*这里省略了else语句,因为根本不需要*/

xiao++;

if(c[i]='A'c[i]='Z')

da++;

if(c[i]='0'c[i]='9')

date++;

i++;

}

printf("xiao=%d,da=%d,date=%d\n",xiao,da,date);

}

楼主如果觉得我答得不错得话,记得给我加分哦……

加油!

设计一个C语言程序 急!要求程序代码至少100条或由4个以上函数组成

随便写个程序(第二个的)

好玩而已,没必要给我分....

呵呵

还有你的要求是很花时间的(很复杂)

一般很少有人答的

#includestdio.h

#includestring.h

#includeconio.h

void getpw(char *in)

{

for(int i=0;;i++)

{

char a=getch();

if(a==13){in[i]=0;printf("\n");return;} /*a==13 输入"回车"结束*/

if(a==8){if(i==0)continue;else {i-=2;printf("\b \b");continue;}}/*a==8 输入"退格"退格*/

if(i==20){i--;continue;} /*最多输入20个字符*/

printf("*");

in[i]=a;

}

}

void main()

{

char *pw="8888";

char in[80];

int ok=0;

for(int i=0;i3;i++)

{

getpw(in); /*输入密码*/

if(strcmp(pw,in)==0)

{

ok=1;

break;

}

else

{

printf("Wrong passward!\n");

}

}

if(ok==0)printf("You have entered three times! You are not welcom!\n");

else printf("You are welcome!\n");

}

求最简单的C语言程序

#includestdio.h

main()

{

int a,b,t=0;

scanf("%d %d",a,b);

if (ab)

{

t=a;

a=b;

b=t;

}

printf("%d %d %d %d %d",(a+b),(a-b),(a/b),(a*b),(a%b));

}

C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。

简单C语言编程

1。#include iostream

using namespace std;

int main()

{

int i;

for(i=0;i=100;i++)

{

if(i%4==0i%9==0)

coutiendl;

}

return 0;

}

2。

#include iostream

using namespace std;

int main()

{int a,b,c,d;

cinabc;

d=c;

c=b;

b=a;

a=d;

coutaendlbendlcendl;

return 0;

}

3。#include fstream

#include iostream

using namespace std;

int main()

{

char a[90];

ofstream outfile("your_file.txt",ios::out);

cin.getline(a,90);

outfilea;

return 0;

}

4。

#include iostream

using namespace std;

int main()

{

char c[80];

cin.getline(c,80);

int i=0,j=0;

while(c[i]!='\0')

{

if(c[i]=65c[i]=90)

i++;

j++;

}

coutjendl;

return 0;

}

5。

#include iostream

using namespace std;

struct student

{

char name[50];

int score;

};

int main()

{

student a[5];

int i;

for(i=0;i=4;i++)

{

cina[i].namea[i].score;

}

FILE *fp;

fp=fopen("data1.txt","w");

for(i=0;i=4;i++)

{

fprintf(fp,"%s\n",a[i].name);

fprintf(fp,"%d\n",a[i].score);

}

fclose(fp);

return 0;

}

求3个100行左右的C语言程序 要代码 无限感谢!!!!

第1个,剪刀石头布,111行

#includeiostream

#includestring

#includectime

using namespace std;

int main()

{

string playStr; //玩家输入的选择

int playWin=0; //玩家赢

int cptWin=0; //电脑赢

int noWin=0; //平局

int cpt; //电脑出什么

int sum=0; //玩了几局

float win=0; //胜效率

begin:

sum=playWin+cptWin+noWin;

if(sum==0)

{

sum=1;

}

if(sum-noWin!=0)

{

win=(float)playWin/(float)(sum-noWin)*100;

}

else

{

win=0;

}

cout"游戏状态:"endlendl" 玩家赢:"playWin

" 电脑赢:"cptWin

" 平局:"noWin" 总局数:"sum" 胜率:"win

"%"endlendl;

cout"请出拳(1-剪刀 2-石头 3-布 Q-退出)"endl;

cinplayStr;

srand(time(0));

cpt=rand()%3+1;

if(cpt==1) //电脑出 剪刀

{

cout"电脑出剪刀"endl;

if(playStr[0]=='1')

{

cout"玩家出剪刀,平局."endl;

noWin++;

}

else if(playStr[0]=='2')

{

cout"玩家出石头,玩家赢."endl;

playWin++;

}

else

{

cout"玩家出布,玩家输."endl;

cptWin++;

}

}

else if(cpt==2)

{

cout"电脑出石头"endl;

if(playStr[0]=='1')

{

cout"玩家出剪刀,玩家输."endl;

cptWin++;

}

else if(playStr[0]=='2')

{

cout"玩家出石头,平局."endl;

noWin++;

}

else

{

cout"玩家出剪布,玩家赢."endl;

playWin++;

}

}

else

{

cout"电脑出布"endl;

if(playStr[0]=='1')

{

cout"玩家出剪刀,玩家赢."endl;

playWin++;

}

else if(playStr[0]=='2')

{

cout"玩家出石头,玩家输."endl;

cptWin++;

}

else

{

cout"玩家出布,平局."endl;

noWin++;

}

}

if(playStr[0]=='q'||playStr[0]=='Q')

{

return 0;

}

else

{

getchar();

getchar();

system("cls");

goto begin;

}

}

第2个:还是111行

#includeiostream

using namespace std;

bool Is(long num);

void Print(long i, long num1, long num2);

int main()

{

long m,m1,m2;

long a,sum;

long i1=0;

begin:

cout"请输入要分解的数字:"endl;

cinm;

if(m1)

{

goto begin;

}

m2=m;

coutendl;

cout"----------------------------------------"endl;

m1=m;

cout" "mendl;

if(Is(m))

{

cout" / \\"endl;

cout" ""1"" "mendl;

}

else

{

for(long i=2; im; )

{

if( (i%2!=0) || (i==2 ) Is(i))

{

a=m%i;

if(a==0)

{

m/=i;

sum*=i;

Print(i1,i,m);

i1++;

if(sum==m1 || (Is(m)) )

{

i1=0;

break;

}

}

else

{

i++;

}

}

else

{

i++;

}

}

}

coutendl"----------------------------------------"endlendl;

return ;

}

bool Is(long num)

{

long m=0;

bool is=false;

if(num==2)

{

return true;

}

if(num%2!=0)

{

m=num+1;

m/=2;

}

else

{

return false;

}

long i;

for( i=2; i=m; i++)

{

if(num%i!=0)

{

is=true;

}

else

{

is=false;

break;

}

}

return is;

}

void Print(long i, long num1, long num2)

{

for(long j=0; j=i+2; j++)

{

cout" ";

}

cout" / \\"endl;

for( j=0; j=i+2; j++)

{

cout" ";

}

cout" "num1" "num2endl;

}

第3个,统计字符串的个数,这个200多行

#include iostream

#include string

#include iomanip

using namespace std;

struct WordNode

{

int num;

char word[3];

WordNode* pNext;

};

WordNode *pHead;

void Fun(char* str)

{

WordNode* pNew= new WordNode;

pNew-pNext = NULL;

char ch[3];

begin:

for(int i=0; istrlen(str); i++)

{

memset( ch, 0, 3);

if( str[i] = 0 str[i] =127 )

{

ch[0] = str[i];

ch[1] = ' ';

}

else

{

ch[0] = str[i];

ch[1] = str[++i];

}

WordNode* head = pHead;

bool flag = false;

if ( !pHead)

{

pNew -num = 0;

strcpy( pNew -word, ch);

pHead = pNew ;

goto begin;

}

while( head )

{

if ( !strcmp( ch, head-word))

{

flag = true;

break;

}

head = head -pNext;

}

if ( flag == true )

{

head -num ++;

}

else

{

pNew = new WordNode;

pNew -num = 1;

strcpy( pNew -word, ch);

pNew -pNext = NULL;

for( WordNode* loop = pHead; loop-pNext; loop= loop-pNext)

{

}

loop -pNext = pNew;

}

}

}

void Print(WordNode* pFirst)

{

cout endl "统计结果:" endl endl;

int maxNum = -1;

int wordNum = 0;

WordNode* head = pFirst;

while ( head )

{

if( maxNum head-num )

{

maxNum = head-num;

}

wordNum ++;

head = head-pNext;

}

head = pFirst;

int num = 0;

cout "-----------------------------------------------------------------" endl;

for( int i=0; iwordNum; i++)

{

num = head-num;

cout head-word " : " ;

for(int j=0; jmaxNum; j++)

{

if( head -num 0 )

{

cout "*" ;

}

else

{

cout " " ;

}

head-num --;

}

cout setw( maxNum + 2 ) num ;

head-num = num;

head = head-pNext;

cout endl;

}

cout "-----------------------------------------------------------------" endl;

head = pFirst;

int layer = maxNum;

while (head)

{

cout head-num setw( 2 ) ;

head = head-pNext;

}

cout endl;

head = pFirst;

for(int j=0; jmaxNum; j++)

{

WordNode* node = head;

for( int i=0; iwordNum; i++)

{

if( layer == node-num )

{

cout "* " ;

node-num --;

}

else

{

cout " " ;

}

node = node-pNext;

}

layer --;

cout endl;

}

head = pFirst;

while( head )

{

cout head-word ;

head = head -pNext;

}

cout endl "-----------------------------------------------------------------" endl;

}

void DeleteWordNode(WordNode* pFirst)

{

WordNode* cp, *np;

cp = pFirst;

while ( cp)

{

np = cp-pNext;

delete cp;

cp = np;

}

pFirst = NULL;

}

int main()

{

char *str = new char[10000];

try

{

if ( str == NULL)

{

throw(str);

}

cout "输入要统计的字符串: " endl;

cin.getline( str, 10000 );

Fun(str);

Print(pHead);

}

catch( char* )

{

cout "动态申请空间发生错误,程序自动退出." endl;

exit( 1 );

}

DeleteWordNode(pHead);

delete []str;

getchar();

return 0;

}

这些都自己写的小程序,你可以看下

(责任编辑:IT教学网)

更多

推荐安全产品文章