C语言零基础入门习题(六)计算e的精确值

前言 

        C语言是大多数小白走上程序员道路的第一步,在了解基础语法后,你就可以来尝试解决以下的题目。放心,本系列的文章都对新手非常友好。


Tips:题目是英文的,但我相信你肯定能看懂

一、Write a program that approximates e by computing the value of e = 1 + 1/1! + 1/2! + 1/3! + ... + 1/n! ,where n is an integer entered by the user.

Here are sample runs:

<output1>

Enter integer n: 5<enter icon>

Approximation of e: 2.716667

<end output1>

<output2>

Enter integer n: 10<enter icon>

Approximation of e: 2.718282

<end output2>

#include <stdio.h>

int main()
{
    int n;
    float s=1,a=1,b=1;
    printf("Enter integer n: ");
    scanf("%d",&n);
        for (;b<=n;b++)
        {
            a=a/b;
            s+=a;
        }

    printf("Approximation of e: %f",s);
    return 0;
}

二、Write a program that reads integers until 0 is entered. After input terminates, the program should report the total number of even integers (excluding the 0) entered, the average value of the even integers, the total number of odd integers entered, and the average value of the odd integers. Show “No input” when input 0 only.

Here are sample runs:

<output1>

1 2 3 4 5 6 7 8 9 0 <enter icon>

Number of even:4,the average of even:5

Number of odd:5,the average of odd:5

<end output1>

<output2>

1 0 <enter icon>

Number of even:0,no even

Number of odd:1,the average of odd:1

<end output2>

<output3>

-2 -10 0 <enter icon>

Number of even:2, the average of even:-6

Number of odd:0,no odd

<end output3>

<output4>

0 <enter icon>

No input

<end output4>

#include <stdio.h>

int main()
{
    int n,e=0,o=0,s1=0,s2=0;
    while (scanf("%d",&n) && n!=0)
    {
        if (n%2==0)
        {
             e++;
             s2+=n;
        }
        else
        {
            o++;
            s1+=n;
        }
    }
    if (s1==0 && s2==0) printf ("No input");
    else
    {
    printf("Number of even:%d",e);
    if (e!=0) printf(",the average of even:%d\n",s2/e);
    else printf(",no even\n");
    printf("Number of odd:%d",o);
    if (o!=0) printf(",the average of odd:%d\n",s1/o);
    else printf(",no odd\n");
    }
    return 0;
}

三、Write a program that requests lower and upper integer limits, calculates the sum of all the integer squares from the square of the lower limit to the square of the upper limit, and displays the answer. The program should then continue to prompt for limits and display answers until the user enters an upper limit that is equal to or less than the lower limit.

Here are sample runs:

<output>

Enter lower and upper integer limits: 5 9<enter icon>

The sums of the squares from 25 to 81 is 255

Enter next set of limits: 3 25<enter icon>

The sums of the squares from 9 to 625 is 5520

Enter next set of limits: 5 5<enter icon>

Done

<end output>

#include <stdio.h>

int main()
{
    int a,b,s=0;
    float i;
    printf("Enter lower and upper integer limits: ");
    scanf("%d%d",&a,&b);
    if (a>=b)
    {
       printf("Done");
        return 0;
    }
    else
    {
         for (i=a;i<=b;i++)
            s+=i*i;
        printf("The sums of the squares from %d to %d is %d\n",a*a,b*b,s);
    }
    while (1)
    {
        s=0;
        printf("Enter next set of limits: ");
        scanf("%d%d",&a,&b);
         if (a>=b)
    {
       printf("Done");
        return 0;
    }
    else
    {
         for (i=a;i<=b;i++)
            s+=i*i;
        printf("The sums of the squares from %d to %d is %d\n",a*a,b*b,s);
    }
    }
}

四、write a program that determines which of two dates comes earlier on the calendar. Generalize the program so that the user may enter any number of dates. The user will enter 0/0/0 to indicate that no more dates will be entered

Here are sample runs:

<output>

Enter a date (mm/dd/yy): 3/6/08<enter icon>

Enter a date (mm/dd/yy): 5/17/07<enter icon>

Enter a date (mm/dd/yy): 6/3/07<enter icon>

Enter a date (mm/dd/yy): 0/0/0<enter icon>

5/17/07 is the earliest date.

<end output>

#include <stdio.h>

int main()
{
    int m,d,y,mm=999,dd=999,yy=999;
    while (1)
    {
        printf("Enter a date (mm/dd/yy):");
        scanf("%d/%d/%d",&m,&d,&y);
        if (m!=0 || d!=0 || y!=0)
        {
           if (y<yy)
        {
            yy=y; mm=m; dd=d;
        }
        if (y==yy&&m<mm)
        {
            yy=y; mm=m; dd=d;
        }
        if (y==yy&&m==mm&&d<dd)
        {
            yy=y; mm=m; dd=d;
        }
        }
        else break;
    }
    printf("%d/%d/%02d is the earliest date.",mm,dd,yy);
    return 0;
}

五、Write a program that asks the user to enter two integers, then calculates and displays their greatest common divisor (GCD):

Hint: The classic algorithm for computing the GCD, known as Euclid's algorithm, goes as follows: Let m and n be variables containing the two numbers. If n is 0, then stop: m contains the GCD. Otherwise, compute the remainder when m is divided by n. Copy n into m and copy the remainder into n. Then repeat the process, starting with testing whether n is 0.

Here are sample runs:

<output>

Enter two integers: 12 28<enter icon>

Greatest common divisor: 4

<end output>

#include <stdio.h>

int main()
{
    int m,n,i,min,max;
    printf("Enter two integers: ");
    scanf ("%d%d",&m,&n);
    min = m<n? m:n;
    max= m>n? m:n;
    while (max !=0 && min !=0)
    {
        i=max%min;
        max =min;
        min =i;
    }
    printf("Greatest common divisor: %d",max);
    return 0;
}

六、(Reduce square root) Write a program that prompts the user to enter a positive integer and obtains its square root in simplest form. For example, the simplest form for √18 is 3√2 is, for √28 is 2√7 , and for √648 is 18√2.

Here are sample runs:

<Output>

Enter a positive integer: 1300 <enter icon>

sqrt(1300) is 10*sqrt(13)

<End Output>

<Output>

Enter a positive integer: 31 <enter icon>

sqrt(31) is sqrt(31)

<End Output>

<Output>

Enter a positive integer: 64 <enter icon>

sqrt(64) is 8

<End Output>

#include <stdio.h>
#include <math.h>

int main()
{
    int c,i;
    float a,b;
    printf("Enter a positive integer: ");
    scanf ("%f",&a);
    b=sqrt(a);
    if (b-(int)b==0)
        printf ("sqrt(%.0f) is %.0f",a,b);
    else
    {
        for (c=b;c>0;c--)
    {
        if (c==1)
            printf ("sqrt(%.0f) is sqrt(%.0f)",a,a);
        if (a/c/c-(int)(a/c/c)==0 && c!=1)
        {
            printf ("sqrt(%.0f) is %d*sqrt(%.0f)",a,c,a/(c*c));
            c=0;
        }
    }
    }
    return 0;
}


总结

以上就是本文全部内容,你学会了吗?

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值