c language program to check if the given number is armstrong number or not using while loop.

Logic Used for Armstrong Consider the number 370 which is a three-digit number so it can be written as:
(370) -> (3*3*3) + (7*7*7)+(0)
0+343+27
=370
370 is Armstrong's number because it is the sum of the cubes of his digits.
Examples
•If the entered number is 370, then 370 = 3*3*3 + 7*7*7 + 0 = 27 + 343 + 0 = 370.
Since the sum of the cubes of every digit is equal to 370, the output will be “The given number is an Armstrong number.”
•If the entered number is 141, then 141 = 1*1*1 + 4*4*4 + 1*1*1 = 1 + 64 + 1 = 66.
Since the sum of the cubes of every digit is not equal to 141, the output will be “The given number is not an Armstrong number”


C Program to check whether a given number is Armstrong or not.

/*
* C Program to Check whether a given Number is Armstrong or not
*/
#include <stdio.h>
#include <math.h> // library file for pow() function
void main()
 {
 int n, sum = 0, rem = 0, cube = 0, temp;
 printf ("Enter any number:\n");
 scanf("%d",&n);
 temp = n;
 while (n!=0)  // loop will continue until the number n do not become 0.
 {
  rem =n%10;  // calculates the last digit of the number.
   cube = pow(rem, 3);  // The cube of the last digit is calculated here.
  sum=sum+cube;
   n=n/10;  // least significant bit of the number be removed.
 }
 if (sum==temp)
   printf ("\nThe given number %d is an Armstrong number",temp);
 else
   printf ("\nThe given number %d is not an Armstrong number",temp);
}

Output
Enter any number
370
The given number 370 is an Armstrong number


Explanation
1. In this C program, we read a numerical value using variable 'n'.
2. The Armstrong number is an n-digit number in base b such that the sum of its digits increases to the power of n.
So 371 because 33 + 73 + 13 = 27 + 343 + 1 = 371.
3. Check the value of the variable "n" is not equal to 0 using while loop.
4. If the condition is true, repeat the loop.
5. The variable ‘rem’ is used to calculate the coefficient of the value of the variable "n" 10
and the variable "cube" is used to calculate the cube of the value of the variable "rem" using pow().
6. Then the variable ‘sum’ is used to calculate the sum of the value of the variable
‘sum’ with the value of the variable ‘cube’.
7. The If-else statement is used to check whether the value of the ‘sum’
variable is equal to the value of the ‘temp’ variable.
8. If the condition is true, Armstrong's number will be printed. Otherwise,
the else conditional statement is executed and Not Armstrong's number is printed.


Time Complexity: O(log(n))
The above program to check whether a number is Armstrong or not has a time complexity of O(log(n)) because the while loop is executed log(n) times because at each iteration the number is divided by 10, where n is a number. given as input.
Space Complexity: O(1)
In Armstrong's numerical program, the space complexity is O(1) because no additional variables are used to store the values. All initialization variables take constant O(1) space.


Previous :-->> 7.Write a program in c programming to calculate the power of a number using a for loop and while loop.
 -->> NEXT: 9.Write a program in c language to print the pattern of a pyramid using a for loop.
-->>ALL Loops Assignments in c