当前位置: 首页 > news >正文

【C语言】基础篇续

  • 最大公约数HCF与最小公倍数LCM

#include<stdio.h>
int main(){int n1,n2,i,hcf,lcm;printf("Enter two numbers:");scanf("%d %d",&n1,&n2);for(i = 1;i <= n1 & i <= n2;i++){if(n1 % i == 0 & n2 % i == 0){hcf = i;lcm = (n1*n2)/hcf;}}printf("HCF of %d and %d is %d\n",n1,n2,hcf);printf("LCM of %d and %d is %d",n1,n2,lcm);
}
  • 简单实现开根号    sqrt

#include<stdio.h>
#include<math.h>
int main(){int a = 9;int b;b = sqrt(a);printf("%d",b);return 0;
}
  • 计算ax²+bx+c=0的根

#include<stdio.h>
#include<math.h>
int main(){double a,b,c,disc,x1,x2,p,q;printf("Enter ax²+bx+c of a b c:");scanf("%lf %lf %lf",&a,&b,&c);disc = b*b - 4*a*c;if(disc<0){printf("This equation hasn't real root\n");}else{p = -b/(2.0*a);q = sqrt(disc)/(2.0*a);x1 = p + q;x2 = p - q;printf("x1 = %5.2f\nx2 = %5.2f\n",x1,x2);}return 0;
}
  •  通过分数评判等级

#include<stdio.h>
int main(){int s,score;scanf("%d",&score);printf("Your grade is:");s = score/10;switch(s){case 1: printf("D");break;case 2: printf("D");break;case 3: printf("D");break;case 4: printf("D");break;case 5: printf("D");break;case 6: printf("C");break;case 7: printf("C");break;case 8: printf("B");break;case 9: printf("A");break;case 10: printf("A");break;default:printf("Error");}return 0;
}
  • 循环嵌套输出矩阵

#include<stdio.h>
int main(){int i,j,n=0;for(i = 1;i <= 4;i++)for(j = 1;j <= 5;j++,n++){if(n%5 == 0)printf("\n");printf("%d\t",i*j);}printf("\n");return 0;
}
  •  求100-200的全部素数

#include<stdio.h>
#include<math.h>
int main(){int n,k,i,m=0;for(n = 101;n <= 200;n = n+2){  // 偶数不用排查k = sqrt(n);for(i = 2;i <= k;i++){if(n % i == 0)break;}if(i >= k + 1){       // 表示n未被整除printf("%d ",n);m = m + 1;}if(m % 10 == 0)      // 一行内输出10个素数printf("\n");}printf("\n");return 0;
}
  • 大写字母转小写

#include<stdio.h>
int main(){char c1,c2;printf("Enter a capital letter :");c1 = getchar();c2 = c1 + 32;printf("It's small lettter is :");putchar(c2);return 0;
}
  • 计算字符串长度

调用内置函数strlen

#include<stdio.h>
#include<string.h>
int main(){char str[50];int len;printf("Enter a string:");scanf("%s",str);len = strlen(str);printf("Lengh of '%s' = %d",str,len);return 0;
}

使用指针

#include<stdio.h>
int str_len(char* str){int count = 0;while(*str != '\0'){count++;str++;}return count;
}int main(){char str[50];printf("Enter a string:");scanf("%s",str);int len = str_len(str);printf("%d",len);return 0;
}

http://www.lryc.cn/news/454976.html

相关文章:

  • 文件丢失一键找回,四大数据恢复免费版工具推荐!
  • 【学习笔记】手写一个简单的 Spring MVC
  • 编程究竟难在哪里?
  • C#医学影像分析源码,医院影像中心PACS系统源码
  • WooCommerce与wordpress是什么关系
  • Web常见的攻击方式及防御方法
  • 基于STM32的超声波测距仪设计
  • 【数据库】Java 集成mongodb— MongoTemplate 详解
  • 腿和脚的动作透露出你的内心“世界”
  • Oracle架构之用户,权限,角色讲解
  • Unity_Obfuscator Pro代码混淆工具_学习日志
  • 已解决:org.springframework.web.HttpMediaTypeNotAcceptableException
  • C/C++简单编译原理
  • 文件处理不再难:带你轻松攻克C语言文件操作
  • Unity3D 单例模式
  • 解析TMalign文本文件中的转换矩阵
  • vue.js组建开发
  • D29【python 接口自动化学习】- python基础之输入输出与文件操作
  • jQuery——平滑翻页
  • 二叉树--DS
  • State of ChatGPT ---- ChatGPT的技术综述
  • 构建高效新闻推荐系统:Spring Boot的力量
  • 如何使用ipopt进行非线性约束求目标函数最小值(NLP非线性规划)内点法(inner point method)
  • 【Unity学习笔记】解决疑似升级Win11或使用Unity6导致Unity旧版本无法打开的问题
  • 回归分析在数据挖掘中的应用简析
  • 【Node.js】worker_threads 多线程
  • 贪心算法c++
  • 【STM32】 TCP/IP通信协议(3)--LwIP网络接口
  • 15分钟学 Python 第39天:Python 爬虫入门(五)
  • 使用Pytorch构建自定义层并在模型中使用