C/C++ Programming Learning-Week 5 ③ The volume of the cone

Topic link

Title description

Suan Tou Jun just learned the knowledge of three-dimensional geometry related to the cone. In order to test his learning effect, Hua Yemei gave him a question, given the radius r and height h of the bottom circle of a cone, calculate the cone's volume.

Mai Mei Huaye may keep asking him, Juntou is a little impatient, and decides to write a program to let the program answer the questions for him. But Jun Suan has never learned programming, and needs your help.

Mr. Suantou is afraid that you don't know the formula for calculating the volume of a cone problem. He tells you that his volume is one-third of a cylinder.

The value of pi can be approximately replaced with 3.1415926.

Sample Input

1 1

Sample Output

1.047

Ideas

The volume formula of the cone V = π * r * r * h / 3, directly calculate the volume of the cone.

C language code:

#include<stdio.h>
int main()
{
    
    
	double pi = 3.1415926, r, h, ans;	//pi 尽量用题目中给的,不然可能会出错
	scanf("%lf %lf", &r, &h);
	ans = pi * r * r * h / 3;
	printf("%.3lf", ans);
	return 0;
}

C++ code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	double r, h;
	while(cin >> r >> h)
		printf("%.3lf\n", 1.0 / 3.0 * 3.1415926 * r * r * h);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/112909382