所有分类
  • 所有分类
  • 实时新闻

C语言实战例题:小白必会的10个C语言经典练习题!源码分享

这些都是基本的 C 程序,可以帮助刚踏入 C 编程世界的新手。快来试试吧:

1、C 语言编程 – Hello World

includeintmain(){printf("Hello world\n");printf("Linux迷 www.linuxmi.com");return0;
}
C语言实战例题:小白必会的10个C语言经典练习题!源码分享

2. C 语言编程 – 执行算术运算

includeintmain()
{inta,b;printf("Enter two numbers:");
scanf("%d%d",&a,&b);printf("Sum=%d difference=%d product=%d quotient=%d\n",a+b,a-b,a*b,a/b);return0;
}

3.C 编程 – 求圆的面积

includeincludeintmain(){floatr;floatN=3.14;floats;//N*r*r;该行必须在输入r值以后printf("请输入该圆的半径:");scanf("%f",&r);
        s=N*r*r;printf("%.7f\n",s);//输出小数点后7位return0;
}
C语言实战例题:小白必会的10个C语言经典练习题!源码分享

4.C编程 – 在3个数字中找到最大值

includeintmain(){inta, b, c;printf("\nEnter value of a, b & c : ");scanf("%d %d %d", &a, &b, &c);if((a > b) && (a > c))printf("\na is greatest\n");if((b > c) && (b > a))printf("\nb is greatest\n");if((c > a) && (c > b))printf("\nc is greatest\n");return0;
}
C语言实战例题:小白必会的10个C语言经典练习题!源码分享

5.C编程 – 查找偶数或奇数

includeintmain(){intn;printf("Enter a number:");scanf("%d",&n);if(n%2==0)
{printf("Number is even\n");
}else{printf("Number is odd\n");
}return0;
}
C语言实战例题:小白必会的10个C语言经典练习题!源码分享

6. C编程 – 显示一个数字的因数

includeintmain(){intn,i;printf("Enter a positive integer: ");scanf("%d",&n);printf("Factors of %d are: ", n);for(i=1;i<=n;++i)
{if(n%i==0)printf("%d ",i);
}return0;
}
C语言实战例题:小白必会的10个C语言经典练习题!源码分享

7. C编程 – 检查质数

includeintmain(){intn, i, flag =0;printf("Enter a positive integer: ");scanf("%d",&n);for(i=2; i<=n/2; ++i)
{// condition for nonprime numberif(n%i==0)
{
flag=1;break;
}
}if(flag==0)printf("%d is a prime number.\n",n);elseprintf("%d is not a prime number.\n",n);return0;
}
C语言实战例题:小白必会的10个C语言经典练习题!源码分享

8.C编程 – 检查闰年

includeintmain()
{intyear;printf("Enter a year: ");
scanf("%d",&year);if(year%4 ==0)
{if( year%100 ==0)
{//year is divisible by400, hence the year is a leap yearif( year%400 ==0)printf("%d is a leap year.\n", year);elseprintf("%d is not a leap year.\n", year);
}elseprintf("%d is a leap year.\n", year );
}elseprintf("%d is not a leap year.\n", year);return0;
}
C语言实战例题:小白必会的10个C语言经典练习题!源码分享

9.C编程 – 从1加到n的和

includeintmain(){inti,n,sum=0;printf("Upto how many terms you want to find the sum:");scanf("%d",&n);for(i=1;i<=n;i++){
sum = sum + i;
}printf("Sum is %d\n",sum);return0;
}
C语言实战例题:小白必会的10个C语言经典练习题!源码分享

10. C编程 – 一个数的阶乘

includeintmain(){intn, i;unsignedlonglongfactorial =1;printf("Enter an integer: ");scanf("%d",&n);// show error if the user enters a negative integerif(n <0)printf("Error! Factorial of a negative number doesnt exist.");else{for(i=1; i<=n; ++i)
{
factorial *= i;// factorial = factorial*i;}printf("Factorial of %d = %llu\n", n, factorial);
}return0;
}
C语言实战例题:小白必会的10个C语言经典练习题!源码分享

这些 C 编程示例,可以很好的帮助初学者进行编码之旅。

来源: Linux公社

写在最后:另外,对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!

编程学习书籍分享:

C语言实战例题:小白必会的10个C语言经典练习题!源码分享

编程学习视频分享:

C语言实战例题:小白必会的10个C语言经典练习题!源码分享

整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)

欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!

对于C/C++感兴趣可以关注小编在后台私信我:【编程交流】一起来学习哦!可以领取一些C/C++的项目学习视频资料哦!已经设置好了关键词自动回复,自动领取就好了!

原文链接:https://www.w1ym.com/83147/,转载请注明出处~~~
0

评论0

请先

站点公告

【温馨提示】 本站不建议您对本站支付任何费用或开通任何会员本站99%资源为免费资源只提供共享不提供技术支持,本站资源主要以学习开发为主,本站是为个人资源记录学习研究等情况而建立,如特殊原因下载,需在24小时删除相关资源。本站资源均来自互联网收集或网友分享,若有侵权,请联系站长删除,谢谢。立即查看
显示验证码
没有账号?注册  忘记密码?