Results 1 to 4 of 4
- 04-18-2012, 02:49 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Java program to calculate numbers longer than long datatype can hold.
hello, I am trying to write a java program that will add and multiply numbers that can have up to 100 digits. here is what i have so far.
Java Code:import java.util.*; public class Mult { private static Scanner in = new Scanner (System.in); public static void main (String[] args) { int test = 0; do{ System.out.print ("Enter the first number: "); String a = in.nextLine(); String y = a; boolean check = isValid (y); if(check == true){ test = 2; } }while(test < 1); System.out.println(a); int test2 = 0; do{ System.out.print("Enter the second number: "); String b = in.nextLine(); String x = b; boolean check2 = isValid(x); if(check2 == true){ test2 = 2; } }while(test2 < 1); System.out.println(b); String T = add(a, b); System.out.println(T); } public static boolean isValid (String num) { for (int i = 0; i < num.length(); i++) { char c = num.charAt(i); if ((c >= '0') && (c <= '9')){ return true; } } return false; } public static String noLeadingZeroes (String num){ int count = 0; for (int i = 0; i < num.length(); i++) { char c = num.charAt(i); if(c == 0){ count = count + 1; } else{ i = num.length(); } } return num; } public static String add (String a, String b){ while (a.length() < b.length()) a = "0"+a; while (b.length() < a.length()) b = "0"+b; int c = 0; String total = ""; for (int i=a.length()-1; i>=0; i--) { int adig = a.charAt(i)-'0'; int bdig = b.charAt(i)-'0'; int ddig = 0; if (adig+c+bdig > 9) { ddig = adig+c+bdig-10; c = 1; } else { ddig = adig+c+bdig; c = 0; } total = ((char)(ddig + '0'))+total; } return total; } }
thanks for any help...
- 04-18-2012, 03:03 AM #2
Re: Java program to calculate numbers longer than long datatype can hold.
Try the BigInteger class.
If you don't understand my response, don't ignore it, ask a question.
- 04-18-2012, 03:05 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
- 04-18-2012, 03:10 AM #4
Re: Java program to calculate numbers longer than long datatype can hold.
where in my code my a and b variables are changing
Be sure to add an ID String with the println("var=" + var) where var is the name of the variable you are printing so you know what the variable is that is printed and where it was printed.
Your code does not compile without errors. You need to fix the errors in the code BEFORE you try to execute it.Last edited by Norm; 04-18-2012 at 03:13 AM.
If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
Without Using Java.lang.Math Square Root.. How to calculate of 2 given Numbers.
By javasri in forum Forum LobbyReplies: 3Last Post: 06-30-2011, 10:01 PM -
Extracting individual numbers from a long integer
By CurbYourEnthusiasm in forum New To JavaReplies: 6Last Post: 02-18-2011, 04:53 PM -
Program in Java To calculate GCD of n numbers.?
By ankitsinghal_89 in forum New To JavaReplies: 4Last Post: 02-15-2011, 10:23 AM -
DnD within applet no longer working on Java 6 update 20
By drez in forum Java AppletsReplies: 9Last Post: 05-21-2010, 11:46 AM -
Calculate sum of long integer!
By Julingo in forum New To JavaReplies: 2Last Post: 09-10-2008, 12:50 AM
Bookmarks