Write a program to print sum of digits of a number in java

Steve Donovan steve j donovan at gmail com; version 2. Creative Commons License ; Reproduction in any form is permitted as long as this attribution is kept. Lua is a modern dynamic language with conventional syntax:

Write a program to print sum of digits of a number in java


The so-called educator wanted to keep the kids busy so he could take a nap; he asked the class to add the numbers 1 to Gauss approached with his answer: The teacher suspected a cheat, but no. Manual addition was for suckers, and Gauss found a formula to sidestep the problem: Pair Numbers Pairing numbers is a common approach to this problem.

As the top row increases, the bottom row decreases, so the sum stays the same. And how many pairs do we have?

List of Java Programs

Wait — what about an odd number of items? What if we are adding up the numbers 1 to 9? Many explanations will just give the explanation above and leave it at that.

However, our formula will look a bit different.

write a program to print sum of digits of a number in java

If you plug these numbers in you get: Yep, you get the same formula, but for different reasons. Use Two Rows The above method works, but you handle odd and even numbers differently. The total of all the numbers above is But we only want the sum of one row, not both.

So we divide the formula above by 2 and get: Now this is cool as cool as rows of numbers can be. It works for an odd or even number of items the same!

write a program to print sum of digits of a number in java

Make a Rectangle I recently stumbled upon another explanation, a fresh approach to the old pairing explanation. Different explanations work better for different people, and I tend to like this one better. Instead of writing out numbers, pretend we have beans.

We want to add 1 bean to 2 beans to 3 beans… all the way up to 5 beans.

Integer - Wikipedia

How do we count the number of beans in our pyramid?Let us use an array of characters (str) to store the string and variables nletter, ndigit, nspace and nother to count the letters, digits, white spaces and other characters, respectively. Lua Unofficial FAQ (uFAQ) 1 Language.

Where to start? Suitability as a first programming language? Suitability as a second programming language? Jul 28,  · Understand the Checksum algorithm. This is the algorithm that is used to validate the authenticity of the routing number.

It is a series of multiplication and addition operations performed on the digits in the routing number.

C Program to Count Digits,White Space,Others

Task. Write a program where the program chooses a number between 1 and A player is then prompted to enter a guess. If the player guesses wrong, then . A binary heap (min-heap) is a complete binary tree with elements from a partially ordered set, such that the element at every node is less than (or equal to) the the element at it's left and right child.

Here is a more generic way to think about this that lets you calculate any equally spaced series of numbers: 1+2+3+4+5 = n = number of digits in the set.

Lua Unofficial FAQ (uFAQ)