Pages

සීමාවන් නොමැති අදහස් වලට පණ දෙන්න.....

Tuesday, October 6, 2009

a program that would read a number and the base of the number system and print the equivalent decimal value

a program that would read a number and the base of the number system
and print the equivalent decimal value ( for bases less than 10 ) using Math.pow

01# import java.util.Scanner;
02#
03# public class javaprog{
04#
05# public static void main(String[] args){
06#
07# //base of the value which enterd
08# System.out.print("Enter the base of the value which you wish to enter :");
09# Scanner scan = new Scanner(System.in);
10# double base = scan.nextInt();
11#
12# //value to convert
13# System.out.println("\n You need to add zero '0' in front of the value, if itdoesn't have 8 digits \n");
14# System.out.print("Enter the value which you want to convert in to decimal:");
15# Scanner value = new Scanner(System.in).useDelimiter("");
16#
17# //scans the value by nextInt(); and saves them as int
18# int d0=value.nextInt();
19# int d1=value.nextInt();
20# int d2=value.nextInt();
21# int d3=value.nextInt();
22# int d4=value.nextInt();
23# int d5=value.nextInt();
24# int d6=value.nextInt();
25# int d7=value.nextInt();
26#
27# //calculates the powers using Math.pow(base,exp);
28# int v7=(int)Math.pow(base,7)*d0;
29# int v6=(int)Math.pow(base,6)*d1;
30# int v5=(int)Math.pow(base,5)*d2;
31# int v4=(int)Math.pow(base,4)*d3;
32# int v3=(int)Math.pow(base,3)*d4;
33# int v2=(int)Math.pow(base,2)*d5;
34# int v1=(int)Math.pow(base,1)*d6;
35# int v0=(int)Math.pow(base,0)*d7;
36#
37# //calculates the sum of v0 to v7
38# int decimalValue=v0+v1+v2+v3+v4+v5+v6+v7;
39#
40# //casting base to int
41# int base1=(int)base;
42#
43# //prints the result
44# System.out.println("\n\tYou entered : " +d0+d1+d2+d3+d4+d5+d6+d7 + " with base : " +base1 );
45# System.out.println("\n\tdeciaml value for : " +d0+d1+d2+d3+d4+d5+d6+d7 +" using Math.pow is :"+ decimalValue);
46#
47# }
48# }




output:

Enter the base of the value which you wish to enter :8
You need to add zero '0' in front of the value, if it doesn't have 8 digits
Enter the value which you want to convert in to decimal:45454545
You entered : 45454545 with base : 8
deciaml value for : 45454545 using Math.pow is :9853285

No comments:

Post a Comment

හිත කියන්නෙ පුදුම දෙයක්. ඒක තියෙන්නෙ කොතැනද කියලා කවුද දන්නෙ?
හිත නිතරම වෙනස් වෙනවා. හිතට නිතරම සිතුවිලි එනවා.
හිතට වරක් ආපු සිතුවිල්ලක් නැවත ඇති වෙන්නෙ කලාතුරකින්.

ඔබ කැමති නම් හිතට එන සිතුවිලි/අදහස් අකුරු කරන්න.


ඇනෝලා සමා වෙත්වා. මගේ බ්ලොග් එකට ස්පෑම්බොට් එකකින් ස්පෑම් කමෙන්ට් එනනිසා ඇනෝලාගේ කමෙන්ට් කිරීමේ අයිතිය නැති කරන්න සිදු වුනා.

Related Posts with Thumbnails