很适合刚开始学C语言的同学。

1、C语言打印一条语句

源代码:

/* C Program to print a sentence. */

#include

int main()

{

printf("C Programming"); /* printf() prints the content inside quotation */

return 0;

}

输出:

C Programming

2、C语言打印用户输入的一个整数

源代码:

#include

int main()

{

int num;

printf("Enter a integer: ");

scanf("%d",&num);  /* Storing a integer entered by user in variable num */

printf("You entered: %d",num);

return 0;

}

输出:

Enter a integer: 25

You entered: 25

3、C语言实现两个整数相加

源代码:

/*C programming source code to add and display the sum of two integers entered by user */

#include

int main( )

{

int num1, num2, sum;

printf("Enter two integers: ");

scanf("%d %d",&num1,&num2); /* Stores the two integer entered by user in variable num1 and num2 */

sum=num1+num2;      /* Performs addition and stores it in variable sum */

printf("Sum: %d",sum);  /* Displays sum */

return 0;

}

输出:

Enter two integers: 12

11

Sum: 23

4、C语言实现两个小数相乘

源代码:

/*C program to multiply and display the product of two floating point numbers entered by user. */

#include

int main( )

{

float num1, num2, product;

printf("Enter two numbers: ");

scanf("%f %f",&num1,&num2);        /* Stores the two floating point numbers entered by user in variable num1 and num2 respectively */

product = num1*num2;  /* Performs multiplication and stores it */

printf("Product: %f",product);

return 0;

}

输出:

Enter two numbers: 2.4

1.1

Product: 2.640000

5、C语言查找字符的ASCII值

源代码:

/* Source code to find ASCII value of a character entered by user */

#include

int main(){

char c;

printf("Enter a character: ");

scanf("%c",&c);        /* Takes a character from user */

printf("ASCII value of %c = %d",c,c);

return 0;

}

输出:

Enter a character: G

ASCII value of G = 71

6、C语言根据用户输入的整数做商和余数

源代码:

/* C Program to compute remainder and quotient  */

#include

int main(){

int dividend, divisor, quotient, remainder;

printf("Enter dividend: ");

scanf("%d",&dividend);

printf("Enter divisor: ");

scanf("%d",&divisor);

quotient=dividend/divisor;           /*  Computes quotient */

remainder=dividend%divisor;          /* Computes remainder */

printf("Quotient = %d\n",quotient);

printf("Remainder = %d",remainder);

return 0;

}

输出:

Enter dividend: 25

Enter divisor: 4

Quotient = 6

Remainder = 1

7、C语言获取整型、单精度浮点型、双精度浮点型和字符型的长度

基本语法是:

temp = sizeof(operand);

/* Here, temp is a variable of type integer,i.e, sizeof() operator

returns integer value. */

源代码:

/* This program computes the size of variable using sizeof operator.*/

#include

int main(){

int a;

float b;

double c;

char d;

printf("Size of int: %d bytes\n",sizeof(a));

printf("Size of float: %d bytes\n",sizeof(b));

printf("Size of double: %d bytes\n",sizeof(c));

printf("Size of char: %d byte\n",sizeof(d));

return 0;

}

输出:

Size of int: 4 bytes

Size of float: 4 bytes

Size of double: 8 bytes

Size of char: 1 byte

注:可能会由于系统的不同出来的结果也不尽相同。

8、C语言获取关键字long的长度范围

源代码:

#include

int main(){

int a;

long int b;                /* int is optional. */

long long int c;            /* int is optional. */

printf("Size of int = %d bytes\n",sizeof(a));

printf("Size of long int = %ld bytes\n",sizeof(b));

printf("Size of long long int = %ld bytes",sizeof(c));

return 0;

}

输出:

Size of int = 4 bytes

Size of long int = 4 bytes

Size of long long int = 8 bytes

9、C语言交换数值

源代码:

#include

int main(){

float a, b, temp;

printf("Enter value of a: ");

scanf("%f",&a);

printf("Enter value of b: ");

scanf("%f",&b);

temp = a;    /* Value of a is stored in variable temp */

a = b;       /* Value of b is stored in variable a */

b = temp;    /* Value of temp(which contains initial value of a) is stored in variable b*/

printf("\nAfter swapping, value of a = %.2f\n", a);

printf("After swapping, value of b = %.2f", b);

return 0;

}

输出:

Enter value of a: 1.20

Enter value of b: 2.45

After swapping, value of a = 2.45

After swapping, value of b = 1.2

10、C语言检查数值是奇数还是偶数

源代码:

/*C program to check whether a number entered by user is even or odd. */

