- 2

Write a program to enter the 8 digit number through keyboard and print the sum of all digits.

Please help me guys in this question tell me answer in c language.

7th Sep 2019, 6:27 AM
Aditya Rana
Aditya Rana - avatar
12 Answers
+ 17
â–ș In C, normal int can store values in the range of -32,768 to 32,767. But you need an 8 digit number, so we have to use unsigned long int as our data type whose range is 0 to 4,294,967,295. â–ș So declare a number using unsigned long int and take the user input scanf("%lu",&num); // %lu is the format specifier for unsigned long int â–ș Then extract individual digits from the number using modulus (%) operator and keep adding the digits in another variable called sum. â–ș You can use a loop till the number is zero, take a digit, add it to sum and divide the number by 10, so that the digit which is added is removed from the number. â–ș At the end, display your result which is in sum.
7th Sep 2019, 7:02 AM
Nova
Nova - avatar
+ 13
Sure, everybody is ready to help to solve your doubts. But for help you need to show what all have you coded, where have you struck, what you don't understand. Show your attempt or else everyone will consider you want ready made solution and no one will be able to help !!
7th Sep 2019, 8:31 AM
Nova
Nova - avatar
+ 13
Yes the program is completely correct 👍, just the problem is of ASCII codes of blank space
7th Sep 2019, 10:26 AM
Nova
Nova - avatar
+ 12
Hello, 😊 If you need help you can post the code you are struggling with!  ‱ SEARCH for similar QUESTIONS or ANSWERS before posting  ‱ Include relevant TAGS Do not ask someone else to do it for you!!?  ‱ https://www.sololearn.com/post/75089/?ref=app  ‱ https://code.sololearn.com/WvG0MJq2dQ6y/
7th Sep 2019, 8:44 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 12
It shows some error when compiled. It is /302 error. This generally occurs due to copy and paste of code from somewhere. You had ASCII codes copied that add spaces, etc to your code. This can be removed by going through each identified line and remove any extra spaces at the beginning and end of any identified line. I just removed the space from lIne 4 and 12 of your code. The logic part is the correct, just those unnecessary blank spaces caused these errors. See the result after removing those spaces : https://code.sololearn.com/c6xxSOAk23dD/?ref=app
7th Sep 2019, 10:12 AM
Nova
Nova - avatar
+ 2
Please don't ask other people to do your homework for you. Show your own attempt.
7th Sep 2019, 8:08 AM
HonFu
HonFu - avatar
+ 1
Keval ya rha mera code #include <stdio.h>   void main() { long num, temp, digit, sum = 0;   printf("Enter the number \n"); scanf("%ld", &num); temp = num; while (num > 0) { digit = num % 10; sum = sum + digit; num /= 10; } printf("Given number = %ld\n", temp); printf("Sum of the digits %ld = %ld\n", temp, sum); }
7th Sep 2019, 9:52 AM
Aditya Rana
Aditya Rana - avatar
+ 1
Is it correct?
7th Sep 2019, 9:52 AM
Aditya Rana
Aditya Rana - avatar
+ 1
Ok bro means it is right only I have to remove the spaces
7th Sep 2019, 10:24 AM
Aditya Rana
Aditya Rana - avatar
+ 1
Ok bro thanks
7th Sep 2019, 10:30 AM
Aditya Rana
Aditya Rana - avatar
9th Sep 2019, 4:53 AM
GAYATHRI KOLLURI
GAYATHRI KOLLURI - avatar
- 2
Bro can you please write the code I need your help
7th Sep 2019, 7:40 AM
Aditya Rana
Aditya Rana - avatar