Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Lesson 1011 Coding Activity
- *
- * For this program you will input an int to represent the octal number
- * and translate to the base ten number. The octal number must be 8 digits or less.
- *
- * Your program should also check that all the digits are 0 - 7, then translate the
- * number to base ten.
- *
- * Sample Run 1:
- * Enter a number in base 8:
- * 1287
- * ERROR: Incorrect Octal Format
- *
- * Sample Run 2:
- * Enter a number in base 8:
- * 123
- * 83
- *
- * Sample Run 3:
- * Enter a number in base 8:
- * 1111111111
- * ERROR: Incorrect Octal Format
- *
- */
- import java.util.Scanner;
- import java.lang.Math;
- class Lesson_1011_Activity{
- public static void main(String[] args)
- {
- Scanner scan = new Scanner (System.in);
- System.out.println("Please enter a number in base 8:");
- String x = scan.nextLine();
- int sum = 0;
- int flag = 0;
- if (x.length() > 8)
- {
- System.out.println("ERROR: Incorrect Octal Format");
- }
- else
- {
- for (int i = 0; i < x.length(); i++)
- {
- if (x.charAt(i) > 55)
- {
- System.out.println("ERROR: Incorrect Octal Format");
- flag++;
- }
- }
- if (flag == 0)
- {
- int a = x.length()-1;
- for(int i = 0; i < x.length(); i++)
- {
- System.out.println((x.charAt(i)-48)*(Math.pow(8, a)));
- sum += (x.charAt(i)-48)*(Math.pow(8, a));
- a--;
- }
- System.out.println(x.charAt(0)-48);
- System.out.println(sum);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement