每个初学者都必须知道的 C 语言编程示例 – 下

开发 后端
C 语言是一种过程式编程语言。它最初是由 Dennis Ritchie 开发的,作为一种系统编程语言来编写操作系统。C 语言的主要特性包括对内存的底层访问、简单的关键字集和干净的风格,这些特性使 C 语言适合于系统编程,如操作系统或编译器开发。

相关教程:

每个初学者都必须知道的 C 语言编程示例 - 上  ​​https://www.linuxmi.com/c-programming-examples.html​

11.C编程 - 生成乘法表

#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=10; ++i)
{
printf("%d * %d = %d \n", n, i, n*i);
}
return 0;
}

12.C编程 - 斐波那契数列

#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm = 0;
printf("Enter the number of terms: ");
scanf("%d",&n);
// displays the first two terms which is always 0 and 1
printf("Fibonacci Series: %d, %d, ", t1, t2);
// i = 3 because the first two terms are already dislpayed
for (i=3; i <= n; ++i)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
printf("%d, ",nextTerm);
}
return 0;
}

13.C编程 - 数字为正或负

#include <stdio.h>
int main()
{
double number;
printf("输入一个数字: ");
scanf("%lf", &number);
if (number <= 0.0)
{
if (number == 0.0)
printf("您输入了 0。");
else
printf("您输入了一个负数。");
}
else
printf("你输入了一个正数。");
return 0;
}

14.C编程 - 在不使用库函数的情况下反转字符串

#include<stdio.h>
#include<string.h>
int main()
{
char str[100],rev[100];
int i,len=0;
printf("请输入字符串");
gets(str);
for(i=0;i<=100;i++)
{
if(str[i]=='\0')
{
break;
}
len++;
}
for(i=0;i<=len-1;i++)
{
rev[i] = str[len-i-1];
}
printf("字符串的反向是 %s",rev);
return 0;
}

15.C编程 - 显示英文字母表

#include <stdio.h>
int main()
{
char c;
for(c='A'; c<='Z'; ++c)
printf("%c ",c);
return 0;
}

16.C编程 - 回文串

#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
// reversed integer is stored in variable
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
// palindrome if orignalInteger and reversedInteger is equal
if(originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);
return 0;
}

17.C编程 - 阿姆斯特朗编号

#include <stdio.h>
int main()
{
int number, originalNumber, remainder, result = 0;
printf("Enter a three digit integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
return 0;
}

18.C编程 - 创建金字塔

#include<stdio.h>
int main() {
int i, j;
int num;
printf("Enter the number of Digits :");
scanf("%d", &num);
for (i = 0; i <= num; i++) {
for (j = 0; j < i; j++) {
printf("%d ", i);
}
printf("\n");
}
return 0;
}

19.C编程 - 反转数字

#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
printf("Reversed Number = %d",reversedNumber);
return 0;
}

20.C编程 - 交换两个数字

#include <stdio.h>
int main()
{
double firstNumber, secondNumber, temporaryVariable;
printf("Enter first number: ");
scanf("%lf", &firstNumber);
printf("Enter second number: ");
scanf("%lf",&secondNumber);
// Value of firstNumber is assigned to temporaryVariable
temporaryVariable = firstNumber;
// Value of secondNumber is assigned to firstNumber
firstNumber = secondNumber;
// Value of temporaryVariable (which contains the initial value of firstNumber) is assigned to secondNumber
secondNumber = temporaryVariable;
printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);
printf("After swapping, secondNumber = %.2lf", secondNumber);
return 0;
}

这些 C 编程示例,可以很好的帮助初学者进行编码之旅。如果您需要更多 C 编程示例,请在下面发表评论并分享您的疑问。

感谢Linux迷的精彩文章。

责任编辑:庞桂玉 来源: Linux公社
相关推荐

2022-04-12 11:20:11

C 语言Linux编程

2018-06-29 14:42:54

编程Python开发

2023-02-16 13:31:22

2012-03-01 13:17:40

Java

2013-04-02 09:23:37

2010-01-14 17:18:17

C++语言

2023-11-01 08:01:48

数据结构软件工程

2010-03-15 18:55:22

Java编程语言

2021-12-09 05:58:51

编程语言开发代码

2014-08-06 10:19:38

编程语言程序员

2020-04-02 15:37:58

数据结构存储

2020-03-04 11:10:14

数据结构程序员编译器

2021-01-12 10:06:57

编程语言开发

2018-04-24 11:09:13

LinuxShellfc

2023-04-11 15:22:06

JavaScript开发前端

2023-01-10 08:12:52

Java程序员负载均衡

2015-12-21 11:45:27

C语言常见问题错误

2010-03-10 17:09:26

Python语言

2011-07-11 17:45:13

java

2017-03-10 17:00:28

编程语言
点赞
收藏

51CTO技术栈公众号