DECIMAL TO OCTAL CONVERSION - CPPIOA003

Description:

Can you help your digital design faculty in correcting the assignment on number conversions decimal number to its equivalent octal number. by writing a program to convert a decimal number to its equivalent octal number. Input and Output Format : Input consists of a single integer. Output is the octal equivalent of the decimal input. [Refer sample input and output for formatting specifications. Test Case 1 Input (stdin) 9 Expected Output 11 Test Case 2 Input (stdin) 123 Expected Output 173

Program :

#include <iostream> 
using namespace std; 

// function to convert decimal to octal 
void decToOctal(int n) 
{ 
 
 // array to store octal number 
 int octalNum[100]; 

 // counter for octal number array 
 int i = 0; 
 while (n != 0) { 

  // storing remainder in octal array 
  octalNum[i] = n % 8; 
  n = n / 8; 
  i++; 
 } 

 // printing octal number array in reverse order 
 for (int j = i - 1; j >= 0; j--) 
  cout << octalNum[j]; 
} 

// Driver program to test above function 
int main() 
{ 
 int n ;
  cin>>n;

 decToOctal(n); 

 return 0; 
}