answersLogoWhite

0


Best Answer

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
void main()
{
clrscr();
int n,num,x,sum=0;
printf("Enter a number=");
scanf("%d",&n);
while(n>0)
{
x=n%10;
sum=sum+x;
n=n/10;
}
printf("Sum of digits of a number=%d",sum);
getch();
}
User Avatar

Wiki User

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

Wiki User

14y ago

#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;

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#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;
}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#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);

}

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

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;

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

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;
}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c programme to find sum of individual digits of a given positive integers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How many positive integers less than 1000 have no odd decimal digits?

52


Sum of all the digits of the integers from 1 to 2008?

The answer is 28 054


How do you show a positive integer for example 12345678901234567890 in C sharp that has more than 17 digits?

There is Int64 class, it will do it.


Write a c plus plus program that inputs 5 integers from the user and separates the integer into individual digits and prints the digits separated from one another by three spaces each.?

// 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 &lt; 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 &lt; ints.length; ++i) { // get individual digits if (ints[i] == 0) { System.out.println(0); } else { while (ints[i] &gt; 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).


How do you extract digits from an integer in java?

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 &gt; 0), and copy the digits to an array.