C語言程序
1. 九九乘法表(利用數組)
#include <stdio.h>
int main(void){
int a [9];
int c [9];
int x;
int y;
for(x=1;x<=9;x++){
a[x]=x;
}
for(y=1;y<=9;y++){
c[y]=y;
}
for(x=1;x<=9;x++){
for(y=1;y<=x;y++){
printf("%d * %d=%d",a[x],c[y],a[x]*c[y]);
}
printf("\n");
}
return 0;
}
2.水仙花數的實現
#include <stdio.h>
int main(void){
int a,b,c,sum;
for(a=1;a<=9;a++){
for(b=0;b<=9;b++){
for(c=0;c<=9;c++){
sum=a*100+b*10+c;
if(sum==a*a*a+b*b*b+c*c*c){
printf("%d\n",sum);
}
}
}
}
return 0;
}
3.一個銀行系統的密碼輸入,限制輸入次數(do ..while語句)
#include <stdio.h>
int main(void){
int password;
int count=0;
printf("您的密碼只能輸入三次,超過三次將被鎖住!\n");
do{
if(count<3){
printf("輸入您的密碼:\n");
scanf("%ld",&password);
printf("密碼錯誤,在想想\n");
}
else if(3==count){
printf("密碼錯誤,卡以被鎖!");
}
count++;
}
while(123456!=password);
printf("進入銀行系統!!");
}
4. 一個數加上100是一個平方數再加上168還是一平方數求這數。。
#include <stdio.h>
int main(void){
long int i,x,y;
for(i=1;i<100000;i++){
x=sqrt(i+100);
y=sqrt(i+268);
if(x*x==i+100&&y*y==i+268)
printf("\n%ld\n",i);
}
}
5.輸入兩個數求平均值
#include <stdio.h>
int main(void){
int count,sun,anInteger;
printf("Enter the inteegers and terminate with negtive number\n");
count =0;
sun=0;
printf("Enter number %d:",count+1);
scanf("%d",&anInteger);
while(anInteger>=0){
sun+=anInteger;
count++;
printf("Enter number %d:",count+1);
scanf("%d",&anInteger);
}if(count!=0){
printf("The average is %f\n",sun/(double)count);
}else{
printf("You enteed no numbers\n");
}
return 0;
}