C language example exercise

Posted by ntlab on Fri, 14 Jan 2022 08:09:44 +0100

To learn programming, you must knock the code, so brushing the questions is the last word.

Title Source: Rookie tutorial C language example

Some minor changes have been made to some topics, and my own learning notes and understanding have been added. The code is not the code in the original tutorial, but I wrote it as an exercise. Each code has been tested and should be no problem, but the shortcomings are still inevitable. If there are problems, please criticize and correct them

Output "Hello, World!"

Use scanf() to receive input and printf() to output "Hello, World!".

#include <stdio.h>

int main(void)
{
	printf("Hello World!\n");

	return 0;
}
//#Is a referenced flag
//include means to quote in English
//<stdio. h> Is one of the library functions
//Format input / output function, abbreviated from standard+input+output h is the header file suffix
//int main void) means that the return value type of the main function is int and the parameter is null
//printf(); Function means format and print, and print means print
//f in printf means format in some places and function in others
//\n is an escape character, indicating a line break
//return 0 means that the return value of the function is 0
Hello World!

Output integer

Use scanf() to receive input and printf() and% d to format the output integer.

#include <stdio.h>

int main(void)
{
	int number = 0;//Define an integer variable a and initialize it to 0\

	scanf("%d", &number);//Format the input function and take the address of number to store the value entered by the user

	printf("%d\n", number);//%d format and print integer parameters and pass parameter a

	return 0;
}
1
1

Output single character

Use scanf() to receive input, and use printf() and% c to format and output a character.

#include <stdio.h>

int main(void)
{
	char ch = 0;//Define a character variable ch and initialize it to 0\

	scanf("%c", &ch);//Format the input function, take the address of ch and store the characters entered by the user

	printf("%c\n", ch);//%d format the printed characters and pass the parameter ch

	return 0;
}
a
a

Output floating point number

Use scanf() to receive input and printf() and% f to output floating-point numbers.

#include <stdio.h>

int main(void)
{
	float number = 0;//Define a single precision floating-point variable number and initialize it to 0\

	scanf("%f", &number);//Format the input function and take the address of number to store the characters entered by the user

	printf("%f\n", number);//%f format and print floating point data, and pass the parameter number

	return 0;
}
1
1.000000

Add two integers

Use scanf() to receive input, printf() and% d to format the output integer.

#include <stdio.h>

int main(void)
{
	int a, b;//Define two integer variables to store two integers

	printf("Please enter two integers and I'll calculate the sum of the two numbers:\n");
	scanf("%d%d", &a, &b);//Take the two addresses of ab to store the data entered by the user
	printf("The sum of the two integers you entered is%d\n", a + b);//The parameters passed are a+b

	return 0;
}
Please enter two integers and I'll calculate the sum of the two numbers:
520 521
 The sum of the two integers you entered is 1041

Multiply two floating point numbers

Enter two floating-point numbers, calculate the product, and keep two decimal places for the result.

#include <stdio.h>

int main(void)
{
	float a, b;

	printf("Please enter two numbers and I'll calculate the product of the two numbers:\n");
	scanf("%f%f", &a, &b);
	printf("The product of the two numbers you entered is%.2f\n", a * b);

	return 0;
}
Please enter two numbers and I'll calculate the product of the two numbers:
520 521.1415926
 The product of the two numbers you entered is 270993.63

Character to ASCII

ASCII value

Control character

ASCII value

Control character

ASCII value

Control character

ASCII value

Control character

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 = 0;
    
	printf("Please enter a character:\n");
	scanf("%c", &ch);
	printf("%d", ch);

	return 0;
}
Please enter a character:
A
65

Multiple groups of input two integers are divided. If there is a remainder, the remainder is output.

#include <stdio.h>

int main(void)
{
	int a, b;

	printf("Please enter two integers:\n");

	while (~scanf("%d%d", &a, &b))//Enter judgment conditions for multiple times, or write scanf ("% d% d", & A, & B)= EOF
	{
		if (a % b == 0)
		{
			printf("The quotient of the two numbers you entered is%d\n", a / b);
		}
		else
		{
			printf("The quotient of the two numbers you entered is%d,The remainder is%d\n", a / b, a % b);
		}
	}

	return 0;
}
Please enter two integers:
9 3
 The quotient of the two numbers you entered is 3
