Python/C/C++/JAVA

Moderate Practice Programs with Code and Concept

By D.S

Convert Octal number to Decimal

If you want to write a program that displays the Fibonacci sequence, it's actually not too complicated. First off, you'll need to understand what the sequence is - essentially, it's a series of numbers where each number after the first two is the sum of the two preceding ones (e.g. 1, 1, 2, 3, 5, 8...). Once you've got that down pat, you can start coding. Typically, this involves using loops and variables to generate each successive value in the sequence until your program reaches whatever endpoint you've specified (either a certain number of iterations or a maximum value for the generated numbers). Whether you're working in a language like Python or C++, there are plenty of resources out there with sample code and tutorials to help guide you through the process. All in all, writing a Fibonacci program can be a fun challenge that pays off with some nifty math-based visualizations!

(a.) C Code

#include <stdio.h>

int main() {
    char octal[100];
        int decimal = 0, i = 0, j;
        
        printf("Enter an octal number: ");
        scanf("%s", octal);
    
        // Find the length of the octal number
        while (octal[i] != '') {
            i++;
        }
        
        // Convert octal to decimal
        for (j = 0; j < i; j++) {
            decimal = decimal * 8 + (octal[j] - '0');
        }
    
        printf("Decimal equivalent: %d
", decimal);
        return 0;
}
Output:-
Decimal equivalent: 62

(b.) C++ Code

#include <iostream>
using namespace std;

int main() {
  string octal;
  int decimal = 0;

  cout << "Enter an octal number: ";
  cin >> octal;

  // Convert octal to decimal
  for (int i = 0; i < octal.length(); ++i) {
      decimal = decimal * 8 + (octal[i] - '0');
  }

  cout << "Decimal equivalent: " << decimal << endl;
    return 0;
}
Output:-
Decimal equivalent: 62

(c.) Python Code

def main():
      octal = input("Enter an octal number: ")
      decimal = int(octal, 8)
      print("Decimal equivalent:", decimal)

if __name__ == "__main__":
    main()
Output:-
Decimal equivalent: 62

(d.) Java Code

public class Main {
    public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      System.out.print("Enter an octal number: ");
      String octalString = scanner.next();
      int decimal = Integer.parseInt(octalString, 8);
      System.out.println("Decimal equivalent: " + decimal);
    }
}
Output:-
Decimal equivalent: 62