answersLogoWhite

0


Best Answer

String num = Integer.toString(12345); // Insert your Integer here.

int sum = 0;

for(int i = 0; i < num.length(); i++){

sum += Character.digit(num.charAt(i), 10);

}

System.out.println(sum); // Simply prints the sum of the digits to standard out.

///// ALTERNATE SOLUTION:

int num = 12345; // Insert your Integer here.

int sum = 0;

while (num > 0){

sum += num % 10;

num /= 10;

}

System.out.println(sum); // Simply prints the sum of the digits to standard out.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

public SumRevInt(String args) {

System.out.println("in constr");

if(args == null)

{

System.out.println("hello, enter a choice during execution");

}

else

{

char[] ch = args.toCharArray();

System.out.println("add(digits)="+add(ch));

System.out.println("rev(digits)="+rev(ch));

}

}

static int add(char[] num)

{

int t=0;

for(int i=0;i<num.length;i++)

{

t += Integer.parseInt(String.valueOf(num[i]));

}

return t;

}

static int rev(char[] num)

{

String s = "";

for(int i=num.length-1;i>=0;i++)

{

s += String.valueOf(num[i]);

}

return Integer.parseInt(s);

}

public static void main(String[] args) {

myObj = new SumRevInt(args[0]);

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

import java.io.*;

class sumo

{

protected static void main()throws IOException

{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the Number: ");

int a=Integer.parseInt(in.readLine()),b=0;

for(int i=a;i>0;i/=10)

{

int c=i%10;

b+=c;

}

System.out.print("Sum= "+b);

}

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The StringBuffer class has a method that does just that - you must simply convert your number to a StringBuffer object.

If you are supposed to program the loop yourself (this would be common for a study asignment), convert to String, then print out the digits one by one, in reverse order, using a forloop.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

public static void main(String[ ] args)

{

int 3digit_num; // Assuming it is a three digit number

int sum_of_first_n_last_digit = (3digit_num/100) + (3digit_num%100)

System.out.println(sum_of_first_n_last_digit);

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

int middleDigit(final int n) { return (n/10) % 10;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you find in java the sum of the first and last digit of 3 digit number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How to write a java program to extract the last digit from the input?

Assuming you've entered a multi-digit number whole number (an integer), then take the modus (%) of the number and 10. E.g., if the number input was 1234, then 1234 % 10 is 4. Thus the final digit is 4. Note that modus 10 is the same as dividing the number by 10 and taking the remainder.


To find strong number in java?

sir , what is perfect no?


Can you get a Java program to find the cube of a number?

Yes


Write a java program to print the last digit in Fibonacci series?

Just generate the Fibonacci numbers one by one, and print each number's last digit ie number%10.


How do you write a Java program to accept a 3-digits integer number and print out the highest digit from the derived input?

The tricky part is getting the individual digits. There are basically two ways to do this: 1) Convert the number to a string, and use string manipulation to get the individual digits. 2) Repeatedly divide the number by 10. The digit is the remainder (use the "%" operator). To actually get the highest digit, initially assume that the highest digit is zero (store this to a variable, called "maxDigit" or something similar). If you find a higher digit, replace maxDigit by that.


How do you find out whether Java is installed on Linux?

Enter "java -version" into a terminal. If Java is installed, it will tell you the version number. If it is not installed, it will say "command not found."


Where can one find a Java development kit?

Java Development Kits are best found on the Java website. There are a number of programmes that can be downloaded. It is also possible to get Java Development Kit downloads from the Oracle website.


How do you find a system number in a JAVA program?

If by system number you mean the version number of the Java language on the client computer, you can useSystem.getProperty("java.version"); .Or, if you want the Java virtual machine version number you can use System.getProperty("java.vm.version"); .Finally, if you want the version number of the operating system, you can use System.getProperty("os.version"); .


Which math function is used to find the square root of a number using java?

Math.sqrt(number) function is used to find the square root of a number.. try it


Example of inclusive or operator in java?

This is often used to specify several possibilities For example, assuming you have the last digit of a number in a variable "lastDigit":if (lastDigit 5)// In this case, the number is divisible by 5


How do you write a java program to find the square root of a number?

You can use the Math.sqrt() method.


Where can one find an array of different java application uses?

There are a number of sites that list the many uses of Java applications. The official Java website is one of these sites. Alternatively, one may also find this information at the web domain DynamicDrive.