9 5
 The quotient of the two numbers you entered is 1 and the remainder is 4
2021 66
 The quotient of the two numbers you entered is 30 and the remainder is 41

Compare the size of two numbers

int main(void)
{
	int a, b;//Here, take integer as an example, declare two integer variables to store two numbers.

	printf("Please enter two integers:\n");
	scanf("%d%d", &a, &b);

	if (a > b)
	{
		printf("%d>%d", a, b);
	}
	else if(b<a)
	{
		printf("%d>%d", b, a);
	}
	else
	{
		printf("%d=%d", a, b);
	}

	return 0;
}
Please enter two integers:
55 58
58>55

Compare the size of the three numbers

#include <stdio.h>

int main() {
    int a, b, c;//Declare three integer variables

    printf("Please enter three integers\n");
    scanf("%d%d%d", &a, &b, &c);//If the direct comparison should have 10 size relationships, here only compare who is the largest or whether there is equality.

    if (a > b && a > c)
    {
        printf("%d maximum", a);
    }
    else if (b > a && b > c)
    {
        printf("%d maximum", b);
    }
    else if (c > a && c > b)
    {
        printf("%d maximum", c);
    }
    else
    {
        printf("Two or three values are equal");
    }

    return 0;
}
Please enter three integers
45 99 98
99 maximum

Calculates the byte sizes of int, float, double, and char

Use sizeof operator to calculate the byte size of int, float, double and char variables.

#include <stdio.h>

int main() 
{
    int a = 0;
    float b = 0;
    double c = 0;
    char d = 0;

    printf("int The occupied byte size is%d\n", sizeof(int));//sizeof is just an operator, similar to + - * / not a function
    printf("float The occupied byte size is%d\n", sizeof(float));
    printf("double The occupied byte size is%d\n", sizeof(double));
    printf("char The occupied byte size is%d\n", sizeof(char));

    return 0;
}
int The occupied byte size is 4
float The occupied byte size is 4
double The occupied byte size is 8
char The occupied byte size is 1

Swap the values of two numbers

#include <stdio.h>

int main() 
{
    int a, b;
    int temp;//Temporary variable

    printf("Please enter two integers a,b Value of:\n");
    scanf("%d%d", &a, &b);
    printf("a=%d,b=%d\n", a, b);

    temp = a;
    a = b;
    b = temp;

    printf("After exchange a=%d,b=%d\n", a, b);

    return 0;
}
Please enter two integers a,b Value of:
520 521
a=520,b=521
 After exchange a=521,b=520

Multiple sets of inputs to judge odd / even

#include <stdio.h>

int main() 
{
    int a = 0;
    
    printf("Please enter an integer:\n");

    while (~scanf("%d", &a))
    {
        if (a % 2 == 0)
        {
            printf("The number you entered is even\n");
        }
        else if (a % 2 != 0)
        {
            printf("The number you entered is odd\n");
        }
    }

    return 0;
}
Please enter an integer:
2020
 The number you entered is even
2021
 The number you entered is odd

Odd / even numbers within the cycle interval

#include <stdio.h>

int main() 
{
    int n1, n2;
    int i = 0;

    printf("You enter the range you want to output\n(For example (entering 0 to 100 means outputting all even numbers between 0 and 100))\n Please select:>>>");
    scanf("%d%d", &n1, &n2);

    for (i = n1; i >= n1 && i <= n2; i++)
    {
        if (i % 2 == 0)
        {
            printf("%d ", i);
        }
    }

    return 0;
}
You enter the range you want to output
(For example (entering 0 to 100 means outputting all even numbers between 0 and 100))
Please select:>>>33 99
34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98

Multiple sets of inputs to judge vowels / consonants

  English has 26 letters. Vowels include only five letters a, e, i, o and u, and the rest are consonants. y is a semi vowel and semi consonant letter, but it is regarded as a consonant in English.

#include <stdio.h>

int main() 
{
    char ch = 0;

    printf("Please enter multiple English letters\n");
    while (~scanf("%c", &ch) && ch != '\n')//Multiple groups of input encountered a newline stop
    {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
        {
            printf("The letter you entered is a vowel\n");
        }
        else
        {
            printf("The letter you entered is a consonant\n");
        }
    }

    return 0;
}
Please enter multiple English letters
abcDEFG
 The letter you entered is a vowel
 The letter you entered is a consonant
 The letter you entered is a consonant
 The letter you entered is a consonant
 The letter you entered is a vowel
 The letter you entered is a consonant
 The letter you entered is a consonant

