1.输出“Hello, World!"
#include<stdio.h>
int main(void)
{
printf("Hello, World!\n");
}
2.输出整数
#include<stdio.h>
int main(void)
{
int num;
printf("Please input a number:");
scanf_s("%d", &num);
printf("The number you input is:%d\n", num);
}
3.输出单个字符
#include<stdio.h>
int main(void)
{
char ch;
printf("Please input a char:");
scanf_s("%c", &ch);
printf("The char you input is:%c\n", ch);
}
4.输出浮点数
int main(void)
{
float fnum;
printf("Please input a float number:");
scanf_s("%f", &fnum);
printf("The float number you input is:%.6f\n", fnum);
}
5.输出双精度数
#include<stdio.h>
int main(void)
{
double dnum;
printf("Please input a double number:");
scanf_s("%lf", &dnum);
printf("The double number you input is:%.12lf\n", dnum);
}
6.两个数字相加
#include<stdio.h>
int main(void)
{
int fnum, snum, sum;
printf("Please input two numbers(divided by blank):");
scanf_s("%d %d", &fnum, &snum);
sum = fnum + snum;
printf("The sum of the two numbers is: %d\n", sum);
}
7.两个浮点数相乘
#include<stdio.h>
int main(void)
{
double fnum, snum, product;
printf("Please input two double numbers(divided by blank):");
scanf_s("%lf %lf", &fnum, &snum);
product = fnum * snum;
printf("The product of the two double numbers is: %.3lf\n", product);
}
8.字符转ASCII码
ASCII码表
ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 |
---|
0 | NUT | 32 | (space) | 64 | @ | 96 | 、 | 1 | SOH | 33 | ! | 65 | A | 97 | a | 2 | STX | 34 | " | 66 | B | 98 | b | 3 | ETX | 35 | # | 67 | C | 99 | c | 4 | EOT | 36 | $ | 68 | D | 100 | d | 5 | ENQ | 37 | % | 69 | E | 101 | e | 6 | ACK | 38 | & | 70 | F | 102 | f | 7 | BEL | 39 | ' | 71 | G | 103 | g | 8 | BS | 40 | ( | 72 | H | 104 | h | 9 | HT | 41 | ) | 73 | I | 105 | i | 10 | LF | 42 | * | 74 | J | 106 | j | 11 | VT | 43 | + | 75 | K | 107 | k | 12 | FF | 44 | , | 76 | L | 108 | l | 13 | CR | 45 | - | 77 | M | 109 | m | 14 | SO | 46 | . | 78 | N | 110 | n | 15 | SI | 47 | / | 79 | O | 111 | o | 16 | DLE | 48 | 0 | 80 | P | 112 | p | 17 | DCI | 49 | 1 | 81 | Q | 113 | q | 18 | DC2 | 50 | 2 | 82 | R | 114 | r | 19 | DC3 | 51 | 3 | 83 | S | 115 | s | 20 | DC4 | 52 | 4 | 84 | T | 116 | t | 21 | NAK | 53 | 5 | 85 | U | 117 | u | 22 | SYN | 54 | 6 | 86 | V | 118 | v | 23 | TB | 55 | 7 | 87 | W | 119 | w | 24 | CAN | 56 | 8 | 88 | X | 120 | x | 25 | EM | 57 | 9 | 89 | Y | 121 | y | 26 | SUB | 58 | : | 90 | Z | 122 | z | 27 | ESC | 59 | ; | 91 | [ | 123 | { | 28 | FS | 60 | < | 92 | / | 124 | | | 29 | GS | 61 | = | 93 | ] | 125 | } | 30 | RS | 62 | > | 94 | ^ | 126 | ~ | 31 | US | 63 | ? | 95 | _ | 127 | DEL |
#include<stdio.h>
int main(void)
{
char ch;
printf("Please input a char:");
scanf_s("%c", &ch);
printf("The char you input is: %c\n", ch);
printf("The ASCII of the char you input is: %d\n", ch);
}
9.两数相除?
#include<stdio.h>
int main(void)
{
int fnum, snum, quotient, remainder;
printf("Please input two numbers(divided by blank):");
scanf_s("%d %d", &fnum, &snum);
while(snum == 0) {
printf("Error input, please input again:");
scanf_s("%d %d", &fnum, &snum);
}
quotient = fnum / snum;
remainder = fnum % snum;
printf("The quotient of the two numbers is: %d\n", quotient);
printf("The remainder of the two numbers is: %d\n", remainder);
}
10.数值比较
#include<stdio.h>
int main(void)
{
int fnum, snum;
printf("Please input two numbers(divided by blank):");
scanf_s("%d %d", &fnum, &snum);
if (fnum > snum) {
printf("The first number is bigger than the second.\n");
}
else if (fnum < snum) {
printf("The first number is smaller than the second.\n");
}
else {
printf("The first number is equal to the second.\n");
}
}
?
?
?
?
?
|