0% found this document useful (0 votes)
5 views

Alexa Algorithm

The document provides C++ programs for five tasks: temperature conversion, generating Pascal's Triangle, decimal to binary conversion, a guessing game, and the Tower of Hanoi. Each program is designed with clear algorithms and minimal code, avoiding user input through cin. The explanations for the algorithms are included to clarify the logic behind each program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Alexa Algorithm

The document provides C++ programs for five tasks: temperature conversion, generating Pascal's Triangle, decimal to binary conversion, a guessing game, and the Tower of Hanoi. Each program is designed with clear algorithms and minimal code, avoiding user input through cin. The explanations for the algorithms are included to clarify the logic behind each program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Log in

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:

1. Temperature Conversion (Fahrenheit to Celsius and Celsius to Fahrenheit)


cpp
Copy
#include <iostream>
using namespace std;

void fahrenheitToCelsius(double fahrenheit) {


double celsius = (fahrenheit - 32) * 5.0 / 9.0;
cout << fahrenheit << " F = " << celsius << " C" << endl;
}

void celsiusToFahrenheit(double celsius) {


double fahrenheit = (celsius * 9.0 / 5.0) + 32;
cout << celsius << " C = " << fahrenheit << " F" << endl;
}

int main() {
fahrenheitToCelsius(100); // Example conversion
celsiusToFahrenheit(37.5); // Example conversion
return 0;
}

2. Generating Pascal's Triangle


cpp
Copy
#include <iostream>
using namespace std;

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;
}

3. Decimal to Binary Conversion


cpp
Copy
#include <iostream>
#include <stack>
using namespace std;

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;
}

4. Guessing Game (Basic Version)


cpp
Copy
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

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;

void towerOfHanoi(int n, char source, char destination, char auxiliary) {


if (n == 1) {
cout << "Move disk 1 from " << source << " to " << destination <<
endl;
return;
}
towerOfHanoi(n - 1, source, auxiliary, destination);
cout << "Move disk " << n << " from " << source << " to " << destination
<< endl;
towerOfHanoi(n - 1, auxiliary, destination, source);
}

int main() {
towerOfHanoi(3, 'A', 'C', 'B'); // Example: 3 disks
return 0;
}

Explanation of Algorithm for Each Problem:

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

You might also like