2 answers
I'd be happy to help you with practical sessions on binary operations, ratios and reciprocals, and circle area and circumference. Let's go through each of these topics step by step.
1. Binary Operations: Binary operations involve performing mathematical operations on two operands. Common binary operations include addition, subtraction, multiplication, and division.
#include <iostream>
int main() {
// Binary addition
int binary1, binary2;
std::cout << "Enter first binary number: ";
std::cin >> binary1;
std::cout << "Enter second binary number: ";
std::cin >> binary2;int result_add = binary1 + binary2;
std::cout << "Binary Addition: " << result_add << std::endl;// Binary subtraction
int binary3, binary4;
std::cout << "Enter first binary number: ";
std::cin >> binary3;
std::cout << "Enter second binary number: ";
std::cin >> binary4;int result_sub = binary3 - binary4;
std::cout << "Binary Subtraction: " << result_sub << std::endl;// Binary multiplication
int binary5, binary6;
std::cout << "Enter first binary number: ";
std::cin >> binary5;
std::cout << "Enter second binary number: ";
std::cin >> binary6;int result_mul = binary5 * binary6;
std::cout << "Binary Multiplication: " << result_mul << std::endl;// Binary division
int binary7, binary8;
std::cout << "Enter dividend (binary): ";
std::cin >> binary7;
std::cout << "Enter divisor (binary): ";
std::cin >> binary8;int result_div = binary7 / binary8;
std::cout << "Binary Division: " << result_div << std::endl;return 0;
}
2. Ratios and Reciprocals: Ratios express the relationship between two quantities. Reciprocals are the multiplicative inverses of numbers.
#include <iostream>
int main() {
// Printing rate calculation
int pages, time_minutes;
std::cout << "Enter number of pages: ";
std::cin >> pages;
std::cout << "Enter time in minutes: ";
std::cin >> time_minutes;double printing_rate = static_cast<double>(pages) / time_minutes;
double reciprocal_rate = 1.0 / printing_rate;
std::cout << "Printing Rate: " << printing_rate << " pages per minute" << std::endl;
std::cout << "Reciprocal Rate: " << reciprocal_rate << " minutes per page" << std::endl;// Ratio and reciprocal
int boys, girls;
std::cout << "Enter number of boys: ";
std::cin >> boys;
std::cout << "Enter number of girls: ";
std::cin >> girls;double ratio = static_cast<double>(boys) / girls;
double reciprocal_ratio = 1.0 / ratio;
std::cout << "Ratio of Boys to Girls: " << ratio << std::endl;
std::cout << "Reciprocal Ratio: " << reciprocal_ratio << std::endl;return 0;
}
3. Circle Area and Circumference: Circle area and circumference are fundamental concepts in geometry. The area of a circle is given by πr^2, and the circumference is given by 2πr, where r is the radius.
#include <iostream>
#include <cmath>int main() {
// Circle calculations
double radius1;
std::cout << "Enter radius of Circle 1: ";
std::cin >> radius1;double area1 = M_PI * radius1 * radius1;
double circumference1 = 2 * M_PI * radius1;
std::cout << "Circle 1 - Area: " << area1 << ", Circumference: " << circumference1 << std::endl;double diameter;
std::cout << "Enter diameter of Circle 2: ";
std::cin >> diameter;double radius2 = diameter / 2.0;
double area2 = M_PI * radius2 * radius2;
double circumference2 = 2 * M_PI * radius2;
std::cout << "Circle 2 - Area: " << area2 << ", Circumference: " << circumference2 << std::endl;return 0;
}
Answer: One C++ program that (1) converts a decimal number to binary, (2) finds the ratio of two numbers and the reciprocal of a number, and (3) finds the area and circumference of a circle.
#include <iostream>
using namespace std;
int main() {
// 1. Decimal to binary
int num;
cout << "Enter a positive integer: ";
cin >> num;
string bin = "";
int t = num;
if (t == 0) bin = "0";
while (t > 0) {
bin = char('0' + t % 2) + bin; // remainder gives next bit
t = t / 2;
}
cout << "Binary of " << num << " = " << bin << endl;
// 2. Ratio and reciprocal
double a, b;
cout << "Enter two numbers a and b: ";
cin >> a >> b;
if (b != 0)
cout << "Ratio a/b = " << a / b << endl;
else
cout << "Ratio not defined (b = 0)" << endl;
if (a != 0)
cout << "Reciprocal of a = " << 1 / a << endl;
else
cout << "Reciprocal of 0 is not defined" << endl;
// 3. Area and circumference of a circle
const double PI = 3.14159;
double r;
cout << "Enter radius of circle: ";
cin >> r;
cout << "Area = " << PI * r * r << endl;
cout << "Circumference = " << 2 * PI * r << endl;
return 0;
}Logic
- Binary: divide the number by 2 repeatedly. Each remainder (0 or 1) is the next bit, and it is added at the front of the string. For example, 13 → 1101.
- Ratio and reciprocal: ratio = a / b and reciprocal = 1 / a. Use
doubleso the answer is not cut off, and check for division by zero. - Circle: area \(= \pi r^{2}\) and circumference \(= 2\pi r\).
Sample output (inputs 13; 6 4; 7):
Binary of 13 = 1101
Ratio a/b = 1.5
Reciprocal of a = 0.166667
Area = 153.938
Circumference = 43.9823The earlier answer on this page reads the "binary" numbers into ordinary int variables and adds them in decimal (for example, 101 + 1 gives 102, not 110), so its binary part does not work in binary at all.
Your answer
Log in or create a free account to answer this question.