Computer Science, asked by diwakar5283, 2 months ago

Write a program in java to accept 3 numbers and find the sum of their last digits. ( don't spam) and no irrelevant answers​


rina26112: o hello what is your problem why are you following and unfollowing me
diwakar5283: sorry
rina26112: it's ok
diwakar5283: l am just making your 400 followers
rina26112: don't do it next time
rina26112: ok

Answers

Answered by vedantimore2003
0

Answer:

okk but see i am not conferm for this ans listen one thing..

okk but see i am not conferm for this ans listen one thing..I asked to my teacher he is sending me one more program plzz wait... And one more think sorry if i don't give correct ans

Procedure to find the sum of first and last digit of a number in java,

Take a number

Declare sum variable and initialize it with 0

Find the last digit. To find the last digit we can use % operator. The expression number%10 gives the last digit of the number.

Find the first digit of the number. For this

Find the total number of digits in the given number.

Divide the number by 10^(total_number_of_digits-1), it will give the first digit of the number.

Add first and the last digit to the sum variable.

How to find or get the last digit of a number in java?

You can easily find the last digit of a number in Java using % operator. Any number%10 gives the last digit of the number. Below code in Java is used to get the last digit of a number,

int number = 1234;

int last_digit = number % 10;

So, last_digit = 1234 % 10 = 4

How to find or get the first digit of a number in Java?

To find or get the first digit of a number in Java, First of all, we need to count the total number of digits in the given number.

int number = 1234;

So, total_digits = 4;

After calculating number of digits, calculate the power of 10 ^ (total_digits-1)

int divisor = (int) Math.pow(10, total_digits-1);

int first_digit = number / divisor;

So, divisor = (int)Math.pow(10, 3) = 1000

The return type of the pow(-,-) method of Math class is double. We are working with integer values so convert it into an integer using type-casting. You can also use your own method to find the power of a given number. Now, divide the number using the power value.

Similar questions