The Program in C# Program to Convert Octal to Decimal Number without Array is given below:
using System;
namespace OctalToDecimalConversion
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family,enter an octal number to convert into decimal number below: ");
Console.WriteLine("Enter an octal number: ");
string octalNumber = Console.ReadLine();
int decimalNumber = 0;
int power = 1;
for (int i = octalNumber.Length - 1; i >= 0; i--)
{
int digit = int.Parse(octalNumber[i].ToString());
decimalNumber += digit * power;
power *= 8;
}
Console.WriteLine("Okay,the Decimal representation: " + decimalNumber);
}
}
}
Output:
Hello Codeauri Family,enter an octal number to convert into decimal number below:
Enter an octal number:
8
Okay,the Decimal representation: 8
Pro-Tips💡
Here are the step by step execution of above program:
The program starts by using the “System” namespace.
The program defines a namespace called “OctalToDecimalConversion”.
The program defines a class called “Program”.
The program defines the Main method, which is the entry point of the program.
The program writes a message to the console asking the user to enter an octal number.
The program reads the octal number entered by the user and stores it in a string variable called “octalNumber”.
The program initializes two variables: “decimalNumber” and “power”. “decimalNumber” is used to store the converted decimal number, and “power” is used to store the current power of 8 being used in the conversion.
The program uses a for loop to iterate through the octal number from right to left.
The program converts each character of the octal number to an integer, and then multiplies the integer by the current power of 8.
The program adds the result of this multiplication to “decimalNumber”.
The program then multiplies “power” by 8 for the next iteration of the loop.
The program repeats steps 9 to 11 for each character of the octal number.
After the loop finishes, the program writes the converted decimal number to the console.
The program ends by returning 0 from the Main method
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.