C语言程序设计实例6
求1+2!+3!+…+n!的和
#include <stdio.h>
#include <stdlib.h>
int main()
{
float n, s = 0, i, t = 1;
printf("请输入n项为:");
scanf("%f", &n);
for (i = 1; i <= n; i++)
{
t *= i;
s += t;
}
printf("前n项阶乘之和s=%f\n", s);
system("pause");
return 0;
}
C语言程序设计实例7
输入一个年份判断它是否是闰年,满足下面条件之一就称为闰年:
(1).能被4整除而不能被100整除。
(2).能被100整除也能被400整除。
#include <stdio.h>
#include <stdlib.h>
int main()
{
long int year;
printf("input year:");
scanf("%d", &year);
if (year % 4 == 0 && year % 100 != 0 || year % 100 == 0 && year % 400 == 0)
{
printf("yes\n");
}
else
{
printf("No\n");
}
system("pause");
return 0;
}
C语言程序设计实例8
将十进制数转换成任意进制。分析:将输入的数循环除以基数取余知道商为0,然后逆序输出。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int idec, i, idr, p = 0, ibase;
char strdr[20], t;
printf("输入要转换的十进制数idec=");
scanf("%d", &idec);
printf("\n输入要转换成的进制基数ibase=");
scanf("%d", &ibase);
while (idec != 0)
{
idr = idec % ibase;
if (idr >= 10)
{
strdr[p++] = idr - 10 + 65; //16进制的A、B、C、D、E、F
}
else
{
strdr[p++] = idr + 48; //将数字转换为数字字符
}
idec /= ibase;
}
for (i = 0; i < p / 2; i++)
{
t = strdr[i];
strdr[i] = strdr[p - i - 1];
strdr[p - i - 1] = t;
}
strdr[p] = '\0';
printf("\n转换成%d进制后的数为:%s", ibase, strdr);
printf("\n");
system("pause");
return 0;
}
C语言程序设计实例9
将一个数组逆序输出。
#include <stdio.h>
#include <stdlib.h>
#define N 5
int main()
{
int a[N] = { 1,2,3,4,5 }, i, temp;
printf("\n original array:\n");
for (i = 0; i < N; i++)
{
printf("%4d", a[i]);
}
for (i = 0; i < N / 2; i++)
{
temp = a[i];
a[i] = a[N - i - 1];
a[N - i - 1] = temp;
}
printf("\nsorted array:\n");
for (i = 0; i < N; i++)
{
printf("%4d", a[i]);
}
system("pause");
return 0;
}
C语言程序设计实例10
打印所有的水仙花数,所谓水仙花数是指一个三位数,其各位数字立方和等于该书本身。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, k, n;
printf("water flower number is:");
for (n = 100; n < 1000; n++)
{
i = n / 100;
j = n / 10 % 10;
k = n % 10;
if (n == j*j*j + i*i*i + k*k*k)
{
printf("%-5d", n);
}
}
system("pause");
return 0;
}