Computer Science, asked by jchatterjee314, 2 days ago

write a program in java to initialize a four digit number display the last and last two digit of the number also print their sum​

Answers

Answered by chougulevaibhav01
0

Answer:

Algorithm

Step 1: Take a number from the user.

Step 2: Create two variables firstDigit and lastDigit and initialize them by 0.

Step 3: Use modulo operator(%) to find last digit.

Step 4: Use either while loop and division operator (/) or log() and pow() methods of Math class to find

.

Step 5: Display the result.

Using while loop

It is easy to use modulo operator that will return the last digit of the number, and we can use while loop and division operator to get first digit of number.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public class Main

{

public static void main(String args[])

{

int number = 502356997;

int firstDigit = 0;

int lastDigit = 0;

lastDigit = number%10;

System.out.println("Last digit: "+lastDigit);

while(number!=0) {

firstDigit = number%10;

number /= 10;

}

System.out.println("First digit: "+firstDigit);

}

}

Explanation:

last digit is 7

1st digit is 1

Similar questions