#include

int main(){

int num;

printf("Enter an integer you want to check: ");

scanf("%d",&num);

if((num%2)==0)      /* Checking whether remainder is 0 or not. */

printf("%d is even.",num);

else

printf("%d is odd.",num);

return 0;

}

输出1:

Enter an integer you want to check: 25

25 is odd.

输出2:

Enter an integer you want to check: 12

12 is even.

也可以用条件运算符解决:

/* C program to check whether an integer is odd or even using conditional operator */

#include

int main(){

int num;

printf("Enter an integer you want to check: ");

scanf("%d",&num);

((num%2)==0) ? printf("%d is even.",num) : printf("%d is odd.",num);

return 0;

}

11、C语言检查是元音还是辅音

源代码:

#include

int main(){

char c;

printf("Enter an alphabet: ");

scanf("%c",&c);

if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U')

printf("%c is a vowel.",c);

else

printf("%c is a consonant.",c);

return 0;

}

输出1:

Enter an alphabet: i

i is a vowel.

输出2:

Enter an alphabet: G

G is a consonant.

也可以用条件运算符解决

/* C program to check whether a character is vowel or consonant using conditional operator */

#include

int main(){

char c;

printf("Enter an alphabet: ");

scanf("%c",&c);

(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U') ? printf("%c is a vowel.",c) : printf("%c is a consonant.",c);

return 0;

}

输出结果和上面的程序相同。

12、C语言实现从三个数值中查找最大值

实现1:

/* C program to find largest number using if statement only */

#include

int main(){

float a, b, c;

printf("Enter three numbers: ");

scanf("%f %f %f", &a, &b, &c);

if(a>=b && a>=c)

printf("Largest number = %.2f", a);

if(b>=a && b>=c)

printf("Largest number = %.2f", b);

if(c>=a && c>=b)

printf("Largest number = %.2f", c);

return 0;

}

实现2:

/* C program to find largest number using if...else statement */

#include

int main(){

float a, b, c;

printf("Enter three numbers: ");

scanf("%f %f %f", &a, &b, &c);

if (a>=b)

{

if(a>=c)

printf("Largest number = %.2f",a);

else

printf("Largest number = %.2f",c);

}

else

{

if(b>=c)

printf("Largest number = %.2f",b);

else

printf("Largest number = %.2f",c);

}

return 0;

}

实现3:

/* C Program to find largest number using nested if...else statement */

#include

int main(){

float a, b, c;

printf("Enter three numbers: ");

scanf("%f %f %f", &a, &b, &c);

if(a>=b && a>=c)

printf("Largest number = %.2f", a);

else if(b>=a && b>=c)

printf("Largest number = %.2f", b);

else

printf("Largest number = %.2f", c);

return 0;

}

输出结果相同:

Enter three numbers: 12.2

13.452

10.193

Largest number = 13.45

13、C语言解一元二次方程

源代码:

/* Program to find roots of a quadratic equation when coefficients are entered by user. */

/* Library function sqrt() computes the square root. */

#include

#include /* This is needed to use sqrt() function.*/

int main()

{

float a, b, c, determinant, r1,r2, real, imag;

printf("Enter coefficients a, b and c: ");

scanf("%f%f%f",&a,&b,&c);

determinant=b*b-4*a*c;

if (determinant>0)

{

r1= (-b+sqrt(determinant))/(2*a);

r2= (-b-sqrt(determinant))/(2*a);

printf("Roots are: %.2f and %.2f",r1 , r2);

}

else if (determinant==0)

{

r1 = r2 = -b/(2*a);

printf("Roots are: %.2f and %.2f", r1, r2);

}

else

{

real= -b/(2*a);

imag = sqrt(-determinant)/(2*a);

printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);

}

return 0;

}

输出1:

Enter coefficients a, b and c: 2.3

4

5.6

Roots are: -0.87+1.30i and -0.87-1.30i

输出2:

Enter coefficients a, b and c: 4

1

0

Roots are: 0.00 and -0.25

