Question:

What is the output of the following C program?
#include #include main() 
{ int sum = 0, n = 1729; 
do { sum += n % 10; n = n / 10; } 
while (n>0); printf("%d", sum); }

Show Hint

When summing digits of a number, use the modulus operator to get the last digit and integer division to reduce the number.
Updated On: May 3, 2025
  • 19
  • 0
  • 28
  • 9271
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

This program sums the digits of the number 1729. In each iteration, the program adds the last digit (using \( n % 10 \)) and divides \( n \) by 10, effectively removing the last digit. The digits of 1729 are 1, 7, 2, and 9, and their sum is \( 1 + 7 + 2 + 9 = 19 \).
Thus, the correct answer is \( 19 \).
Was this answer helpful?
0
0

Top Questions on Programming in C

View More Questions