Multiple sets of inputs to find the solution of the univariate quadratic equation

   find the root of quadratic equation in one variable: ax 2+bx+c=0.

   enter the values of three real numbers a, B and C, and a is not equal to 0.

#include <stdio.h>
#include <math. h> / / sqrt() requires reference to < math h> Library function header file

int main() 
{
    float a, b, c;
    float x1, x2;
    float det;

    printf("Please enter a,b,c Value of, where a Not equal to 0.\n");

    while (~scanf("%f%f%f", &a, &b, &c))
    {
        det = b * b - 4 * a * c;//Δ=b*b-4*a*c

        if (a != 0 && det > 0)//The equation has two solutions
        {
            x1 = (-b - sqrt(det)) / (2 * a);
            x2 = (-b + sqrt(det)) / (2 * a);
            printf("Root of equation x1=%.2f,x2=%.2f\n", x1, x2);
        }
        else if (a != 0 && det == 0)//The equation has two identical solutions
        {
            x1 = (-b - sqrt(det)) / (2 * a);
            x2 = (-b + sqrt(det)) / (2 * a);
            printf("Root of equation x1=x2=%.2f\n", x1);
        }
        else if (a != 0 && det < 0)//The equation has no real solution
        {
            printf("The equation has no real solution\n");
        }
        
        if (a == 0)//a equals 0
        {
            printf("a If it is equal to 0, the original formula is no longer a univariate quadratic equation, please re-enter.\n");
            break;
        }
    }

    return 0;
}
Please enter a,b,c Value of, where a Not equal to 0.
5 12 13
 The equation has no real solution
1 -2 1
 Root of equation x1=x2=1.00
1 5 1
 Root of equation x1=-4.79,x2=-0.21
0 1 2
a If it is equal to 0, the original formula is no longer a univariate quadratic equation, please re-enter.

Multiple sets of inputs to judge leap years

Leap year is a noun in the Gregorian calendar. Leap years are divided into ordinary leap years and century leap years.

Ordinary leap year: the Gregorian calendar year is a multiple of 4, not a multiple of 100. It is an ordinary leap year (for example, 2004 and 2020 are leap years).

Century leap year: the Gregorian calendar year is a whole hundred, and it must be a multiple of 400 to be a century leap year (for example, 1900 is not a century leap year, and 2000 is a century leap year).

#include <stdio.h>

int main() 
{
    int year = 0;

    printf("Please enter a few years:\n");

    while (~scanf("%d", &year))
    {
        if (year % 4 == 0)
        {
            if (year % 100 == 0)
            {
                printf("This year is a leap year of the century.\n");
            }
            else
            {
                printf("This year is a normal leap year.\n");
            }
        }
        else
        {
            printf("This year is not a leap year.\n");
        }
    }

    return 0;
}
Please enter a few years:
2000 2008 2020 2021
 This year is a leap year of the century.
This year is a normal leap year.
This year is a normal leap year.
This year is not a leap year.

Multiple sets of inputs to judge positive / negative numbers

  the user enters a number to judge whether the number is positive, negative or zero.

#include <stdio.h>

int main()
{
    int number;

    printf("Please enter several integers: \n");
    
    while (~scanf("%d", &number))
    {
        if (number > 0)
        {
                printf("You entered a positive number\n");
        }
        else if (number <0)
        {
            printf("You entered a negative number\n");
        }
        else
        {
            printf("The number you entered is 0\n");
        }
    }

    return 0;
}
Please enter several integers:
-1 0 1
 You entered a negative number
 The number you entered is 0
 You entered a positive number

Multiple groups of input, judgment letters

#include <stdio.h>

int main()
{
    char ch = 0;

    printf("Please enter a string of characters:\n");

    while (~scanf("%c", &ch) && ch != '\n')
    {
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
        {
            printf("The character you entered is a letter\n");
        }
        else
        {
            printf("The character you entered is not a letter\n");
        }
    }

    return 0;
}
Please enter a string of characters:
a@1A
 The character you entered is a letter
 The character you entered is not a letter
 The character you entered is not a letter
 The character you entered is a letter

