Alexa Algorithm
Alexa Algorithm
Sign up
You said:
read about the problems in detail,and write c++programs for the following questions:1,tempretur
conversion from f to c and c to f.2,generating pascal triangl.3,decimal to binary
conversion.4,guessing game.5,tower of hanoi generate c++ progames in algorithm in short and
clearly with out cin
ChatGPT said:
Here are C++ programs for each of the tasks you mentioned. I've kept the code short and clear,
following the respective algorithms for each problem:
int main() {
fahrenheitToCelsius(100); // Example conversion
celsiusToFahrenheit(37.5); // Example conversion
return 0;
}
void printPascal(int n) {
for (int line = 0; line < n; line++) {
int number = 1;
for (int i = 0; i <= line; i++) {
cout << number << " ";
number = number * (line - i) / (i + 1);
}
cout << endl;
}
}
int main() {
printPascal(5); // Example: 5 rows of Pascal's Triangle
return 0;
}
void decimalToBinary(int n) {
stack<int> binary;
while (n > 0) {
binary.push(n % 2);
n = n / 2;
}
while (!binary.empty()) {
cout << binary.top();
binary.pop();
}
cout << endl;
}
int main() {
decimalToBinary(10); // Example: Convert decimal 10 to binary
return 0;
}
void guessingGame() {
srand(time(0)); // Seed the random number generator
int number = rand() % 100 + 1; // Random number between 1 and 100
int guess;
do {
guess = 50; // Guess hardcoded to 50 (you can enhance it to take
user input)
if (guess < number) {
cout << "Guess " << guess << " is too low!" << endl;
} else if (guess > number) {
cout << "Guess " << guess << " is too high!" << endl;
} else {
cout << "Correct guess!" << endl;
}
} while (guess != number);
}
int main() {
guessingGame(); // Example guessing game
return 0;
}
5. Tower of Hanoi
cpp
Copy
#include <iostream>
using namespace std;
int main() {
towerOfHanoi(3, 'A', 'C', 'B'); // Example: 3 disks
return 0;
}
1. Temperature Conversion:
o Fahrenheit to Celsius: Formula is (Fahrenheit - 32) * 5 / 9.
o Celsius to Fahrenheit: Formula is (Celsius * 9 / 5) + 32.
2. Pascal’s Triangle:
o Each number in a row is calculated using the binomial coefficient, C(n, r) = n!
/ (r! * (n-r)!). For efficiency, use the previous value to calculate the next in
the same row.
3. Decimal to Binary Conversion:
o Use division by 2 and store the remainders (either 0 or 1) in a stack. After
converting, pop the stack to get the binary representation.
4. Guessing Game:
o Generate a random number between 1 and 100, and compare guesses until the
correct number is guessed.
5. Tower of Hanoi:
o Move disks recursively from the source pole to the destination using the auxiliary
pole. The base case is when there’s only one disk to move.
Each program addresses the problem with minimal code, clear logic, and avoids the use of cin
for input in line with your instructions