Wednesday, April 16, 2014

Octal Convert – C Program

Problem Question


Write a program to find the octal equivalent of the entered number.

Explanation of Problem


It is a simple program that accepts user’s input (integer) and calculate its octal equivalent and displays the result.

Code


#include <stdio.h>
/*Octal Calculator.c*
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: Code::Blocks 12.11*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 16-04-2014**/
int main()
{
  int number;
  printf("\n\nEnter a 5-digit number:\t");
  scanf("%d", &number);
  printf("The octal equivalent is: 0000");
  while (number > 0)
  {
    printf("%d\b\b", number % 8);
    number /= 8;
  }
  printf("\n\n");
  system("pause");
  return 0;
}

Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This function has to be named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

while (number > 0)
{
printf("%d\b\b", number % 8);
number /= 8;
}
This is the main part of the program where the calculation takes place. I am not storing the complete octal equivalent as such, rather displaying each digit on the screen as it is calculated. I used the line printf("The octal equivalent is: 0000"); for the output prompt. I have used four zeroes, so that I can display the output in the correct manner. Since I am not storing the digits of the octal output, and my logic calculates the octal equivalent backwards, if I display the digits as it is the result would be reverse of the expected output! So I used four zeroes in the output prompt, and ‘\b’ when I wanted to print the digit. What happens is that, once the digit is printed, the cursor shifts one character ahead. Then upon seeing the ‘\b’ the printf() function forces the cursor to move one space back. I have used two ‘\b’ so that my already printed digit doesn’t get erased by the next digit being printed. Instead of using 4 zeroes, I could have used something else too, but if the user enters a number which has less than 5 digits, the result would contain extra characters that are not required, and zero that I have used is not a problem since a zero to the left of a number doesn’t make a difference. If I used spaces instead, the formatting of the result would be absurd. Like if the user enters a single digit number, the output would be quite far from the output prompt. I have asked the user to enter a 5-digit number because I have formatted the output that way. Else, if you observe, within the limits of ‘int’ any number is acceptable and a CORRECT result will be produced.

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)



No comments:

Post a Comment

Need help?