Calculate the sum of natural numbers

   natural number refers to the number representing the number of objects, that is, starting from 0, 0, 1, 2, 3, 4,... One by one, forming an infinite collective, that is, non negative integer.

#include <stdio.h>
int main()
{
    int i = 0, n = 0, sum = 0;

    printf("How many natural numbers do you need to calculate\n Please enter:");
    scanf("%d", &n);

    for (i = 0; i < n; i++)
    {
        sum += i;
    }

    printf("Natural number 0-%d The sum of is%d\n", n - 1, sum);

    return 0;
}
How many natural numbers do you need to calculate
 Please enter: 10
 Natural number 0-9 The sum of is 45

multiplication table

  for such problems, similar to printing special triangles, I found a method to approximate the general method. For details, see my other blog, Point me in

#include <stdio.h>
int main()
{
    int i, j;

    for (i = 1; i <= 9; i++)//This all starts with 1. Notice
    {
        for (j = 1; j <= i; j++)//Because it is increasing, like 1 2 3 4, there must be i to control it
                                //i is initially 1, and the first line of printing effect is also 1, so here should be < = i
        {
            printf("%d*%d=%d ", j, i, i * j);
        }
        
        printf("\n");//Print outermost loop wrap
    }

    return 0;
}
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

Fibonacci sequence

  Fibonacci sequence refers to such a sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 23337761097159725844181676510946177112865746368

#include <stdio.h>

int main()
{
    int i, n;//i is used to control the loop, and n is used to control the number of items in the Fibonacci sequence input by the user.
    int t1 = 1, t2 = 1, next = 0;//Obviously, the law of t1+t2=next is followed by a cycle

    printf("How many items of Fibonacci sequence do you want to output?\n Please enter:");
    scanf("%d", &n);

    for (i = 0; i < n; i++)
    {
        printf("%d ", t1);

        next = t1 + t2;
        t1 = t2;//Move one to the right, the value of t1 becomes t2, the value of t2 becomes next, and so on.
        t2 = next;
    }

    return 0;
}
How many items of Fibonacci sequence do you want to output?
Please enter: 9
1 1 2 3 5 8 13 21 34

Find the greatest common divisor of two numbers

#include <stdio.h>

int main()
{
    int a, b;
    int i = 0;
    int gy = 0;

    printf("Please enter two integers:\n");
    scanf("%d%d", &a, &b);

    for (i = 1; i <= a && i <= b; i++)//Note that i should start with 1, because 0 cannot be divisor
    {
        if ((a % i == 0) && (b % i == 0))
        {
            gy = i;
        }
    }

    printf("The maximum common divisor of the two integers you entered is%d", gy);
    return 0;
}
Please enter two integers:
25 5
 The maximum common divisor of the two integers you entered is 5

Find the least common multiple of two numbers

#include <stdio.h>

int main()
{
    int a, b;
    int i = 0;
    int gb = 0;
    //This variable is the core. The method of finding the least common multiple is to find the maximum value of these two numbers first,
    //If the maximum value is satisfied and can be divided by the two numbers, then the number is the least common multiple of the two numbers. If not, then + 1

    printf("Please enter two integers:\n");
    scanf("%d%d", &a, &b);

    //First find the maximum of the two numbers
    if (a >= b)
    {
        gb = a;
    }
    else
    {
        gb = b;
    }

    //Start judging
    while (1)
    {
        if (gb % a == 0 && gb % b == 0)
        {
            printf("The maximum common divisor of the two integers you entered is%d", gb);
            break;
        }
        else
        {
            gb++;
        }
    }

    return 0;
}
Please enter two integers:
25 5
 The maximum common divisor of the two integers you entered is 25

Calculate factorial

   the factorial of a positive integer (English: factorial) is the product of all positive integers less than or equal to the number, and the factorial of 0 is 1. The factorial of natural number n is written as n!.

n!=1×2×3×...×n

#include <stdio.h>

int main()
{
    int n = 0;
    int i = 0;
    int jc = 1;

    printf("Please enter multiple natural numbers:\n");
    while (~scanf("%d", &n))
    {
        jc = 1;//After the second set of data is input, jc should be returned to 1

        if (n < 0)
        {
            printf("illegal input\n");
        }
        else if (n == 0)
        {
            printf("0!=1\n");
        }
        else if (n > 0)
        {
            for (i = 1; i <= n; i++)
            {
                jc *= i;
            }
            printf("%d!=%d\n", n, jc);
        }
    }

    return 0;
}
Please enter multiple natural numbers:
-1 0 1 2 3 4 5
 illegal input
