C# Sharp Exercises: Convert Octal number to Decimal number
C# Sharp For Loop: Exercise-51 with Solution
Write a program in C# Sharp to convert a Octal number to Decimal number without using an array, function and while loop.
Pictorial Presentation:
Sample Solution:
C# Sharp Code:
using System;
public class Exercise51
{
public static void Main()
{ int n1, n5,p=1,k,ch=1;
int dec=0,i=1,j,d;
Console.Write("\n\n");
Console.Write("Convert Octal number to Decimal number:\n");
Console.Write("------------------------------------------");
Console.Write("\n\n");
Console.Write("Input an octal number (using digit 0 - 7) :");
n1 = Convert.ToInt32(Console.ReadLine());
n5=n1;
for(;n1>0;n1=n1/10)
{
k=n1 % 10;
if(k>=8)
{
ch=0;
}
}
switch(ch)
{
case 0 :
Console.Write("\nThe number is not an octal number. \n\n");
break;
case 1:
n1=n5;
for (j=n1;j>0;j=j/10)
{
d = j % 10;
if(i==1)
p=p*1;
else
p=p*8;
dec=dec+(d*p);
i++;
}
Console.Write("\nThe Octal Number : {0}\nThe equivalent Decimal Number : {1} \n\n",n5,dec);
break;
}
}
}
Sample Output:
Convert Octal number to Decimal number: ------------------------------------------ Input an octal number (using digit 0 - 7) :5 The Octal Number : 5 The equivalent Decimal Number : 5
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to convert a decimal number into octal without an using an array.
Next: Write a program in C# Sharp to find the Sum of GP series.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework