int sumDigits(int n) {
int sum = 0;
while( n > 0 ) {
sum += (n % 10); // add last digit of n to sum
n /= 10; // divide n by 10 to "chop off" the last digit
}
return sum;
}
____________________________________________________
C program to find the sum of entered digit: By Jatinder Pal Singh#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string
int main()
{
const int numberOfdigits = 6;
string myNumber = "0";
char myNumberChar[numberOfdigits] = {0};
cout << endl << "Enter 5 digit integer: ";
cin >> myNumber;
int sumOfDigits = 0;
int temp = 0;
for (int arrayIndex = 0; arrayIndex < (numberOfdigits - 1); arrayIndex++)
{
temp = atoi(&myNumber.substr(arrayIndex, 1)[0]);
sumOfDigits += temp;
}
cout << endl << "Sum of 5 digits is: " << sumOfDigits << endl;
system("PAUSE");
return 0;
}
#include
int main()
{
int num, dig, sum = 0; /* be sure to initialize sum to 0 */
printf("Enter number:");
scanf("%d", &num); /* num is our actual number */
while (num > 0) /* While we still have digits in num */
{
dig = num % 10; /* Get the rightmost digit in num... */
sum += dig; /* and add it to sum */
num = num / 10; /* Get rid of the rightmost digit in num */
}
printf("Result: %d", sum);
return 0;
}
#include
#include
void main()
{
int sumdig(int);
int n,sum,d,f,rev=0;
clrscr();
printf("enter the number");
scanf("%d",&n);
sum=sumdig(n);
printf("%d",sum);
getch();
}
int sumdig(int n)
{
int sum,d,r=0;
if(n==0)
return(0);
else
sum=n%10+sumdig(n/10);
while(sum!=0)
{
d=sum%10;
r=r+d;
sum=sum/10;
}
return(r);
}
To determine the value of the least-significant digit in a number, you divide by the base and take the remainder. To get the next digit, divide by the base and repeat. The following function will sum the digits of any integer (the length does not matter):
int sum_digits (int num, int base=10) { // assume decimal notation by default
int sum = 0;
while (num) {
sum += num%base; // add the least-significant digit
num /= base; // move all digits one place to the right
}
return sum;
}
I am writing only the function
int sum_of_digit(int num)
{
int sum=0;
while(num)
{
sum=sum+num%10;
num=num/10;
}
return sum;
}
52
The answer is 28 054
There is Int64 class, it will do it.
// create an BufferedReader from the standard input stream BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String currentLine = ""; int total = 0; int[] ints = new int[5]; // read integers while (total < 5) { // get input System.out.print("Input an integer: "); currentLine = in.readLine(); // parse as integer try { int input = Integer.parseInt(currentLine); ints[total] = input; ++total; } catch (final NumberFormatException ex) { System.out.println("That was not an integer."); } } // print each number for (int i = 0; i < ints.length; ++i) { // get individual digits if (ints[i] == 0) { System.out.println(0); } else { while (ints[i] > 0) { System.out.println(ints[i] % 10); ints[i] /= 10; } } } Note that this prints out the digits in reverse order (2048 will print 8 first and 2 last).
There are different ways to do it. One is to convert it to a String, then use the string manipulations methods to extract individual digits as strings. You can then convert them back to numbers. Another is to do some calculations. For example, to get the last digit: int i = 12345; int lastdigit = i % 10; //To get additional digits, divide by 10 and repeat: i /= 10; int lastdigit = i % 10; In this case you can create a loop for this (repeating while i > 0), and copy the digits to an array.
The sum of all the digits of all the positive integers that are less than 100 is 4,950.
279,999,720
50%
Three of them.
They are 13.
405
There are 21.
52
There are none.
Every number from 100 to 999 inclusive !
3
The sum of the digits is 6.