TOSPSHIRIQLARNI BAJARISHDA FUNKSIYADAN FOYDALANISH ZARUR.
Topshiriqlar
Uch xonali son berilgan. Berilgan sonning raqamlari yig‘indisini topuvchi dastur tuzing.
Input:
|
Output:
|
N kiriting: 867
|
21
|
To‘rt xonali son berilgan. Berilgan sonning raqamlari ko‘paytmasini topuvchi dastur tuzing.
Input:
|
Output:
|
N kiriting: 4368
|
576
|
To‘g‘ri burchakli uchburchakning katetlari berilgan. Berilgan uchburchakning medianasini topuvchi dastur tuzing (Yaxlitlashda format() dan foydalaning).
Input:
|
Output:
|
b,c ni kiritng: 5,6
|
15.25
|
Uchburchakning tomonlari berilgan. Uning a tomoniga tushuvchi medianasini topuvchi dastur tuzing (Yaxlitlashda format() dan foydalaning).
Input:
|
Output:
|
b,c ni kiritng: 7,5,9
|
6.38
|
Shar radiusi berilgan. Uning hajmini topuvchi dastur tuzing (Yaxlitlashda format() dan foydalaning).
Input:
|
Output:
|
r ni kiriting: 7.5
|
31.42
|
Uch xonali son berilgan. Berilgan sonning raqamlari yig‘indisini topuvchi dastur tuzing.
#include
// Function prototype
int sumOfDigits(int);
int main() {
int number;
// Get the three-digit number from the user
std::cout << "Enter a three-digit number: ";
std::cin >> number;
// Print the result
std::cout << "The sum of the digits is: " << sumOfDigits(number) << std::endl;
return 0;
}
// Function definition
int sumOfDigits(int n) {
int sum = 0;
// Calculate the sum of the digits
sum += n % 10;
n /= 10;
sum += n % 10;
n /= 10;
sum += n % 10;
return sum;
}
To‘rt xonali son berilgan. Berilgan sonning raqamlari ko‘paytmasini topuvchi dastur tuzing.
#include
// Function prototype
int productOfDigits(int);
int main() {
int number;
// Get the four-digit number from the user
std::cout << "Enter a four-digit number: ";
std::cin >> number;
// Print the result
std::cout << "The product of the digits is: " << productOfDigits(number) << std::endl;
return 0;
}
// Function definition
int productOfDigits(int n) {
int product = 1;
// Calculate the product of the digits
product *= n % 10;
n /= 10;
product *= n % 10;
n /= 10;
product *= n % 10;
n /= 10;
product *= n % 10;
return product; }
To‘g‘ri burchakli uchburchakning katetlari berilgan. Berilgan uchburchakning medianasini topuvchi dastur tuzing (Yaxlitlashda format() dan foydalaning).
#include
#include
// Function prototype
int median(int, int, int);
int main() {
int side1, side2, side3;
// Get the lengths of the triangle's sides from the user
std::cout << "Enter the lengths of the sides of the triangle: ";
std::cin >> side1 >> side2 >> side3;
// Print the result
std::cout << "The median of the triangle is: " << median(side1, side2, side3) << std::endl;
return 0;
}
// Function definition
int median(int a, int b, int c) {
int median;
// Find the median of the triangle
if (a > b && a > c) {
median = round(sqrt(b*b + c*c));
} else if (b > a && b > c) {
median = round(sqrt(a*a + c*c));
} else {
median = round(sqrt(a*a + b*b));
}
return median;
}
Uchburchakning tomonlari berilgan. Uning a tomoniga tushuvchi medianasini topuvchi dastur tuzing (Yaxlitlashda format() dan foydalaning).
#include
#include
#include
using namespace std;
// Function prototype
double find_median_on_side_a(double a, double b, double c);
int main() {
// Test the function
cout << fixed << setprecision(2); // Set output to 2 decimal places
cout << find_median_on_side_a(3, 4, 5) << endl; // Output: "4.47"
cout << find_median_on_side_a(5, 12, 13) << endl; // Output: "9.00"
return 0;
}
// Function definition
double find_median_on_side_a(double a, double b, double c) {
// Find the length of the median that falls on side a
// using the formula:
// median = sqrt((2*b^2 + 2*c^2 - a^2)/4)
double median = sqrt((2*b*b + 2*c*c - a*a)/4);
// Return the length of the median
return median;
}
Shar radiusi berilgan. Uning hajmini topuvchi dastur tuzing (Yaxlitlashda format() dan foydalaning).
#include
#include
#include
using namespace std;
// Function prototype
double find_size_of_sphere(double radius);
int main() {
// Test the function
cout << fixed << setprecision(2); // Set output to 2 decimal places
cout << find_size_of_sphere(2) << endl; // Output: "33.51"
cout << find_size_of_sphere(3) << endl; // Output: "113.10"
return 0;
}
// Function definition
double find_size_of_sphere(double radius) {
// Find the size of the sphere using the formula:
// size = 4/3 * pi * radius^3
const double PI = 3.14159265;
double size = 4.0/3.0 * PI * radius * radius * radius;
// Return the size of the sphere
return size;
}
Do'stlaringiz bilan baham: |