实验一
#include <stdio.h>
long long fac(int n);
int main() {
int i, n;
printf("Enter n:");
scanf("%d", &n);
for ( i = 1; i <= n; ++i)
{
printf("%d!=%lld\n", i, fac(i));
}
return 0;
}
long long fac(int n) {
static long long p = 1;
p = p * n;
return p;
}
#include <stdio.h>
long long fac(int n);
int main() {
int i, n;
printf("Enter n:");
scanf("%d", &n);
for ( i = 1; i <= n; ++i)
{
printf("%d!=%lld\n", i, fac(i));
}
return 0;
}
long long fac(int n) {
static long long p = 1;
p = p * n;
printf("p=%d\n", p);
return p;
}
#include <stdio.h>
int func(int, int);
int main() {
int k = 4, m = 1, p1, p2;
p1 = func(k, m);
p2 = func(k, m);
printf("%d,%d\n", p1, p2);
return 0;
}
int func(int a, int b) {
static int m = 0, i = 2;
i += m + 1;
m = i + a + b;
return (m);
}
实验2
#include <stdio.h>
void printsymbol(int n, char symbol);
int main() {
int n;
char symbol;
while (scanf("%d %c",&n,&symbol)!=EOF)
{
printsymbol(n, symbol);
printf("\n");
}
return 0;
}
void printsymbol(int n, char symbol) {
int i;
for ( i = 0; i < n; i++)
{
printf("%c", symbol);
}
}
#include <stdio.h>
void printsymbol(int n, char symbol);
int main() {
int n;
char symbol;
while (scanf("%d %c",&n,&symbol)!=EOF)
{
printsymbol(n, symbol);
printf("\n");
}
return 0;
}
void printsymbol(int n, char symbol) {
if (n==1)
{
printf("%c", symbol);
}
else
{
printf("%c", symbol);
printsymbol(n - 1, symbol);
}
}
实验3
#include <stdio.h>
long long fun(int n);
int main() {
int n;
long long f;
while (scanf("%d", &n) != EOF) {
f = fun(n);
printf("n=%d,f=%lld\n", n, f);
}
return 0;
}
long long fun(int n) {
int result;
if (n==1)
{
result = 1;
return result;
}
else
{
result =(fun(n - 1)+1)*2-1;
return result;
}
}
实验4
#include <stdio.h>
#include<math.h>
int isprime(int n);
int main() {
int i,num=0;
for ( i = 101; i < 201; i++)
{
if (!isprime(i))
{
printf(" %d", i);
num++;
}
}
printf("\n101~200之间一共有%d个非素数",num);
return 0;
}
int isprime(int n){
int i;
for (int i = 2; i < (n + 4) / 2; i++)
{
if (n == 2)
{
return 1;
}
else if (n % i == 0)
{
return 0;
break;
}
if (i == (n + 4) / 2 - 1 && n % i != 0)
{
return 1;
}
}
}
实验5
#include <stdio.h>
#include<math.h>
long fun(long s);
int main() {
long s, t;
printf("Ener a number:");
while (scanf("%ld", &s) != EOF)
{
t = fun(s);
printf("new number is:%ld\n", t);
printf("Enter a number:");
}
return 0;
}
long fun(long s) {
int a[64];
int i,j,num;
num = 0;
for ( i = 0; s >0 ; i++,s=s/10)
{
a[i] = s % 10;
}
for ( j= 0; j <i ; j++)
{
if (a[j]%2!=0)
{
num = a[j] + num * 10;
}
}
s = num;
num = 0;
for ( i = 0; s >0 ; i++,s=s/10)
{
num = num * 10 + s % 10;
}
return num;
}
实验6
#include <stdio.h>
#include<math.h>
double fun(int n);
int main() {
int n,i;
double s,sum;
printf("Enter n(1~10):");
while (scanf("%d",&n)!=EOF)
{
sum = 0;
for ( i = 1; i <= n; i++)
{
s = fun(i);
sum = sum + s;
}
printf("n=%d,s=%f\n\n", n, sum);
printf("Enter n(1~10):");
}
return 0;
}
double fun(int n) {
double result ;
int a[10];
int i;
if (n==1)
{
return 1;
}
else
{
result = -fun(n - 1) / n;
return result;
}
}