Call Support +91-85588-96644
TCYonline

Login

Sign Up

Please enter a Username or Email ID
Please enter a password
Keep me logged in
Please enter your name
Please enter your mobile number
You can't leave Captcha Code empty
By submitting this form, you agree to the Terms & Privacy Policy.
OR

Sign Up via Facebook

Sign Up via Google

Sign Up via Twitter

Download Software
Tests given

Download TCY App

App Image

Write a program to print the sum of the first and last digit of a number ? ex: n=1234 then sum=1+4; and sum=5;

Reply


Please type your answer before submitting.
Submit

public static void main(String args[]) {
int n=1234;
int a=n/1000;
int b=n%1000;
int c=b/100;
int d=b%100;
int e=d/10;
int f=d%10;
System.out.println(a);
System.out.println(c);
System.out.println(e);
System.out.println(f);
int g=a+f;
System.out.println("Sum of first and Last two number is="+g);

}

Use % to get last number and do the same to get first digit as well