top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a Java program to find the Sum of last digits of two given numbers?

+2 votes
32,210 views

Rohit wants to add the last digits of two given numbers.
For example, If the given numbers are 267 and 154, the output should be 11.

Below is the explanation -
Last digit of the 267 is 7
Last digit of the 154 is 4
Sum of 7 and 4 = 11

Write a program to help Rohit achieve this for any given two numbers.

Note: The sign of the input numbers should be ignored. i.e.
if the input numbers are 267 and 154, the sum of last two digits should be 11
if the input numbers are 267 and -154, the sum of last two digits should be 11
if the input numbers are -267 and 154, the sum of last two digits should be 11
if the input numbers are -267 and -154, the sum of last two digits should be 11

posted May 13, 2017 by Tanay Hanuman Toor

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Use string and solve this problem in O(1) constant complexity.

2 Answers

+2 votes
import java.io.*;
import  java.util.*;

// Read only region start
class UserMainCode
{

    public int addLastDigits(int input1,int input2){
        // Read only region end
        int sum=0;

        if((input1<10&&input1>0)&&(input2<10&&input2>0))
            sum=input1+input2;
        else if(input1<0&&input2<0)
        {
            input1=0-input1;
            input2=0-input2;
            input1=input1%10;
            input2=input2%10;
             sum=input1+input2;
        }
        else if(input1<0||input2<0)
        {
            input1=input1%10;
            input2=input2%10;
             sum=input1-input2;
            sum=sum%10;
        }
        else
        {
            while((input1 >10)||(input2 >10))
            {
              input1=input1%10;
              input2=input2%10;
              sum=input1+input2;
            }
        }

        System.out.print(sum);
        return sum;
    }
}
answer Jul 3, 2017 by anonymous
+1 vote

import java.io.*;
import java.util.*;
class UserMainCode
{
public int addLastDigits(int input1,int input2){
input1=Math.abs(input1%10);
input2=Math.abs(input2%10);
return Math.abs(input1+input2);
}
}

Even this works

answer Nov 9, 2017 by anonymous
its simple and easy to understand... thank you.
Similar Questions
0 votes

import java.io.*
import java.util.*;
class UserMainCode
{
public static long output1;
public static void lastTwoDigitSum(int input1)
{}
}
in this format
plzz someone solve my problem

0 votes

Alex has been asked by his teacher to do an assignment on sums of digits of a number. The assignment requires Alex to find the sum of sums of digits of a given number, as per the method mentioned below.

If the given number is 582109, the Sum of Sums of Digits will be calculated as =
= (5 + 8 + 2 + 1 + 0 + 9) + (8 + 2 + 1 + 0 + 9) + (2 + 1 + 0 + 9) + (1 + 0 + 9) + (0 + 9) + (9)
= 25 + 20 + 12 + 10 + 9 + 9 = 85

Alex contacts you to help him write a program for finding the Sum of Sums of Digits for any given number, using the above method.

Help Alex by completing the login in the given function sumOfSumsOfDigits which takes as input an integer input1 representing the given number.
The function is expected to return the "Sum of Sums of Digits" of input1.

Assumptions: For this assignment, let us assume that the given number will always contain more than 1 digit, i.e. the given number will always be >9.

+3 votes

Write a program to print the sum of the element of the array with the given below condition?

If the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the numbers between them for the calculation of sum.

Array Elements - 10, 3, 6, 1, 2, 7, 9

Output: 22
i.e 10+3+9

+2 votes

Input: 1223
1+3=2*2
that means if sum of 1st and last digit is equal to product of remaining 2 digits then print 1+3=2*2 else error msg.

...