大学生必备C语言基础练习题目来了(二)(建议收藏哦)❤️

题目1. 若一个三位数等于其各位上数字的立方和,则称这个三位数为水仙花数。

例如:153是一个水仙花数,因为153=13+53+33。编写一个程序,输出所有的水仙花数。(用while循环)

流程图:

image-20210911150716041

实现代码:

#include <stdio.h>

int main()
{
 int i=100;
	int a,b,c; 
	while(i < 1000){
	    a = i / 100;
		b = i /10 % 10;
		c = i % 10;
		if(i == (a*a*a + b*b*b + c*c*c)){
		  printf("水仙花数为:%d \n", i);
		}
		i++;
	}
}

运行结果:

image-20210911150829979


题目2. 编写一个程序,统计500~1000之间素数的个数。(用do while循环)

流程图:

image-20210911150938123

实现代码:

#include <stdio.h>

int main()
{
 int i=500,j,count=0;
	do
	{
	   for(j=2;j<i;j++)
	   {
		 if (i%j==0)
			break;
	   }
		 if (j==i)
			count++;
		 i++;	
	}
	while(i<=1000);	
	printf("count = %d\n", count);
}

运行结果:

image-20210911152256443


题目3.编写一个程序,计算200以内能被3整除,且个位数为5的所有整数之和。(用for循环)

流程图:

image-20210911151057688

实现代码:

#include <stdio.h>

int main()
{
 int sum=0, i;
	for(i = 1; i <= 200; i++)
	{
		if((i % 3 == 0) && (i % 10 == 5) )
		{
			sum += i;
		} 
	}
	 printf("和为:sum = %d \n",sum);
}

运行结果:

image-20210911152325394


题目4. 编写一个程序,输出九九乘法表。(循环嵌套)

流程图:

image-20210911151325488

实现代码:

#include <stdio.h>

int main()
{
  int i, j, result;
  printf("\n");
  for(i = 1; i < 10; i++)
  {
	  for(j = 1; j <= i; j++)
	  {
		  result = i * j;
		  printf("%d*%d = %-3d",i,j,result);
	  }
   printf("\n");
  }
}

运行结果:

image-20210911152344401


题目5. 若一个三位数等于其各位上数字的立方和,则称这个三位数为水仙花数。例如153

是一个水仙花数,因为153=13+53+33。编写一个程序,输出第一个水仙花数。(break)

流程图:

image-20210911151357871

实现代码:

#include <stdio.h>

int main()
{
 int i,a,b,c;
	for(i = 100; i < 1000; i++){
		a = i / 100;
		b = i /10 % 10;
		c = i % 10;
		if(i == (a*a*a + b*b*b + c*c*c)){
		  printf("第一个水仙花数为:%d \n", i);
          break;
		}
	}
}

运行结果:

image-20210911152403443


题目6. 编写一个程序,计算img。其中m由输入决定。

流程图:

image-20210911151442845

实现代码:

#include <stdio.h>

int main()
{
    int m,i;
	float s = 0.0;
    printf("请输入m的值:");
    scanf("%d",&m);
    for (i = 1; i <= m; i++)
    {
        if (i % 2 == 0)
        {
            s -= 1.0 / i;	
        }
        else
        {
            s += 1.0 / i;
        }
    }
    printf("s的值为:s = %f\n",s);
}

运行结果:

image-20210911152427542


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码上言

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

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

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

打赏作者

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

抵扣说明:

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

余额充值