相关教程:
每个初学者都必须知道的 C 语言编程示例 - 上 https://www.linuxmi.com/c-programming-examples.html
11.C编程 - 生成乘法表
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编程 - 斐波那契数列
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编程 - 数字为正或负
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编程 - 在不使用库函数的情况下反转字符串
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编程 - 显示英文字母表
int main()
{
char c;
for(c='A'; c<='Z'; ++c)
printf("%c ",c);
return 0;
}
16.C编程 - 回文串
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编程 - 阿姆斯特朗编号
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编程 - 创建金字塔
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编程 - 反转数字
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编程 - 交换两个数字
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迷的精彩文章。