Knowee
Questions
Features
Study Tools

Write the C code to Check whether a given 4 digit number is an Armstrong number or not. The formula to be used here. if 1234 is the input then armstrong number is(1^4+2^4+3^4+4^4)Sample Input:n=1532Output:Not ArmstrongSample Input:n=1634Output:Armstrong

Question

Write the C code to Check whether a given 4 digit number is an Armstrong number or not. The formula to be used here. if 1234 is the input then armstrong number is(1^4+2^4+3^4+4^4)Sample Input:n=1532Output:Not ArmstrongSample Input:n=1634Output:Armstrong

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the C code to check whether a given 4 digit number is an Armstrong number or not:

#include <stdio.h>
#include <math.h>

int main() {
    int num, originalNum, remainder, n = 0;
    float result = 0.0;

    printf("Enter an integer: ");
    scanf("%d", &num);

    originalNum = num;

    // store the number of digits of num in n
    for (originalNum = num; originalNum != 0; ++n) {
        originalNum /= 10;
    }

    originalNum = num;

    // calculate the sum of the nth power of each digit
    while (originalNum != 0) {
        remainder = originalNum%10;
        result += pow(remainder, n);
        originalNum /= 10;
    }

    // check if result is same as num
    if((int)result == num)
        printf("%d is an Armstrong number.", num);
    else
        printf("%d is not an Armstrong number.", num);

    return 0;
}

This program first counts the number of digits in the input number. Then it calculates the sum of the nth power of each digit. Finally, it checks if the calculated sum is equal to the original number. If they are equal, the number is an Armstrong number. Otherwise, it is not.

This problem has been solved

Similar Questions

Write a C program to check whether a number is Armstrong number or not.

Write a C program to determine if a given integer is an Armstrong number or not. An Armstrong number (also known as a narcissistic number or pluperfect digital invariant) is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 13+53+33=15313+53+33=153.Design a function called isArmstrong that takes an integer num as input and returns 1 if num is an Armstrong number, and 0 otherwise. Your program should prompt the user to enter an integer, call the isArmstrong function to check if it is an Armstrong number, and display an appropriate message indicating whether the entered number is an Armstrong number or not.inputEnter a number: 153output153 is an Armstrong number.

Complete the code segment to check whether the number is an Armstrong number or not.Armstrong Number:

You are given an integer 'n'. Return 'true' if 'n' is an Armstrong number, and 'false' otherwise.An Armstrong number is a number (with 'k' digits) such that the sum of its digits raised to 'kth' power is equal to the number itself. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371.

Arun wants to implement a function to check whether an integer n is an Armstrong number or not using a call-by-reference function. Assist him in completing the program.An Armstrong number is a number that is equal to the sum of its digits each raised to the power of the number of digits.Function Specifications: void checkArmstrong(int *n)Note: This question was asked in companies like Gemini Solution,TCS, Infosys, and Unthinkable Solutions.Input format :The input consists of an integer n.Output format :If n is an Armstrong number, the output prints "Armstrong number".If n is not an Armstrong number, the output prints "Not an Armstrong number".Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 1000Sample test cases :Input 1 :153Output 1 :Armstrong numberInput 2 :29Output 2 :Not an Armstrong numberInput 3 :1Output 3 :Armstrong numberInput 4 :1000Output 4 :Not an Armstrong numberNote :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.