Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Given 2 non-negative ints, a and b, return their sum, so long as the sum
- * has the same number of digits as a. If the sum has more digits than a,
- * just return a without b.
- */
- package TypesOfSum;
- //-------import scanner class-----
- import java.util.Scanner;
- //--------created SumLimit class ----
- public class SumLimit {
- //--------created sumLimit method--------
- static public int sumLimit(int a, int b) {
- int sum = a + b;
- //--------------converted a int to String and calculate length of a string------
- int aLength = String.valueOf(a).length();
- //--------------converted sum int to String and calculate length of sum string------
- int sumLength = String.valueOf(sum).length();
- if(sumLength == aLength)
- return sum;
- return a;
- }
- public static void main(String[] args) {
- //--------calling input objects---------
- Scanner s=new Scanner(System.in);
- System.out.println("Please,Enter Two number:");
- int n=s.nextInt();
- int m=s.nextInt();
- //--------calling sumLimit method------
- int sum=sumLimit(n,m);
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement