w3resource
C# Sharp Programming Exercises

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:

C# Sharp Exercises:  Convert Octal number to Decimal number

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:

Flowchart : Convert decimal number to octal without using array

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?



New Content: Composer: Dependency manager for PHP, R Programming