实验3

#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!=%11d\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!=%11d\n",i,fac(i));
    }
    return 0;
}

long long fac(int n)
{
    static long long p = 1;
    printf("p = %11d\n",p); 
    p = p*n;
    return p;
}

 

#include <stdio.h>
int func(int,int);
int main(){
    int k = 4,m = 2,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); 
}

 

#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!=0){
        printf("%c",symbol);
        return printSymbol (n-1,symbol);
    }   
     
}

 

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

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

 


#include <stdio.h>
int isprime(int n); 
int main()
{
    int j=0;
    int n;
    for(n = 101;n>=101 && n<=200;n++){
        if(isprime(n)==0){
            printf("%d  ",n);
            j++;
            if(j%10 == 0){
                printf("\n");
            }
        }
    }
    printf("\n");
    printf("共有%d个非素数",j);
    return 0;
}

int isprime(int n){
    int i;
    int a;
    for(i=2;i<n;i++){
        if(n%i == 0){
            a = 0;
            break;
        }else if(n%i != 0){
            a = 1;
        } 
    }
    return a;
}

 

#include <stdio.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,b;
    int c = 0;
    int sum = 0;
    for(;s>0;){
        a = s%10;
        s = s/10;
        if(a%2 != 0){
            c++;
            for(b=c;b>0;b--){
                a = a*10; 
            }
            sum += a;
        }
    }
    sum = sum/10;
    return sum;
}

 

#include <stdio.h>
double fun(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){
    int i = 1;
    int a;
    int b = -1;
    double sum = 0.0;
    double fact = 1.0;
    for(a=1;a<=n;a++){
        for (i=1;i<=a;i++){
        fact *= i;
    } 
    b = -b;
    sum = sum +  1/(b*fact);
    fact = 1.0;
  }
  return sum;
}

 

posted @ 2021-11-29 16:43  刘骏涛  阅读(4)  评论(2编辑  收藏  举报