Write a C program to check whether a number is Armstrong number or not.
Question
Write a C program to check whether a number is Armstrong number or not.
Solution
Sure! Here is a step-by-step guide to write a program in C to check whether a number is an Armstrong number or not:
Step 1: Include the necessary header files.
#include <stdio.h>
#include <math.h>
Step 2: Define the main function.
int main() {
// Your code goes here
return 0;
}
Step 3: Declare the necessary variables.
int number, originalNumber, remainder, result = 0, n = 0;
Step 4: Take input from the user.
printf("Enter an integer: ");
scanf("%d", &number);
Step 5: Store the original number in a separate variable.
originalNumber = number;
Step 6: Calculate the number of digits in the given number.
while (originalNumber != 0) {
originalNumber /= 10;
++n;
}
Step 7: Calculate the Armstrong number.
originalNumber = number;
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += pow(remainder, n);
originalNumber /= 10;
}
Step 8: Check if the calculated result is equal to the original number.
if (result == number)
printf("%d is an Armstrong number.", number);
else
printf("%d is not an Armstrong number.", number);
Step 9: End the program.
return 0;
That's it! You have successfully written a C program to check whether a number is an Armstrong number or not.
Similar Questions
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.
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
Complete the code segment to check whether the number is an Armstrong number or not.Armstrong Number:
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
Single File Programming QuestionProblem StatementArun 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 number
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.