例一:财务人员给员工发工资时经常遇到这样一个问题,即根据每个人的工资额(以元作为单位)计算出各种面值的钞票的张数,且要求总张数最少。
例如,某职工工资为3436元,发放方案为:100元34张,20元1张,10元1张,5元1张,1元1张。
#include
int main()
{
int money;
int rmb100;
int rmb50;
int rmb20;
int rmb10;
int rmb5;
printf("please input your salary:");
scanf("%d",&money);
rmb100 = money / 100;
money %= 100;
rmb50 = money / 50;
money %= 50;
rmb20 = money / 20;
money %= 20;
rmb10 = money / 10;
money %= 10;
rmb5 = money / 5;
money %= 5;
printf("rmb100 = %d , rmb50 = %d , rmb20 = %d , rmb10 = %d,\
rmb5 = %d , rmb1 = %d\n",rmb100,rmb50,rmb20,rmb10,rmb5,money);
return 0;
}
例二:从键盘输入一个大写字母,要求改用小写字母输出。
#include
char ch;
int main()
{
printf("please input a character:");
scanf("%c",&ch);
if(ch >= 'A'&& ch <= 'Z')
{
char ch_chang = ch + 32;
printf("ch_chang = %c\n",ch_chang);
}
else
{
printf("error\n");
}
return 0;
}
例三:输入三个实数a,b,c,按代数值由小到大次序输出这三个数。
#include
float a,b,c;
int main()
{
printf("please input three num:");
scanf("%f %f %f",&a,&b,&c);
if(a > b)
{
float tmp = a;
a = b;
b = tmp;
}
if(c > a)
{
float tmp = c;
c = a;
a = tmp;
}
if(a > b)
{
float tmp = a;
a = b;
b = tmp;
}
printf("%5.1f %5.1f %5.1f\n",c,a,b);
return 0;
}
例四:写程序,判某一年是否闰年。
输入:年(year)
计算:判是否闰年
输出:闰年或非闰年(leap)
闰年条件:
能被4整除,但不能被100整除。
能被4整除,又能被400整除。
#include
int year;
int main()
{
int flag = 0;
printf("please input year:");
scanf("%d",&year);
if(0 == year % 4)
{
if(0 == year % 100)
{
if(0 == year % 400)
{
flag = 1;
}
}
else
flag = 1;
}
else
printf("it is not leap year\n");
if (1 == flag)
printf("it is leap year\n");
return 0;
}
例五:编写一个能进行两个操作数加减乘除四则运算的计数器模拟程序
输入:两个操作数和运算符
计算:根据运算符确定运算
输出:运算结果
include
float a,b;
char op;
int main()
{
printf("please input *** ");
scanf("%f%c%f",&a,&op,&b);
switch(op)
{
case '+':
printf("=%f\n",a+b);
break;
case '-':
printf("=%f\n",a-b);
break;
case '*':
printf("=%f\n",a*b);
break;
case '/':
if(0 == b)
{
printf("error\n");
}
else
{
printf("=%f\n",a/b);
}
break;
}
return 0;
}
例六:给出一百分制成绩,要求输出成绩等级‘A’、’B’、‘C’、‘D’、‘E’。90分以上为‘A’ ,80~89分为’B’ ,70~79分为‘C’...
#include
int score;
int main()
{
printf("please input score:");
scanf("%d",&score); //& can't forget
if(score > 100 || score < 0)//|| and && look careful
{
printf("error\n");
}
else
{
score /= 10;
switch (score)
{
case 10:
case 9:
printf("A\n");//more case
break;
case 8:
printf("B\n");
break;
case 7:
printf("C\n");
break;
case 6:
printf("D\n");
break;
default:
printf("E\n");
}
}
return 0;
}
例七:输入:无
计算:1+2+3+L+100
输出:计算的和
#include
int n = 0;
int sum = 0;
int main()
{
while(n <= 100)
{
sum = sum + n;
n++;
}
printf("%d\n",sum);
return 0;
}
例八:计算一条语句中的空格、字母、数字的个数。
#include
int x = 0;
int y = 0;
int z = 0;
char ch;
int main()
{
while(ch != '\n')
{
scanf("%c",&ch);
if((ch <= 'Z'&& ch >= 'A') ||(ch <= 'z' && ch >= 'a'))
{
x++;
}
if(ch == ' ')
{
y++;
}
if(ch <= '9' && ch >= '0')
{
z++;
}
}
printf("nonu= %d,character= %d,num= %d\n",y,x,z);
return 0;
}