文章目录
注:小编是一个C语言初学者,所以可能会犯一些很弱智的错误,希望大家勿怪。
C语言练习代码库
练习1:输入数字打印对应的星期
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
//声明变量并初始化
int day = 1;
//输入
again:
scanf("%d", &day);
//判断并打印
switch (day)
{
case 1:
printf("星期一");
break;
case 2:
printf("星期二");
break;
case 3:
printf("星期三");
break;
case 4:
printf("星期四");
break;
case 5:
printf("星期五");
break;
case 6:
printf("星期六");
break;
case 7:
printf("星期天");
break;
//输入比7大的数字时重新输入
default :
printf("输入错误,请重新输入");
goto again;
}
return 0;
}
练习2:在屏幕上打印1~10的值
2.1 使用while循环
#include <stdio.h>
int main(void)
{
//声明变量并初始化
int a = 1;
while (a <= 10)
{
printf("%d ", a);
a++;
}
return 0;
}
2.2 使用for循环
#include <stdio.h>
int main(void)
{
//declara a variable but don't initialize it.
int a;
//use a for loop to print integers from 1 to 10
for (a = 1; a <= 10; a++)
printf("%d ", a);
return 0;
}
练习3:输入一个正整数,逆序打印这个整数的每一位
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
//declare a variable and initialize it.
int a = 1;
//input a positive integer
scanf("%d", &a);
//print each bit of this integer in reverse order on the screen
while (a)
{
printf("%d ", a % 10);
a /= 10;
}
return 0;
}
练习4:计算1~100之间3的倍数的数字之和
#include <stdio.h>
int main(void)
{
//declara variables.
int a;
int sum = 0;
//use a for loop to traverse all multiples of 3 between 1 and 100.
for (a = 1; a <= 100; a++)
if (a % 3 == 0)
{
sum += a;
printf("%d ", a);
continue;
}
//print sum
printf("\n1~100之间3的倍数的数字之和:%d\n", sum);
return 0;
}
练习5.2
//判断是否是闰年
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int is_Leap_Year(int num);
int main(void) {
int year = 0;
while (scanf("%d", &year) == 1) {
if (is_Leap_Year(year) == 1) {
printf("%d是闰年\n", year);
}
else {
printf("%d年不是闰年\n", year);
}
}
}
int is_Leap_Year(int num) {
int flag = 1;//假设是闰年
if((num % 4 == 0 && num % 100 != 0) || (num % 400 == 0)) {
flag = 1;//是闰年
}
else {
flag = 0;//不是闰年
}
return flag;
}
练习5:判断是否是闰年
练习5.1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
//declara a variable and don't initialize it.
int year;
//iuput the value of the variable.
scanf("%d", &year);
//determine whether it is a leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf("%d年是闰年\n", year);
}
else
{
printf("%d年不是闰年\n",year);
}
}
练习6:判断输入数字的位数
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
//declare variables.
int a = 0;
int count = 0;
//input the value of the variable a.
scanf("%d", &a);
//determine the digits of the input number a.
for (count = 1; count != 0; count++)
{
a /= 10;
if (a == 0)
break;
else
continue;
}
printf("输入的整数是一个%d位数", count);
return 0;
}
练习7:找出100~200之间的素数(质数)
练习次数1:
#include <stdio.h>
#include <math.h>
int main()
{
//declara variables.
int a;
int b;
int c;
//use for loops to traverse all numbers from 100 to 200 and use if statements to find all prime numbers.
for (a = 101; a <= 200; a+=2)
{
c = 1;
for (b = 2; b <= sqrt(a); b++)
{
if (a % b == 0)
{
c = 0;
break;
}
}
if (c == 1)
printf("%d ", a);
}
return 0;
}
练习8. 多组输入数据,输入的数据作为三角形的三个边,判断能否形成三角形以及三角形的类型
#include <stdio.h>
int main()
{
//声明变量
int a, b, c;
//多组输入
while (scanf("%d %d %d", &a, &b, &c) == 3)
{
//判断
if (a + b > c && a + c > b && b + c > a)
{
if (a == b && b == c)
{
printf("Equilateral triangle!\n");
}
else if (a == b || b == c || a == c)
{
printf("Isosceles triangle!\n");
}
else
{
printf("Ordinary triangle!\n");
}
}
else
{
printf("Not a triangle!\n");
}
}
return 0;
}
练习9. 在屏幕上打印乘法口诀表
练习9.1
//自定义打印乘法口诀表multiplication table
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void print_Multiplication_Table(int num);
int main(void) {
while (1) {
int num = 0;
printf("请输入:");
scanf("%d", &num);
print_Multiplication_Table(num);
}
}
void print_Multiplication_Table(int num) {
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= i; j++) {
printf("%d × %d = %d\t", j, i, i * j);
}
printf("\n");
}
}
练习9.2
#include <stdio.h>
int main(void)
{
//声明变量
int a;
int b;
int x;
//循环
for (a = 1; a <= 9; a++)
{
for (b = 1; b <= a; b++)
{
x = a * b;
printf("%d × %d = %d\t", a, b, x);
}
}
return 0;
}
练习10. 猜数字游戏
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game()
{
int r = rand() % 100 + 1;
int guess = 0;
while (1)
{
printf("请猜数字>:");
scanf("%d", &guess);
if (guess < r)
{
printf("猜小了\n");
}
else if (guess > r)
{
printf("猜大了\n");
}
else
{
printf("恭喜你,猜对了\n");
break;
}
}
}
void menu()
{
printf("***********************\n");
printf("****** 1. play ******\n");
printf("****** 0. exit ******\n");
printf("***********************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏结束\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
}while (input);
return 0;
}