Untitled

ICS2601 secret lang natin toh conversion
 avatar
unknown
java
2 years ago
4.9 kB
4
Indexable
import java.util.*;


public class ics2601secret{

    static Scanner A = new Scanner(System.in);
    public static void main(String[]args)
    {
        String x;
        
        
        do
        {System.out.println("Please enter the base of the number:");
        int base=A.nextInt(),number;

        //else if statments used since there is more than one condition and swtich can't be used since there is more than 11 different cases
        if (base<10&&base>1)
        {
            System.out.println("Please enter the number:");
            number=A.nextInt(); //used to collect the number to be converted to base 10
            switch(base) //basic switch statement just changed the method that will be used to print everything
            {
                case 2: 
                System.out.println("The number "+number+" in the base "+base+" is "+base2(number)+" in the base 10");
                break;

                case 3:
                System.out.println("The number "+number+" in the base "+base+" is "+base3(number)+" in the base 10");
                break;

                case 4:
                System.out.println("The number "+number+" in the base "+base+" is "+base4(number)+" in the base 10");
                
                break;

                case 5:
                System.out.println("The number "+number+" in the base "+base+" is "+base5(number)+" in the base 10");
                break;

                case 6:
                System.out.println("The number "+number+" in the base "+base+" is "+base6(number)+" in the base 10");
                break;

                case 7:
                System.out.println("The number "+number+" in the base "+base+" is "+base7(number)+" in the base 10");
                break;

                case 8:
                System.out.println("The number "+number+" in the base "+base+" is "+base8(number)+" in the base 10");
                break;

                case 9:
                System.out.println("The number "+number+" in the base "+base+" is "+base9(number)+" in the base 10");
                break;
            }
        }
        else if (base>10)
        {
            System.out.println("Base cannot be greater than 10!");    
        }
        else if (base==1)
        {
            System.out.println("Base cannot be equal to 1!");        
        }
        System.out.println("Do you wish to continue? (y/n)");
        x = A.next().toUpperCase();
        
        }
        while(x.equals("Y"));
        
    }
    
    //formula is all the same just change the number multiplied to 1 by the base
    static int base2(int a)
    {
        int n = a,decimal=0,x,base=1,finall;
        for(x=n;x>0;)
        {
            finall = x%10;
            x = x/10;
            decimal += finall*base;
            base = base*2;
        }
        return decimal; //returns number converted from base n to base 10
    }

    static int base3(int a)
    {
        int n = a,decimal=0,x,base=1,finall;
        for(x=n;x>0;)
        {
            finall = x%10;
            x = x/10;
            decimal += finall+base;
            base = base*3;
        }
        return decimal;
    }

    static int base4(int a)
    {
        int n = a,decimal=0,x,base=1,finall;
        for(x=n;x>0;)
        {
            finall = x%10;
            x = x/10;
            decimal += finall*base;
            base = base*4;
        }
        return decimal;
    }

    static int base5(int a)
    {
        int n = a,decimal=0,x,base=1,finall;
        for(x=n;x>0;)
        {
            finall = x%10;
            x = x/10;
            decimal += finall*base;
            base = base*5;
        }
        return decimal;
    }

    static int base6(int a)
    {
        int n = a,decimal=0,x,base=1,finall;
        for(x=n;x>0;)
        {
            finall = x%10;
            x = x/10;
            decimal += finall*base;
            base = base*6;
        }
        return decimal;
    }

    static int base7(int a)
    {
        int n = a,decimal=0,x,base=1,finall;
        for(x=n;x>0;)
        {
            finall = x%10;
            x = x/10;
            decimal += finall*base;
            base = base*7;
        }
        return decimal;
    }

    static int base8(int a)
    {
        int n = a,decimal=0,x,base=1,finall;
        for(x=n;x>0;)
        {
            finall = x%10;
            x = x/10;
            decimal += finall*base;
            base = base*8;
        }
        return decimal;
    }

    static int base9(int a)
    {
        int n = a,decimal=0,x,base=1,finall;
        for(x=n;x>0;)
        {
            finall = x%10;
            x = x/10;
            decimal += finall*base;
            base = base*9;
        }
        return decimal;
    }
}