C语言——循环结构练习题

这篇文章包含一系列C语言编程练习,涉及计算数列的特定项之和,如偶数项、前15项中的特定函数和、奇数项的倒数和等。同时,还有分析数字位数、寻找特定条件的整数和求幂级数的和等题目,旨在提升循环结构的运用能力。

循环结构练习题

习题1

编写程序,计算并输出下面数列前15项中偶数项的和。

2*3,4*5,……,2n*(2n+1),……

//编写程序,计算并输出下面数列前15项中偶数项的和。
# include <stdio.h>
# include <stdlib.h>
# include <math.h>

int main()
{
    int n, sum = 0, a;
    for (n = 2; n <= 14; n += 2)
    {
        a = (2 * n) * (2 * n + 1);
        sum = sum + a;
    }
    printf("%d\n", sum);
    system("pause");
    return 0;
}

习题2

编写程序,计算并输出下面数列前15项(x=0.5)的和(结果取3位小数输出)。

cos(x)/x,cos(2x)/2x,cos(3x)/3x, ……,cos(n*x)/(n*x)……

# include <stdio.h>
# include <stdlib.h>
#include <math.h>
#define nx
#define y (cos(nx))
int main()
{
    float x = 0.5, a, n = 1, sum = 0;
    //printf("请输入x的值:");
    //scanf_s("%f",&x);
    //a = cos(n*x)/(n*x);
    while (n <= 3)
    {
        a = (cos(n * x) / (n * x));
        sum = sum + a;
        n++;
    }
    printf("前15项的和为:%.3f\n", sum);
    system("pause");
    return 0;
}

习题3

编写程序,计算并输出下面数列中前20项中奇数项的和(结果取3位小数输出)。

1/(1*2),1/(2*3),1/(3*4),……,1/(n*(n+1)),……

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

int main()
{
    float a, n=0,k=0, sum=0;
    while (n < 20)
    {   
        n = 2*k + 1;
        a = (1 / (n * (n + 1)));
        sum = sum + a;
        k++;
    }
    printf("前20项中奇数项的和:%.3f",sum);
    system("pause");
    return 0;
}

习题4

编写程序,读入一个正整数,分析它是几位数。

# include <stdio.h>
# include <stdlib.h>
int main()
{
    int a,n=0;
    scanf_s("%d",&a);
    while (a>0)
    {
        a = a / 10;
        n = n + 1;
    }
    printf("%d",n);
    system("pause");
    return 0;
}

习题5

编写程序,求1~2000之间所有3的倍数之和,当和大于1000时结束。

# include <stdio.h>
# include <stdlib.h>
int main()
{
    int sum = 0;
    for (int a = 1; a <= 2000; a++)
    {
        while (a % 3 == 0)
        {
            sum = sum + a;
            break;
        }
        if (sum > 1000)
            break;
    }
        printf("%d\n",sum);
    system("pause");
    return 0;
}

习题6

输出1000以内个位数字是6且能被3整除的所有数,要求每行输出6个数据

# include <stdio.h>
# include <stdlib.h>
int main()
{  
    int count = 0;
    printf("1000以内个位数字是6且能被3整除的所有数: \n");
    for (int i = 6; i <= 1000; i+=10)
    {
        if (i % 3 == 0)
        {
            printf("%d\t", i);
            count++;
            if (count % 6 == 0)
            {
                printf("\n");
            }
        }
    }
    printf("\n");
    system("pause");
    return 0;
}

习题7

编写程序,输出九九乘法表。

# include <stdio.h>
# include <stdlib.h>
int main()
{
    int a, b, c;
    for (a = 1; a < 10; a++)
    {
        for (b = 1; b <= a; b++)
        {
            c = a * b;
            printf("%d * %d = %d  ",a,b,c);
        }
        printf("\n");
    }
    system("pause");
    return 0;
}

习题8

编写程序, 计算出1000到5000之间能被3和7整除但不能被13整除的所有整数的和。

# include <stdio.h>
# include <stdlib.h>
int main()

{

    int sum=0 ;

    for (int a = 1000; a <= 5000; a++)

    {
    
        if (a % 3 == 0 && a % 7 == 0 && a % 13 != 0)

        {
            sum = sum + a;
        }

    }
    printf("所有整数的和:%d\n", sum);

    system("pause");

    return 0;

}

习题9

编写程序,求 1!+2!+3!+4!+…+20!

# include <stdio.h>
# include <stdlib.h>
int main()
{  
    double n = 1,a=1, sum = 0;
    while (n < 21)
    {
        a = a * n;
        sum = sum + a;
        n++;
    }
    printf("%.0lf",sum);
    system("pause");
    return 0;
}

收藏起来吧!

评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是先森丫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值