每个初学者都必须知道的 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;
}
OK,就这样。
The post 每个初学者都必须知道的 C 语言编程示例 – 下 first appeared on Linux迷.
版权声明:
作者:zhangchen
链接:https://www.techfm.club/p/29745.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。
THE END
二维码
共有 0 条评论