0!=1
1!=1
2!=2
3!=6
4!=24
5!=120

Output 26 English letters

#include <stdio.h>

int main(void)
{
    char ch = 0;

    printf("input d Display capital letters, enter x Show lowercase letters: ");
    scanf("%c", &ch);

    if (ch == 'd')
    {
        for (ch = 'A'; ch <= 'Z'; ch++)
        {
            printf("%c ", ch);
        }
    }
    else if (ch == 'x')
    {
        for (ch = 'a'; ch <= 'z'; ch++)
        {
            printf("%c ", ch);
        }
    }
    else
    {
        printf("illegal input\n");
    }

    return 0;
}
input d Display capital letters, enter x Show lowercase letters: d
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Determine the number of digits of the data entered by the user

  the user inputs the data and judges how many digits the number is.

#include <stdio.h>

int main(void)
{
    int number = 0; //Used to store the value entered by the user
    int count = 0;//The number of digits used to record our guess.

    printf("Please enter a number and I will judge the number of digits you entered:\n");
    scanf("%d", &number);

    while (number != 0)//When number/10 equals 0
    {
        number = number / 10;//Update number
        count++;//Update the record first. If you update it first, the number of digits of the previous value will be recorded.
    }

    printf("The number you entered is%d digit", count);

    return 0;
}
Please enter a number and I will judge the number of digits you entered:
123456756
 The number you entered is 9 digits

Calculate a number to the nth power

#include <stdio.h>

int main(void)
{
	int x, y;
	int result = 1;
	int flag = 0;

	printf("Please enter base number:");
	scanf("%d", &x);

	printf("Please enter the index:");
	scanf("%d", &y);

	flag = y;
	while (flag != 0)
	{
		result *= x;
		flag--;
	}

	printf("%d of%d The power is%d\n", x, y, result);
}
Please enter base number: 2
 Please enter index: 10
2 The 10th power of is 1024

Judgement prime

#include <stdio.h>
 
int main()
{
    int n, i, flag = 0;
 
    printf("Enter a positive integer: ");
    scanf("%d",&n);
 
    for(i=2; i<=n/2; ++i)
    {
        // This condition is not a prime number
        if(n%i==0)
        {
            flag=1;
            break;
        }
    }
 
    if (flag==0)
        printf("%d It's a prime",n);
    else
        printf("%d Not prime",n);
    
    return 0;
}
Enter a positive integer:
1
1 Not prime.
Enter a positive integer:
2
2 It's a prime.
Enter a positive integer:
67
67 It's a prime.

Output daffodils

  Narcissistic number is also known as hyperperfect digital invariant (PPDI), Narcissistic number, autoidempotent number, Armstrong number or Armstrong number. Narcissus number refers to a three digit number, and the sum of the three powers of the numbers in each bit is equal to itself (for example, 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153)

#include <stdio.h>

int main(void)
{
	int number;
	int a, b, c;//Store single digit, ten digit and hundred digit respectively.

	printf("Number of all daffodils:\n");

	for (number = 100; number < 999; number++)
	{
		a = number % 10;
		b = number / 10 % 10;
		c = number / 100;

		if (number == (a * a * a + b * b * b + c * c * c))
		{
			printf("%d ", number);
		}
	}

	return 0;
}
Number of all daffodils:
153 370 371 407

Judging palindromes

  let n be any natural number. If the natural number n1 obtained by reversing the bits of n is equal to N, then n is called a palindrome number. For example, if n=1234321, n is called a palindrome number; However, if n=1234567, then n is not a palindrome number.

I also wrote about this topic in a blog before. It is more detailed and can be Kangkang, Point me in

#include <stdio.h>

int main(void)
{
	int number = 0;
	int n_number = 0;
	int last_number = 0;
	int flag = 0;

	printf("Please enter a number:\n");
	scanf("%d", &number);
	flag = number;

	number = flag;
	while (number != 0)
	{
		last_number = number % 10;
		n_number = n_number * 10 + last_number;
		number = number / 10;
	}

	number = flag;
	if (n_number == number)
	{
		printf("The number you entered is the palindrome number.\n");
	}
	else
	{
		printf("The number you entered is not a palindrome number.\n");
	}

	return 0;
}
Please enter a number:
1234321
 The number you entered is the palindrome number.
