Here's a C program to find the given number is Armstrong or not with output and proper explanation. This program also uses C while loop and modulus.

What is an Armstrong Number?A number is an Armstrong number if the sum of cubes of each digit of that number is equal to the number itself e.g. 153 is an Armstrong number i.e. 1^{3}+ 5^{3}+ 3^{3}= 153.

# include <stdio.h> # include <conio.h> void main() { int n, r, s = 0, t ; clrscr() ; printf("Enter a number : ") ; scanf("%d", &n) ; t = n ; while(n > 0) { r = n % 10 ; s = s + (r * r * r) ; n = n / 10 ; } if(s == t) printf("\n%d is an armstrong number", t) ; else printf("\n%d is not an armstrong number", t) ; getch() ; }

**Output of above program is**

Enter a number : 153

153 is an armstrong number.

**Explanation of above program**

Following is the list and use of each variable that we're using in this program -

- n - is the number that we're checking.
- t - is the temporary variable whose value is equal to n i.e. t = n.
- r - is used to store the remainder (we'll see that in a minute).
- s - is the sum of cubes of all digits of n.

We first ask the user to enter a number n and then we also store this value of n in temporary variable t. Then using the While loop we calculate sum of cubes of all digits of n. Outside the loop we check if the sum of cubes of all digits of n is equal to the number itself then we print that the number is Armstrong number otherwise the number is not an Armstrong number.

Suppose n = 153. So t is also 153. Let's see how while loop works in this example -

**n = 153 i.e. n > 0. While loop's first iteration -**

- First we calculate value of r -
**r = n % 10**= 153 % 10 = 3. - Next we update the value of sum i.e. s (currently equal to 0) as -
**s = s + (r * r * r) =**0 + (3 * 3 * 3) = 27. - Next we update the value of n by - n = n / 10 = 153 / 10 = 15. (Since n is of data type int only integer value of division is stored in n).

**n = 15 i.e. n > 0. While loop's first iteration -**

- First we calculate value of r -
**r = n % 10**= 15 % 10 = 5. - Next we update the value of sum i.e. s (now equal to 1) as -
**s = s + (r * r * r)**= 27 + (5 * 5 * 5 ) = 27 + 125 = 152. - Next we update the value of n by - n = n / 10 = 15 / 10 = 1.

**n = 1 i.e. n > 0. While loop's first iteration -**

- First we calculate value of r -
**r = n % 10**= 1 % 10 = 1. - Next we update the value of sum i.e. s (now equal to 126) as -
**s = s + (r * r * r) =**152 + (1 * 1 * 1)= 152 + 1 = 153. - Next we update the value of n by - n = n / 10 = 1 / 10 = 0.

n = 0 i.e n < 0. While loop is terminated now. Outside the loop, we compare the value of sum s and variable t and if these two are equal that means the number is Armstrong number.

Very informative article.Thank you author for posting this kind of article .

http://www.wikitechy.com/view-article/c-program-to-check-armstrong-number-with-example-and-explanation

Both are really good,

Cheers,

Venkat

Very informative article.Thank you author for posting this kind of article .

http://www.wikitechy.com/view-article/c-program-to-check-armstrong-number-with-example-and-explanation

Both are really good,

Cheers,

Venkat

Hola,

I learnt so much in such little time about #topic. Even a toddler could become smart reading of your amazing articles.

I'm doing a small tool. I compile the code with gcc in MingW.

One of its windows has a lot of normal controls on it (comboboxes, editboxes, textboxes, groupboxes...).

I'm trying to reduce the window flickering (when modifying the controls) by using this code:

Code:

//button click event

SetWindowRedraw(hWnd,FALSE);

//do a lot of modifications (hide/show/move/edit controls with their repaint disabled)

SetWindowRedraw(hWnd,TRUE);

//here I get the rect of the update region

RedrawWindow(hWnd, &updrect, 0, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN | RDW_UPDATENOW);

It flickers a little less, but it can be improved.

Is there a way?

I know about "double buffered", implemented in Delphi successfully but couldn't find a way in C.

I look forward to see your next updates.

Obrigado,