Sum of digits program in c


















Add a comment. Active Oldest Votes. You are using uninitialized variables int sum, n, p; that have indeterminate values. As a result your program has undefined behavior. Improve this answer. Vlad from Moscow Vlad from Moscow k 18 18 gold badges silver badges bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.

Post as a guest Name. This is what an array looks like: Source: w3resource. Enter size of the array : 5. Enter elements in array : 7.

Using Function. Enter elements in array : 1. Using Recursion. Given a number, find sum of its digits. Print or return the sum Below are the solutions to get sum of the digits. How to compute in a single line? The below function has three lines instead of one line, but it calculates the sum in line. It can be made one-line function if we pass the pointer to sum. Recursive Thanks to Ayesha for providing the below recursive solution.

So take input as a string, run a loop from start to the length of the string and increase the sum with that character in this case it is numeric Below is the implementation of the above approach. Skip to content. Change Language. Improve this question. Derek Wang 9, 4 4 gold badges 15 15 silver badges 37 37 bronze badges.

Yagya Yagya 29 1 1 gold badge 1 1 silver badge 8 8 bronze badges. Show what you have tried — Gopi. I have reversed the number and put it in array and elements at odd or even places but it too long. That will automatically reverse. Please edit your post and add the code there. As it is currently written , I am not able to find a definition of rem , sum or summ , a[i] etc.

Show 3 more comments. Active Oldest Votes. Improve this answer.



0コメント

  • 1000 / 1000