Sum The Digits Of A Number | C Programming Example

  Рет қаралды 8,092

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 13
@carmenniro8021
@carmenniro8021 3 жыл бұрын
Hi! I'm new to programming and I've found your video very helpful . I wanted to ask you something. Let's say that I wanted to print not just the sum of the digits of a given number, but, if the result is greater that 9, like in your case it was 12, is there a way to print 3 (1 + 2) instead of a two digit number?
@PortfolioCourses
@PortfolioCourses 3 жыл бұрын
I might be able to help, but I'm not sure I understand. So instead of "12" you would want to print "1 + 2"? Or instead of "12" you would want to print "2 + 3 + 7", the numbers used to add up to "12"?
@carmenniro8021
@carmenniro8021 3 жыл бұрын
In your video, the program prints 12 but, I'd want to print only one digit number. So in this case, before printing 12, the program has to sum the result and print 3
@PortfolioCourses
@PortfolioCourses 3 жыл бұрын
@@carmenniro8021 Here is a version of the program that should do this. This one will keep summing the digits of the resulting sum until it is less than 10 (i.e. a one digit number). So it may sum the digits more thna one time. #include int main() { int number = 0; int sum = 0; int digit = 0; printf("Enter number: "); scanf("%d", &number); int sums = 0; do { // reset variables if necessary if (sums > 0) { number = sum; sum = 0; } while (number != 0) { digit = number % 10; sum += digit; number = number / 10; } sums++; } while (sum >= 10); printf("sum: %d ", sum); return 0; }
@carmenniro8021
@carmenniro8021 3 жыл бұрын
@@PortfolioCourses thank you! 😊
@ReginaldCGray
@ReginaldCGray Жыл бұрын
Great but what if they enter a negative number how do you handle that using this code
@PortfolioCourses
@PortfolioCourses Жыл бұрын
This code is not created to handle negative numbers. You could always apply abs() to the negative number before this algorithm to make the number positive: www.tutorialspoint.com/c_standard_library/c_function_abs.htm. :-)
@drago-ig6ms
@drago-ig6ms Жыл бұрын
YOU ARE AMAZING
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Aww thank you for the kind feedback! :-)
@mccauleybacalla2228
@mccauleybacalla2228 4 ай бұрын
thanks!
@JaxonLo-y1g
@JaxonLo-y1g 2 ай бұрын
‰ Tutorial: Press Down On %
@jsnam8139
@jsnam8139 3 жыл бұрын
super!
Reverse An Integer Number | C Programming Example
6:30
Portfolio Courses
Рет қаралды 14 М.
C++ Program to Find the Sum of All Digits of a Number
9:23
Example Program
Рет қаралды 67 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
Sum of Digits in the Given Number | Logical Programming in C | Naresh IT
5:02
Naresh i Technologies
Рет қаралды 80 М.
Sorting An Array Of Strings | C Programming Example
8:46
Portfolio Courses
Рет қаралды 30 М.
C Program to find Sum of Digits of a Number | Learn Coding
9:18
Learn Coding
Рет қаралды 248 М.
Check If A Number Is Prime | C Programming Example
11:57
Portfolio Courses
Рет қаралды 12 М.
Check If A Number Is Prime Using Recursion | C Programming Example
11:05
Portfolio Courses
Рет қаралды 6 М.
3.18.1 Example extract digit of number in C
8:12
CSE Learning
Рет қаралды 2 М.
Compute the Average of an Array | C Programming Example
8:39
Portfolio Courses
Рет қаралды 15 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН