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++ program decimal to octal
3 answers
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.
```
#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.