Section 23 Use if statement to realize branch structure

1. Single branch selection structure and if statement

#include <stdio.h>
#include <math.h>
int main()
{
    
    
	double a = 3, b = 4, c = 5, s, area;
	if (a + b > c && b + c > a && a + c > b)
	{
    
    
		s = (a + b + c) / 2;
		area = sqrt(s * (s - a) * (s - b) * (s - c));
		printf("area=%7.2f\n", area);
	}
}

2. Double branch selection structure and if statement

#include <stdio.h>
#include <math.h>
int main()
{
    
    
	double result, n, d;
	scanf_s("%lf %lf", &n, &d);
	if (d != 0)
	{
    
    
		result = n / d;
		printf("%f\n", result);
	}
	else
	{
    
    
		printf("除数不能为0!\n");
	}
}

3. Example 1: A program to find the area of ​​a triangle

#include <stdio.h>
#include <math.h>
int main()
{
    
    
	double a = 3, b = 4, c = 5, s, area;
	if (a + b > c && b + c > a && a + c > b)
	{
    
    
		s = (a + b + c) / 2;
		area = sqrt(s * (s - a) * (s - b) * (s - c));
		printf("area=%7.2f\n", area);
	}
	else
	{
    
    
		printf("三边数值不能构成三角形\n");
	}
}

4. Example 2: Find the roots of a quadratic equation in one unknown

#include <stdio.h>
#include <math.h>
int main()
{
    
    
	double a, b, c, x1, x2;
	scanf_s("%lf %lf %lf", &a, &b, &c);
	if ((b * b - 4 * a * c) >= 0)
	{
    
    
		x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
		x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
		printf("x1=%.2f\n", x1);
		printf("x2=%.2f\n", x2);
	}
	else
	{
    
    
		printf("方程无实根!\n");
	}
	return 0; 
}
运行结果:
-6 3 7
x1=-0.86
x2=1.36

Five. Typical mistakes using if statements

#include <stdio.h>
int main()
{
    
    
	int a = 0;
	if (a == 100);
	printf("满分!\n");
}
#include <stdio.h>
int main()
{
    
    
	int a = 0;
	if (a == 100);
	printf("满分!\n");
	else
		printf("再努力!");
}
#include <stdio.h>
int main()
{
    
    
	int a = 0;
	if (a == 100)
		printf("满分!\n");
		printf("哈哈!\n");	
	else
		printf("再努力!");
}

Six. Practical projects

1. Enter a number to determine whether it can be divisible by 3 or 5. If it is divisible by at least one of these two numbers, print this number, otherwise it will not print.

#include <stdio.h>
int main()
{
    
    
	int x;
	scanf_s("%d", &x);
	if (x % 3 == 0 || x % 5 == 0)
	{
    
    
		printf("%d\n", x);
	}
}

2. Input integers a and b. If the sum of the two numbers is greater than 100, then output the two numbers and the number in the hundreds place , otherwise output the sum of the two numbers.

#include <stdio.h>
int main()
{
    
    
	int a, b, sum;
	scanf_s("%d %d", &a, &b);
	sum = a + b;
	if (sum > 100)
	{
    
    
		sum /= 100;
		sum %= 10;
		printf("两数和百位上的数字为%d\n", sum);
	}
	else
	{
    
    
		printf("两数和为%d\n", sum);
	}
}

3. Compile the program, calculate the value of the piecewise function and output it (x is just an integer)

#include <stdio.h>
int main()
{
    
    
	int x, y;
	scanf_s("%d", &x);
	if (x >= 1)
	{
    
    
		y = x - 1;
	}
	else
	{
    
    
		y = -x + 1;
	}
	printf("y=%d\n", y);
}

4. Write a program to find the y value (the x value is input by the keyboard and evaluated by the trigonometric formula).

#include <stdio.h>
#include <math.h>
int main()
{
    
    
	double x, y;
	const PI = 3.1415926;
	scanf_s("%lf", &x);
	x = x * PI / 180;
	if (x >= 0)
	{
    
    
		y = (sin(x) + cos(x)) / 2;
	}
	else
	{
    
    
		y = (sin(x) - cos(x)) / 2;
	}
	printf("y=%.2f\n", y);
}

5. Calculate weekly wage by piecewise function

#include <stdio.h>
int main()
{
    
    
	int rate, hour;
	double salary;
	scanf_s("%d %d", &rate, &hour);
	if (hour > 40)
	{
    
    
		salary = 40 * rate + (hour - 40) * rate * 1.5;
	}
	else
	{
    
    
		salary = rate * hour;
	}
	printf("%.2f", salary);
}

6. Input hours and minutes, and output in the form of hh:mm. Example: input 14 25, output 14:25, input 8 9, output 08:09

#include <stdio.h>
int main()
{
    
    
    int h, m;
    printf("输入小时和分钟");
    scanf_s("%d %d", &h, &m);
    printf("%02d:%02d\n", h, m);
}
#include <stdio.h>
int main()
{
    
    
    int h, m;
    printf("输入小时和分钟: ");
    scanf_s("%d %d", &h, &m);
    if (h < 10)
        printf("0");
    printf("%d:", h);
    if (m < 10)
        printf("0");
    printf("%d\n", m);
}

Guess you like

Origin blog.csdn.net/m0_51439429/article/details/114904653