You're online now.

Hurray! you are online now.

Write a c program to check the given number is armstrong or not

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.

Algorithm

1. Start
2. Declared a variable named n as integer
3. Read the input from the user
4. Check n == reverse(n) equal then print "armstrong number"
5. Otherwise print "not an armstrong number"
6. End

Pseudocode

1. INCLUDE stdio.h
2. FUNCTION reverse(int n)
    1. SET reverse = 0
    2. while n > 0
        1. SET last_digit = n % 10
        2. SET reverse = reverse * 10 + last_digit;
        3. SET n = n / 10
    RETURN reverse
3. FUNCTION main()
    1. DECLARE n AS INTEGER
    2. PROMPT "Enter the number : "
    3. READ n from the USER
    4. IF n == reverse(n)
        PRINT "armstrong number"
    5. ELSE
        PRINT "not an armstrong number"
    6. RETURN 0
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...