A number is called as the armstrong number if the actual number and revese of that number both are equal. For example: 24342 is a armstrong number.
// Write a c program to check the given number is armstrong or not
#include<stdio.h>
// function to reverse the number.
int reverse(int n){
int reverse = 0;
while(n > 0){
int last_digit = n % 10; // get the last digit
reverse = reverse * 10 + last_digit; // last digit store in the reverse variable
n = n / 10; // Remove the last digit
}
return reverse;
}
int main(){
int n;
printf("Enter the number : ");
scanf("%d", &n);
if(n == reverse(n)){
printf("%d is an armstrong number", n);
}else{
printf("%d is not an armstrong number", n);
}
return 0;
}
#include<stdio.h>
Include the standard input/output library to use the built-in function like printf() for output and scanf() for input.
// function to reverse the number.
int reverse(int n){
int reverse = 0;
while(n > 0){
int last_digit = n % 10; // get the last digit
reverse = reverse * 10 + last_digit; // last digit store in the reverse variable
n = n / 10; // Remove the last digit
}
return reverse;
}
The above function reverse() is used to reverse of any number. In Details Read now.
// main function
int main(){
int n;
printf("Enter the number : ");
scanf("%d", &n);
if(n == reverse(n)){
printf("%d is an armstrong number", n);
}else{
printf("%d is not an armstrong number", n);
}
return 0;
}
It is the main function and also known as the entry point of the program, declared a variable n as integer. Prompt to user enter the number and reads the input from the user and store the value in n. check given number and reverse number both are same using the if-else statement then number is armstrong otherwise given number is not armstrong. In the end main function return 0, which indicated that program is successfully executed.
Comments
Oops!