Advertisement
mdporan

SumLimit.java

Jan 25th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1.  
  2.  /* Given 2 non-negative ints, a and b, return their sum, so long as the sum
  3.  *  has the same number of digits as a. If the sum has more digits than a,
  4.  *  just return a without b.
  5.  */
  6. package TypesOfSum;
  7. //-------import scanner class-----
  8. import java.util.Scanner;
  9.  
  10. //--------created SumLimit class ----
  11. public class SumLimit {
  12.     //--------created sumLimit method--------
  13.     static public int sumLimit(int a, int b) {
  14.     int sum = a + b;
  15.     //--------------converted a int  to String and calculate length of a string------
  16.     int aLength = String.valueOf(a).length();
  17.     //--------------converted sum int  to String and calculate length of sum string------
  18.     int sumLength = String.valueOf(sum).length();
  19.        
  20.     if(sumLength == aLength)
  21.         return sum;
  22.                  
  23.     return a;
  24. }
  25.    
  26.    
  27.     public static void main(String[] args) {
  28.     //--------calling input objects---------  
  29.     Scanner s=new Scanner(System.in);
  30.     System.out.println("Please,Enter Two number:");
  31.     int n=s.nextInt();
  32.     int m=s.nextInt();
  33.     //--------calling sumLimit method------
  34.     int sum=sumLimit(n,m);
  35.     System.out.println(sum);
  36.     }
  37.    
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement