Write a c program to check whether a number is Armstrong or not. In this program, we take an input number from a user and check whether an input number is Armstrong or not.
Before we start solving this problem, let's understand what is Armstrong number?
An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.
For example - 153 , 371 is an Armstrong number.
Example 1 - 153 has 3 digits.
153 : 1 3 + 5 3 ;+ 3 3 = 153
Example 2 - Take an example of 6 which has 1 digit.
6 : 61 = 6 It's an Armstrong number
Example 3 - Take another example let's say 15, it has 2 digits.
15 : 12 + 52 = 26
So 15 is not an Armstrong number.
Example 4 - This time let's take bigger number 1634, it has 4 digits.
1634 : 1 4 + 6 4 + 3 4 + 4 4 = 1634
It's an Armstrong number.
We have understood the concept, let's solve this problem.
Program to count number of digits in a number
Program to check whether entered number is Palindrome or not
C, C++ Program for Practice
In this example, we first take an input number. Then we find the length of a number and then calculate the sum of a number (Which i have explained in examples). If entered number and the sum of a number is equal then it's an Armstrong number.
C program to calculate the sum of digits of a number
Output -
Enter a number : 153
153 is an Armstrong number
Programming questions for beginners
Program to check whether a number is prime or not
Before we start solving this problem, let's understand what is Armstrong number?
What is Armstrong number?
An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.
For example - 153 , 371 is an Armstrong number.
Example 1 - 153 has 3 digits.
153 : 1 3 + 5 3 ;+ 3 3 = 153
Example 2 - Take an example of 6 which has 1 digit.
6 : 61 = 6 It's an Armstrong number
Example 3 - Take another example let's say 15, it has 2 digits.
15 : 12 + 52 = 26
So 15 is not an Armstrong number.
Example 4 - This time let's take bigger number 1634, it has 4 digits.
1634 : 1 4 + 6 4 + 3 4 + 4 4 = 1634
It's an Armstrong number.
We have understood the concept, let's solve this problem.
Program to count number of digits in a number
Program to check whether entered number is Palindrome or not
C, C++ Program for Practice
C Program to Check Whether a Number is Armstrong or Not
In this example, we first take an input number. Then we find the length of a number and then calculate the sum of a number (Which i have explained in examples). If entered number and the sum of a number is equal then it's an Armstrong number.
C program to calculate the sum of digits of a number
#include <stdio.h> #include <math.h> int main() { int num, remainder, temp, sum = 0, n = 0; //Take input number printf("Enter a number"); scanf("%d", &num); temp = num; //Find the length of a number while(temp > 0) { n++; temp = temp/10; } //Again assign temp temp = num; while(temp > 0){ remainder = temp % 10; sum = sum + pow(remainder, n); temp = temp/10; } if (num == sum){ printf("\n %d is an Armstrong number ", num); } else { printf ("\n %d is not an Armstrong number ", num); } return 0; }
Output -
Enter a number : 153
153 is an Armstrong number
Programming questions for beginners
Program to check whether a number is prime or not
No comments:
Post a Comment