Sum of Unit and Most Significant Digits

 

Java Code:

Method I:

import java.util.*;
public class Hello {

    public static void main(String[] args) {
        String n;
        Scanner s=new Scanner(System.in);
        n=s.nextLine();
        int l=n.length();
        int m=Character.getNumericValue(n.charAt(0));
        int ls=Character.getNumericValue(n.charAt(l-1));
         System.out.print(m+ls);

    }
}

 Method II :

 import java.util.*;
public class Hello
{
    public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n,l,m=0;
n=s.nextInt();
l=n%10;
while(n>0)
{
    m=n%10;
    n/=10;
}
System.out.print(l+m);
    }
}

Comments

Popular posts from this blog

Fisherman's Mantra

First & Last X Digits

N Integers – Sum S – All Combinations(Weekly Test)