1、输出9*9口诀。共9行9列,i控制行,j控制列。
//
// Created by 冲哥 on 2020/12/29.
//
#include "stdio.h"
int main() {
int i, j, result;
for (i = 1; i < 10; i++) {
for (j = 1; j <= i; j++) {
result = i * j;
printf("%d*%d=%-4d", i, j, result);
}
printf("\n");
}
return 0;
}
//
// Created by 冲哥 on 2020/12/29.
//
#include "stdio.h"
int main() {
long f1, f2;
int i;
f1 = f2 = 1;
for (i = 1; i <= 20; i++) {
printf("%12ld %12ld", f1, f2);
if (i % 2 == 0) printf("\n");/*控制输出,每行四个*/
f1 = f1 + f2; /*前两个月加起来赋值给第三个月*/
f2 = f1 + f2; /*前两个月加起来赋值给第三个月*/
}
return 0;
}
程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
//
// Created by 冲哥 on 2020/12/29.
//
#include "stdio.h"
#include "math.h"
int main() {
int m, i, k, h = 0, leap = 1;
printf("\n");
for (m = 101; m <= 200; m++) {
k = sqrt(m + 1);
for (i = 2; i <= k; i++)
if (m % i == 0) {
leap = 0;
break;
}
if (leap) /*内循环结束后,leap依然为1,则m是素数*/
{
printf("%-4d", m);
h++;
if (h % 10 == 0)
printf("\n");
}
leap = 1;
}
printf("\nThe total is %d", h);
return 0;
}
//
// Created by 冲哥 on 2020/12/29.
//
#include "stdio.h"
int main() {
static int k[10];
int i, j, n, s;
for (j = 2; j < 1000; j++) {
n = -1;
s = j;
for (i = 1; i < j; i++) {
if ((j % i) == 0) {
n++;
s = s - i;
k[n] = i;
}
}
if (s == 0) {
printf("%d is a 完数: ", j);
for (i = 0; i < n; i++)
printf("%d,", k[i]);
printf("%d\n", k[n]);
}
}
return 0;
}
//
// Created by 冲哥 on 2020/12/29.
//
#include "stdio.h"
int main() {
int i, j, a[6][6];
for (i = 0; i <= 5; i++) {
a[i][i] = 1;
a[i][0] = 1;
}
for (i = 2; i <= 5; i++)
for (j = 1; j <= i - 1; j++)
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
for (i = 0; i <= 5; i++) {
for (j = 0; j <= i; j++)
printf("%4d", a[i][j]);
printf("\n");
}
return 0;
}
//
// Created by 冲哥 on 2020/12/29.
//
#include "stdio.h"
#include <string.h>
int main() {
char c[200], c1;
int i, j, k;
printf("Enter a string: ");
scanf("%s", c);
k = strlen(c);
for (i = 0, j = k - 1; i < k / 2; i++, j--) {
c1 = c[i];
c[i] = c[j];
c[j] = c1;
}
printf("%s\n", c);
return 0;
}
//
// Created by 冲哥 on 2020/12/29.
//
#include "stdio.h"
#include <string.h>
int main() {
char s1[6] = "thisis";
char s2[5] = "is";
printf("%d\n", search(s1, s2));
return 0;
}
int search(char s1[], char s2[]) {
int i = 0, j, len = strlen(s2);
while (s1[i]) {
for (j = 0; j < len; j++)
if (s1[i + j] != s2[j]) break;
if (j >= len)return i;
else i++;
}
return -1;
}
//
// Created by 冲哥 on 2020/12/29.
//
#include <stdio.h>
#include <string.h>
#include<string.h>
int main() {
char s[100];
int i, j, n;
printf("输入字符串:\n");
gets(s);
n = strlen(s);
for (i = 0, j = n - 1; i < j; i++, j--)
if (s[i] != s[j]) break;
if (i >= j) printf("是回文串\n");
else printf("不是回文串\n");
return 0;
}
//
// Created by 冲哥 on 2020/12/29.
//
#include <stdio.h>
struct student {
int num;//学号
char *name;//姓名
char sex;//性别
int age;//年龄
} stu[5] = {{1001, "张三", 'F', 18},
{1002, "李四", 'M', 28},
{1003, "王五", 'F', 34},
{1004, "赵六", 'F', 25},
{1005, "前七", 'M', 19}};
int main() {
int i;
struct student *ps;
printf("Num \tName\t\t\tSex\tAge\t\n");
//用指针变量输出结构体数组元素。
for (ps = stu; ps < stu + 5; ps++)
printf("%d\t%-10s\t\t%c\t%d\t\n", ps->num, ps->name, ps->sex, ps->age);
printf("************************************\n");
//用数组下标法输出结构体数组元素学号和年龄。
for (i = 0; i < 5; i++)
printf("%d\t%d\t\n", stu[i].num, stu[i].age);
return 0;
}