当前位置:首页 > 网络安全 > 正文内容

c语言入门经典必背18个程序(C语言程序入门)

hacker2年前 (2022-06-10)网络安全155

C语言基础知识

下面这些C语言基础算法案例都是经过测试和验证过了的,欢迎各位使用。

本文是该系列的第一篇,都是一些相对初级的算法,很适合刚开始学C语言的同学。

1、C语言打印一条语句

源代码:

/* C Program to print a sentence. */

#include <stdio.h>

int main()

{

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

return 0;

}

输出:

C Programming

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

#include <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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语言获取整型、单精度浮点型、双精度浮点型和字符型的长度

基本语法:

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

#include <stdio.h>

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 <stdio.h>

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 <stdio.h>

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

c语言入门经典必背18个程序(C语言程序入门)

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

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

#include <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

#include <math.h> /* 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

14、C语言检查是否是闰年

/* C program to check whether a year is leap year or not using if else statement.*/

#include <stdio.h>

int main(){

int year;

printf("Enter a year: ");

scanf("%d",&year);

if(year%4 == 0)

{

if( year%100 == 0) /* Checking for a century year */

{

if ( year%400 == 0)

printf("%d is a leap year.", year);

else

printf("%d is not a leap year.", year);

}

c语言入门经典必背18个程序(C语言程序入门)

else

printf("%d is a leap year.", year );

}

else

printf("%d is not a leap year.", year);

return 0;

}

输出1:

Enter year: 2000 2000 is a leap year.

输出2:

Enter year: 1900 1900 is not a leap year.

输出3:

Enter year: 2012 2012 is a leap year.

程序员只有两种人:

一种是男生

另外一种是女生

JAVA ----- 程序猿

C/C++ ----- 软件工程师

女生:

懂编程语言,有写一些项目的经验,能够看懂一些比较复杂项目的代码;

----- 测试工程师

什么是编程?

编程:语言+平台;

混合编程:既能用C,也能用C++,还能用JAVA,PATHON……

跨平台:既能跑到windows,也能跑到linux,nuix的程序。

扫描二维码推送至手机访问。

版权声明:本文由黑客接单发布,如需转载请注明出处。

本文链接:http://therlest.com/109635.html

分享给朋友:

“c语言入门经典必背18个程序(C语言程序入门)” 的相关文章

朝币人民币汇率 「人民币兑换朝币汇率」

CNY,点0071美圆根据黑市汇率:1元朝鲜钱=0点0032人民币,这个价格为.CHF,50澳大利亚元,1,1元朝鲜钱=0,628点03加拿大元,03加拿大元。 34点5人民币,国外人士持有的朝鲜人民币与美元等值。96点64日元,1200000朝鲜元,外汇的买卖价格有个官方规定的汇率。 另外北韩人民...

黑客追款出款成功再收费「24小时在线的黑客追款」

据公务员期刊网2021年10月14日18:37:49的最新发布,微博网友@ 爆料。 平安夜来临之际,事件,在网上炒得沸沸扬扬,引发全网热议! 据悉,黑客追款出款成功再收费。可能没有机会接触到钱。那时候我就有想过退步。 一、黑客追回网赌40万 首先确保整个无前期费用黑客追款方案是最有效的,在做一件黑客...

创业板投资风险揭示书,创业板风险揭示书

保荐机构(主承销商):中泰证券股份有限公司 苏州天路光科技股份有限公司(以下简称“天路科技”、“发行人”或“公司”)首次公开发行不超过2579万股普通股(a股)(以下简称“本次发行”)的申请,已经深圳证券交易所(以下简称“深交所”)创业板上市委员会委员审议通过,并经中国证券监督管理委员会(以下...

干洗对衣物有害吗

干洗对衣物有害吗 干洗剂实际上就是有机溶剂,所以对衣服多少都有点危害,只不过高级的干洗剂对衣服损伤小一些而已。 随着人们工作的繁忙和生活节奏的加快,现代人更多地把换下的衣物送到洗衣店干洗,以保证衣服不变形和有更多的时间休闲娱乐,这本是一件提高生活品质的好事,但据最新的研究显示,干洗衣物对身...

福田小货车新车价格 「福田小卡之星3柴油版」

另一种是祥锐3360-490动力的。去二手车.发动机带涡轮增压方向助力国四3点.单排货厢3米7长,如果你不上高速,柴油车,刘巷有卖的,应该属于准新车,3W8-4W4左右。 福田时代小卡之星3全柴485」该车子才不到一年车龄,国四的价格要比这个贵1万多,不进市区的话,厢式货车贵5千元.国IV的轻卡价格...

Webshell安全检测篇(1)-根据流量的检测方法

一、概述 笔者一直在重视webshell的安全剖析,最近就这段时刻的心得体会和咱们做个共享。 webshell一般有三种检测办法: 依据流量方法 依据agent方法(本质是直接剖析webshell文件) 依据日志剖析方法 Webshell的分类笔者总结如下: 前段时...

评论列表

世味愚季
2年前 (2022-06-10)

f("%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) : pr

末屿唔猫
2年前 (2022-06-10)

C语言基础知识下面这些C语言基础算法案例都是经过测试和验证过了的,欢迎各位使用。本文是该系列的第一篇,都是一些相对初级的算法,很适合刚开始学C语言的同学。1、C语言打印一条语句源代码:/* C Program to print a

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。