c语言读取无压缩的cbl的源代码,c语言基础算法案例相关推荐

  1. c语言读取一行的前几个字符串,c语言读取文件某一列 c语言 读取文件中某一行的前一行...

    各种格式的文件用什么软件打开.$$$TemporaryFile)Q7?5g4U5m&_.@.m;K"S.$$AOS/2club.looaoo.net-s;X'a)B.N,w7\;{9 ...

  2. C语言安卓FTP服务器,FTP服务器客户端源代码C语言

    <FTP服务器客户端源代码C语言>由会员分享,可在线阅读,更多相关<FTP服务器客户端源代码C语言(12页珍藏版)>请在人人文库网上搜索. 1.FTP 服务器源代码: #inc ...

  3. 如何用c语言读取硬盘串号_如何用C语言实现OOP

    我们知道面向对象的三大特性分别是:封装.继承.多态.很多语言例如:C++和Java等都是面向对象的编程语言,而我们通常说C是面向过程的语言,那么是否可以用C实现简单的面向对象呢?答案是肯定的!C有一种 ...

  4. c语言判断一个数是否为偶数源代码,c语言判断一个数是否为偶数

    #include #include _Bool isOu(int n){ //高度注意:&的优先级低于== )==){ return true; } else{ return false; } ...

  5. 利用C语言读取BMP文件

    文章目录 什么是bmp文件 1.文件头信息块 2.图像描述信息块 3.颜色表 4.图像数据区 编写代码 C文件 h头文件 存储算法 什么是bmp文件 BMP是bitmap的缩写形式,bitmap顾名思 ...

  6. C语言读取bmp图像并做简单显示

    C语言读取bmp图像并做简单显示) bmp文件格式 读取bmp文件信息并展示 bmp文件格式 bmp文件大体上分为四个部分: bmp文件构成 位图文件头BITMAPFILEHEADER 位图信息头BI ...

  7. C语言读取和存储bmp格式图片

    开发过程中有时候需要解析bmp数据,下面先简单介绍bmp数据组成,后面附上C语言读取和存储bmp格式图片代码. 典型的位图文件格式通常包含下面几个数据块: BMP文件头:保存位图文件的总体信息. 位图 ...

  8. HDMI原理详解以及时序流程(视频是三对差分信号,音频Audio是PCM级(无压缩)传输,包含在数据包内,依靠协议规定采样)HDMI可以传输RGB与YUV两种格式

    资料来源:HDMI介绍与流程 - TaigaComplex - 博客园 最近要用ZYNQ开发版的HDMI做显示,看着硬件管脚和例程只能发呆,于是决心去弄清楚HDMI的工作原理,查找了很多资料,都是碎片 ...

  9. python语言可以处理数据文件吗_Python语言读取Marc后处理文件基础知识.pdf

    Python语言读取Marc后处理文件基础知识 基于 python 的焊接后处理 知识要点:  Python 语言  Python 模块功能  PyPost 后处理模块  PyPost 模块函 ...

最新文章

  1. css重叠边界,关于css:两个重叠元素上的边界半径; 背景闪耀
  2. 滴滴与能链杀红眼的加油市场,究竟有多大?
  3. 迪粉汇接入网易云信,打造比亚迪车友亲密沟通利器
  4. 虚拟机扩容后mysql无法使用_VMWARE 扩容踩坑记
  5. PowerBI分析Exchange服务器IIS运行日志
  6. CAN总线技术 | 数据链路层01 - CAN报文的组成
  7. linux中的进程有哪三种基本状态,Linux下的进程有哪三种状态?()
  8. 基于Bootstrap的Asp.net Mvc 分页的实现(转)
  9. 流行的 NPM 包依赖关系中存在远程代码执行缺陷
  10. FFMPEG结构体分析:AVCodecContext
  11. 应用层安全协议Kerberos
  12. 区块链应用如何实现资金盘分红
  13. 实用系列丨免费可商用视频素材库
  14. 视觉跟踪近年来的进展(2010年以前)——Advances in Visual Tracking
  15. mysql 有没有minus_MySQL实现差集(Minus)和交集(Intersect)
  16. 游戏开发物理引擎PhysX研究系列:通过Unity中的物理系统学习Physx指引贴
  17. python matting后如何设置透明背景
  18. 【侠客行】Lombok深度解析
  19. TCPIP之IP协议及IP地址详解
  20. 【Git】fatal Not a git repository or any of the parent direc

热门文章

  1. 利用卷积神经网络实现手写字识别
  2. 基于JAVA传统文化知识竞赛系统计算机毕业设计源码+系统+数据库+lw文档+部署
  3. 心、肝、脾、肺、肾的毒藏在哪,你知道吗?
  4. 五年企稳上升的阿里,还能再涨吗?
  5. lua/cocos加载动画以及可以使用加载纹理的方式来替换图片并且加载个人制作的艺术字体(fnt字体)
  6. 关于be of noun的用法
  7. 查看电脑使用了多长时间
  8. SQL(16)--获取员工当前薪水比其manager薪水还高的相关信息
  9. 点云公开数据集:Semantic3D
  10. Android应用数据备份