葛才璟

实验3 函数

实验3 函数

1. 实验任务1

#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;
printf("p = %lld\n", p);
p=p*n;
return p;
}

#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)
{long long f;
if(n==1)
f=1;
else
f=2*fun(n-1)+1;
}

 

 static变量的特性:作为局部静态变量,能保持变量内容的持久。

2.实验任务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
{
printSymbol(n-1,symbol);
printf("%c",symbol);
}
}

 

 

 

 3.实验任务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)
{long long f;
if(n==1)
f=1;
else
f=2*fun(n-1)+1;
}

 

 

 

 4.实验任务4

#include <stdio.h>
int isPrime(int n);

int main(){
    int i,k=0,n=0;
    
    for(i=101;i<=200;i++){
        if(isPrime(i)==0){
            printf("%4d",i);
            k++;
            n++;
            if(k%10==0){
                printf("\n");
                k=0;
            } 
        }
    }
    printf("\n一共有%d个非素数\n",n);
    
    return 0;
}

int isPrime(int n){
    int j;
    for(j=2;j<n;j++){
        if(n%j==0){
            break;
        }    
    }
    if(j<n){
        return 0;
    }else{
        return 1;
    }
}

 

 

 5.实验任务5

#include <stdio.h>
#include<math.h>
long fun(long s); 

int main()
{
    long s, t;
    printf("Enter a number: ");
    while (scanf("%ld", &s) != EOF)
    {
        t = fun(s); 
        printf("new number is: %ld\n\n", t);
        printf("Enter a number: ");
    }
    return 0;
}

long fun(long s){
    int a,i=0;
    long sum=0;
    while(s>0){
        a=s%10;
        if(a%2!=0){
            sum+=a*pow(10,i);
            i++;
        }
        s/=10;
    }
    return sum;
}

 

 

 6.实验任务6

#include <stdio.h>
#include<math.h>
double fun(int n);
int factorial(int n);

int main()
{
    int n;
    double s;
    printf("Enter n(1~10): ");
    while (scanf("%d", &n) != EOF)
    {
        s = fun(n); 
        printf("n = %d, s= %f\n\n", n, s);
        printf("Enter n(1~10): ");
    }
    return 0;
}

double fun(int n){
    double sum=0;
    if(n==1){
        sum=1;
    }else{
        sum=pow(-1,n-1)/factorial(n)+fun(n-1);
    }
    return sum;
}

int factorial(int n){
    int fac=0;
    if(n==1){
        fac=1;
    }else{
        fac=n*factorial(n-1);
    }
    return fac;
}

 

posted on 2021-11-28 15:52  葛才璟  阅读(36)  评论(0编辑  收藏  举报

导航