Please enter a number:
123
 The number you entered is not a palindrome number.

Find all the factors of a number

int main(void)
{
	int number = 0;
	int i = 0;

	printf("Please enter an integer\n");
	scanf("%d", &number);

	for (i = 1; i <= number; i++)//0 cannot be divisor. Start with 1
	{
		if (number % i == 0)
		{
			printf("%d ", i);
		}
	}

	return 0;
}
Please enter an integer
24
1 2 3 4 6 8 12 24

Create various triangular patterns

  there is an approximate general method for this kind of problem, which I wrote in my previous blog, and it is a little more complete than this, blog Point me in

Regular right triangle

#include <stdio.h>

int main(void)
{
	int n;
	int i, j;

	printf("Please enter the number of sides of the right triangle you want to output:\n");
	scanf("%d", &n);

	for (i = 0; i < n; i++)
	{
		for (j = 0; j < i+1 ; j++)
		{
			printf("* ");
		}
		printf("\n");
	}

	return 0;
}
Please enter the number of sides of the right triangle you want to output:
5
*
* *
* * *
* * * *
* * * * *

Flip right triangle

int main(void)
{
	int n;
	int i, j;

	printf("Please enter the number of sides of the inverted right triangle you want to output:\n");
	scanf("%d", &n);

	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n - i ; j++)
		{
			printf("* ");
		}
		printf("\n");
	}

	return 0;
}
Please enter the number of sides of the inverted right triangle you want to output:
5
* * * * *
* * * *
* * *
* *
*

Positive digital right triangle

#include <stdio.h>

int main(void)
{
	int n;
	int i, j;

	printf("Please enter the number of sides of the right triangle you want to output:\n");
	scanf("%d", &n);

	for (i = 0; i < n; i++)
	{
		for (j = 0; j < i + 1 ; j++)
		{
			printf("%d ",j);
		}
		printf("\n");
	}

	return 0;
}
Please enter the number of sides of the right triangle you want to output:
5
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4

Flip number right triangle

#include <stdio.h>

int main(void)
{
	int n;
	int i, j;

	printf("Please enter the number of sides of the flip right triangle you want to output:\n");
	scanf("%d", &n);

	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n - i ; j++)
		{
			printf("%d ",j);
		}
		printf("\n");
	}

	return 0;
}
Please enter the number of sides of the flip right triangle you want to output:
5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0

Regular letter right triangle

#include <stdio.h>

int main(void)
{
	int n;
	int i, j;

	printf("Please enter the number of sides of the letter right triangle you want to output:\n");
	scanf("%d", &n);

	for (i = 0; i < n; i++)
	{
		for (j = 0; j < i + 1 ; j++)
		{
			printf("%c ",j + 65);
		}
		printf("\n");
	}

	return 0;
}
Please enter the number of sides of the letter right triangle you want to output:
5
A
A B
A B C
A B C D
A B C D E

Flip letter right triangle

#include <stdio.h>

int main(void)
{
	int n;
	int i, j;

	printf("Please enter the number of sides of the right triangle of the inverted letter you want to output:\n");
	scanf("%d", &n);

	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n - i ; j++)
		{
			printf("%c ",j + 65);
		}
		printf("\n");
	}

	return 0;
}
Please enter the number of sides of the right triangle of the inverted letter you want to output:
5
A B C D E
A B C D
A B C
A B
A

Regular pyramid triangle

#include <stdio.h>

int main(void)
{
	int n;
	int i, j;

	printf("Please enter the number of rows of the pyramid triangle you want to output:\n");
	scanf("%d", &n);

	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n - i ; j++)
		{
			printf(" ");
		}
		for (j = 0; j < i + 1; j++)
		{
			printf("* ");
		}
		printf("\n");
	}

	return 0;
}
Please enter the number of rows of the pyramid triangle you want to output:
5
     *
    * *
   * * *
  * * * *
 * * * * *

Flip pyramid triangle

#include <stdio.h>

