שאלה 2
Back Up Next

 

/* 
  Module: tr4_q2.cpp
  Author: Yariv Amar
  What? This program converts any octal number (base 8) to decimal base.
  Input : Octal  number.
  Output: Decimal number.
  Assumptions on input: Valid octal number only the 0,1,2,3,4,5,6,7 
  digits are allowed!
  How?
  On each iteration we multiple the previous result in 8 
  and add the next number. 
  In order to overcome the first iteration we adjust 
  the Dec_num to 0 at the begging.  
*/
#include<iostream.h>
void main()
{
  int Dec_num=0;//the decimal number
  char ch;//input of octalic digits
  
  cout<<"\nPlease enter the octalic number and press enter :"; 
  
  while((ch=cin.get())!='\n')//start loop till enter is presed
      Dec_num=Dec_num*8+(ch-'0');//see how
  cout<<"The number in decimal is: "<<Dec_num<<"\n";//print the result
   
}

פלטים מיצגים


Please enter the octalic number and press enter :375
The number in decimal is: 253


Please enter the octalic number and press enter :671
The number in decimal is: 441

 

© Copyright YAProductions 2000