Tospshiriqlarni bajarishda funksiyadan foydalanish zarur
Download 19.32 Kb.
|
1-topshiriq-amaliy
- Bu sahifa navigatsiya:
- Input: Output
TOSPSHIRIQLARNI BAJARISHDA FUNKSIYADAN FOYDALANISH ZARUR. Topshiriqlar Uch xonali son berilgan. Berilgan sonning raqamlari yig‘indisini topuvchi dastur tuzing.
To‘rt xonali son berilgan. Berilgan sonning raqamlari ko‘paytmasini topuvchi dastur tuzing.
To‘g‘ri burchakli uchburchakning katetlari berilgan. Berilgan uchburchakning medianasini topuvchi dastur tuzing (Yaxlitlashda format() dan foydalaning).
Uchburchakning tomonlari berilgan. Uning a tomoniga tushuvchi medianasini topuvchi dastur tuzing (Yaxlitlashda format() dan foydalaning).
Shar radiusi berilgan. Uning hajmini topuvchi dastur tuzing (Yaxlitlashda format() dan foydalaning).
Uch xonali son berilgan. Berilgan sonning raqamlari yig‘indisini topuvchi dastur tuzing. #include // Function prototype
int main() { int number; // Get the three-digit number from the user std::cout << "Enter a three-digit number: "; std::cin >> number; std::cout << "The sum of the digits is: " << sumOfDigits(number) << std::endl; return 0; } 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 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
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; } Download 19.32 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling