[go: up one dir, main page]

0% found this document useful (0 votes)
9 views3 pages

Explanation

Uploaded by

SRDC CDE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Explanation

Uploaded by

SRDC CDE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Here’s a C Program to find the sum of digits of an integer with output and proper explanation.

The program uses while loop.

Explanation of above program

In this program we are first inputting an integer, counting the sum of digits of that number and
finally displaying the output to the user. Let’s see how the above program does this.

There are three variables -

 n - An integer to store the number.


 s - An integer to store the sum of digits of n. s is initialized to 0.
 r - An integer that stores the remainder (we will get to that in a minute)

Now we are asking the user to enter a number and storing it in the variable n. After that our
while loop calculates the sum of digits of n.

To understand what is happening in while loop it’s better to take an example. Suppose, user
entered 1234. The process of while loop with n = 1234 is as follows -
Facts: Whenever you take modulus of a number with 10 i.e. modulo 10, it always returns the last
digit of that number.

 First n > 0 i.e. 1234 > 0, so the program will enter in the while loop.
 In the next step, n % 10 is calculated and stored in r i.e. r = 1234 % 10 = 4 (last digit of
n).
 Now the above value of r is added to the variable s. After this step, s = 4.
 Now the number (n) is divided by 10 and result is stored again in n i.e. n = n / 10 or n =
1234 / 10. Now as you know when you divide two numbers and store the result in an
integer variable, only integer part of that result is stored in that integer variable not the
fractional part. So when you divide 1234 by 10, even when the correct result is 123.4 the
new value of n will be 123 i.e. after this step n = 123 and s = 4.
 Now that all statements inside the while loop has been executed once, the looping
condition is again checked.
 Again n > 0 i.e. 123 > 0, so the program will again enter the while loop and above steps
are followed in the same manner.
 Again, n % 10 is calculated and stored in r i.e. r = 123 % 10 = 3 (last digit of n).
 Now the above value of r is again added to the variable s. After this step, s = 7.
 Now the number (n) is divided by 10 and result is stored again in n i.e. n = n / 10 or n =
123 / 10. So after this step n = 12 and s = 7.
 Again n > 0, so the while loop will again execute following the above process.
 Once the value of n becomes 0 or less than 0, the while loop will exit and rest of the
program will continue execution.
Here’s a C Program to find the sum of digits of an integer with output and proper explanation.
The program uses while loop.

Explanation of above program

In this program we are first inputting an integer, counting the sum of digits of that number and
finally displaying the output to the user. Let’s see how the above program does this.

There are three variables -

 n - An integer to store the number.


 s - An integer to store the sum of digits of n. s is initialized to 0.
 r - An integer that stores the remainder (we will get to that in a minute)

Now we are asking the user to enter a number and storing it in the variable n. After that our
while loop calculates the sum of digits of n.

To understand what is happening in while loop it’s better to take an example. Suppose, user
entered 1234. The process of while loop with n = 1234 is as follows -
Facts: Whenever you take modulus of a number with 10 i.e. modulo 10, it always returns the last
digit of that number.

 First n > 0 i.e. 1234 > 0, so the program will enter in the while loop.
 In the next step, n % 10 is calculated and stored in r i.e. r = 1234 % 10 = 4 (last digit of
n).
 Now the above value of r is added to the variable s. After this step, s = 4.
 Now the number (n) is divided by 10 and result is stored again in n i.e. n = n / 10 or n =
1234 / 10. Now as you know when you divide two numbers and store the result in an
integer variable, only integer part of that result is stored in that integer variable not the
fractional part. So when you divide 1234 by 10, even when the correct result is 123.4 the
new value of n will be 123 i.e. after this step n = 123 and s = 4.
 Now that all statements inside the while loop has been executed once, the looping
condition is again checked.
 Again n > 0 i.e. 123 > 0, so the program will again enter the while loop and above steps
are followed in the same manner.
 Again, n % 10 is calculated and stored in r i.e. r = 123 % 10 = 3 (last digit of n).
 Now the above value of r is again added to the variable s. After this step, s = 7.
 Now the number (n) is divided by 10 and result is stored again in n i.e. n = n / 10 or n =
123 / 10. So after this step n = 12 and s = 7.
 Again n > 0, so the while loop will again execute following the above process.
 Once the value of n becomes 0 or less than 0, the while loop will exit and rest of the
program will continue execution.
Here’s a C Program to find the sum of digits of an integer with output and proper explanation.
The program uses while loop.

Explanation of above program

First take a look at all the variables. All variables are of Long Data Type-

 n - is the number that user enters.


 a[10] - is an array of size 10 and is used to store each digit of the above number n at
separate indexes.
 i - is the loop variable.
 c - is a counter variable which is initialized to zero at the start of the program.

First we ask the user to enter a number (n can contain up to 10 digits for now) and stores it in
variable n. Then using while loop, we're storing each digit of number n inside the array a[10].
Remember each digit is stored at a separate index. After the while loop the array a[10] will
contain every digit of n but in reverse order e.g. if n = 123 then a[0] = 3, a[1] = 2 and a[2] = 1.
(How to Reverse an Integer. Read it here.)

After this we're converting each digit to words with the help of for loop and switch case.
Remember that the array contains the values in reverse order so we're also executing our for loop
in reverse direction too.

Let's understand this program with an example. Suppose n = 123 then the steps involved are -

You might also like