Java Program For Find Addition Of First and Last Digit Of Number.

 


Addition Of First and Last Digit Of Number.

// Java Program to find first and last and addition of it.
import java.util.*;
import java.lang.*;
class FirLastAdd{
    // Find the first digit
    public static int firstDigit(int n)
    {
        // Remove last digit from number
        // till only one digit is left
        while (n >= 10)
            n /= 10;
   
        // return the first digit
        return n;
    }

    // Find the last digit
    public static int lastDigit(int n)
    {
        // return the last digit
        return (n % 10);
    }
   
    // driver function
    public static void main(String argc[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number");
        int n=sc.nextInt();
        System.out.println(firstDigit(n) + + + lastDigit(n));
    }
}

 

Popular posts from this blog

When a method in a subclass overrides a method in superclass, it is still possible to call the overridden method using super keyword - Hacker Rank Solution.

You are given a date. You just need to write the method getDay which returns the day on that date - Hacker Rank Solution.