int main(void)
{
	int n;
	int i, j;

	printf("Please enter the number of rows of the inverted pyramid triangle you want to output:\n");
	scanf("%d", &n);

	for (i = 0; i < n; i++)
	{
		for (j = 0; j < i + 1; j++)
		{
			printf(" ");
		}
		for (j = 0; j < n - i; j++)
		{
			printf("* ");
		}

		printf("\n");
	}

	return 0;
}
Please enter the number of rows of the inverted pyramid triangle you want to output:
5
 * * * * *
  * * * *
   * * *
    * *
     *

diamond

#include <stdio.h>
int main(void)
{
    int n = 0;
    int i = 0, j = 0;

    while (~scanf("%d", &n))
    {
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < i; j++)
            {
                printf(" ");
            }
            for (j = 0; j < n - i; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
    }
    
    return 0;
}
3
   * 
  * * 
 * * * 
* * * * 
 * * * 
  * * 
   * 

Yang Hui triangle

#include <stdio.h>

int main()
{
    int rows = 0;//Number of rows
    int number = 1;//Numeric value
    int space = 0;//Space
    int i, j = 0;//Control cycle

    printf("Number of rows: ");
    scanf("%d", &rows);

    for (i = 0; i < rows; i++)
    {
        for (space = 1; space <= rows - i; space++)
            printf("  ");

        for (j = 0; j <= i; j++)
        {
            if (j == 0 || i == 0)
                number = 1;
            else
                number = number * (i - j + 1) / j;

            printf("%4d", number);
        }
        printf("\n");
    }

    return 0;
}
Number of rows: 8
                   1
                 1   1
               1   2   1
             1   3   3   1
           1   4   6   4   1
         1   5  10  10   5   1
       1   6  15  20  15   6   1
     1   7  21  35  35  21   7   1

Freudian triangle

#include <stdio.h> 

int main()
{
    int i, j, l;
    int n;

    printf("How many lines do you want to output\n Please enter:");
    scanf("%d", &n);

    for (i = 0, l = 1; i < n; i++)//Add a new variable l
    {
        for (j = 0; j < i + 1; j++, l++)//Let l add 1 after the inner loop, and the second line will start with 2
        {
            printf("%4d", l);
        }
        printf("\n");
    }
    return 0;
}
How many lines do you want to output
 Please enter: 8
   1
   2   3
   4   5   6
   7   8   9  10
  11  12  13  14  15
  16  17  18  19  20  21
  22  23  24  25  26  27  28
  29  30  31  32  33  34  35  36

Output data in tabular form

#include <stdio.h> 

int main()
{
    int i, j, l;
    int n;

    printf("How many lines do you want to output\n Please enter:");
    scanf("%d", &n);

    for (i = 0, l = 1; i < n; i++)
    {
        for (j = 0; j < 10; j++, l++)
        {
            printf("%4d", l);
        }
        printf("\n");
    }
    return 0;
}
How many lines do you want to output
 Please enter: 10
   1   2   3   4   5   6   7   8   9  10
  11  12  13  14  15  16  17  18  19  20
  21  22  23  24  25  26  27  28  29  30
  31  32  33  34  35  36  37  38  39  40
  41  42  43  44  45  46  47  48  49  50
  51  52  53  54  55  56  57  58  59  60
  61  62  63  64  65  66  67  68  69  70
  71  72  73  74  75  76  77  78  79  80
  81  82  83  84  85  86  87  88  89  90
  91  92  93  94  95  96  97  98  99 100

Implement a simple calculator

# include <stdio.h>

int main()
{

    char ch;
    double n1, n2;

    printf("Input operator (+, -, *, \\):\n ");
    scanf("%c", &ch);

    printf("Enter two numbers: ");
    scanf("%lf %lf", &n1, &n2);

    switch (ch)
    {
    case '+':
        printf("%.2lf + %.2lf = %.2lf", n1, n2, n1 + n2);
        break;

    case '-':
        printf("%.2lf - %.2lf = %.2lf", n1, n2, n1 - n2);
        break;

    case '*':
        printf("%.2lf * %.2lf = %.2lf", n1, n2, n1 * n2);
        break;

    case '/':
        printf("%.2lf / %.1lf = %.2lf", n1, n2, n1 / n2);
        break;

    default:
        printf("illegal input\n");
    }

    return 0;
}
Input operator (+, -, *, \):
 +
Enter two numbers: 2020 2021
2020.00 + 2021.00 = 4041.00