C++ program decimal to octal
Here is a C++ program to convert decimal to octal:
```
#include <iostream>
using namespace std;
int main() {
int decimal, octal = 0, i = 1;
cout << "Enter a decimal number: ";
cin >> decimal;
while (decimal != 0) {
octal += (decimal % 8) * i;
decimal /= 8;
i *= 10;
}
cout << "Octal equivalent: " << octal;
return 0;
}
```
Explanation:
1. Declare variables decimal, octal, and i. Decimal is the input decimal number, octal is the output octal number, and i is used as a multiplier to place each digit in the correct position.
2. Ask the user to enter a decimal number and store it in the variable decimal.
3. Use a while loop to repeatedly divide the decimal number by 8 and store the remainder (in octal form) in the variable octal. Each time we do this, we multiply the remainder by the current value of i and add it to octal. Then we divide decimal by 8 and update i by multiplying it by 10. This way, the remainder is placed in the correct position in the final octal number.
4. Display the final octal number to the user.
C++: Binary to Decimal
Here is a C++ program to convert binary to decimal:
```
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int binary, decimal = 0, i = 0, remainder;
cout << "Enter a binary number: ";
cin >> binary;
while (binary != 0) {
remainder = binary % 10;
decimal += remainder * pow(2, i);
++i;
binary /= 10;
}
cout << "Decimal equivalent: " << decimal;
return 0;
}
```
Explanation:
1. Declare variables binary, decimal, i, and remainder. Binary is the input binary number, decimal is the output decimal number, i is used as a power of 2 to place each digit in the correct position, and remainder stores the remainder when we divide by 10.
2. Ask the user to enter a binary number and store it in the variable binary.
3. Use a while loop to repeatedly divide binary by 10 and store the remainder in the variable remainder. Each time we do this, we multiply the remainder by the current value of 2^i and add it to decimal. Then, we increment i and divide binary by 10 to move on to the next digit.
4. Display the final decimal number to the user.
To write a C++ program that converts a decimal number to its octal equivalent, you can follow these steps:
1. Include the necessary header file for input/output operations:
```cpp
#include <iostream>
```
2. Use the `using` directive for the standard library namespace, which will allow you to omit the `std::` prefix for certain operations:
```cpp
using namespace std;
```
3. Define the main function, which is the entry point for your program:
```cpp
int main() {
// your code here
return 0;
}
```
4. Prompt the user to enter a decimal number:
```cpp
int decimal;
cout << "Enter a decimal number: ";
cin >> decimal;
```
5. Create a variable to hold the octal equivalent and initialize it to 0:
```cpp
int octal = 0;
```
6. Use a while loop to perform the conversion:
```cpp
int place = 1; // represents the place value of the digit in the octal number
while (decimal > 0) {
int remainder = decimal % 8; // calculate the remainder when dividing by 8
octal += remainder * place; // add the digit to the octal number
decimal /= 8; // update the decimal number by dividing by 8
place *= 10; // increment the place value by multiplying by 10
}
```
7. Print the octal equivalent:
```cpp
cout << "Octal equivalent: " << octal << endl;
```
The complete program will look like this:
```cpp
#include <iostream>
using namespace std;
int main() {
int decimal;
cout << "Enter a decimal number: ";
cin >> decimal;
int octal = 0;
int place = 1;
while (decimal > 0) {
int remainder = decimal % 8;
octal += remainder * place;
decimal /= 8;
place *= 10;
}
cout << "Octal equivalent: " << octal << endl;
return 0;
}
```
You can compile and run this program to convert a decimal number to its octal equivalent.