C programming for Armstrong number.

C program for printing Armstrong Number.

What is Armstrong Number ?
Armstrong Number are those number whose number of digit is the power of digit and there sum. If sum is equal to the number the that number is call Armstrong Number.
Example:- If we assume three number 153,8208 and 123.
153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Another example.
8208 = 8^4 + 2^4 + 0^4 + 8^4 = 4096 + 16 + 0 + 4096 = 8208
So these numbers are example of Armstrong number.

Another example but this number is not Armstrong Number.
123 = 1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36
Which is not equal to the original number so it is not a Armstrong Number. 
So these numbers are example of Armstrong number.

Let us look at programming of it.

Programming

#include<stdio.h>
#include<conio.h>
int main()
{
int num, count = 0 , temp, num_1; //Data type deceleration.
/*
num = user input number to check ether it is Armstrong Number or not.
count = storing sum value.
temp = Temporary variable for storing num value.
num_1 = for storing remainder.
*/
printf("Enter any number");
scanf("%d",&num);  // For user input.
temp = num;        // temporary storing the value.
while( temp > 0)   //Condition to stop the loop
{
num_1 = temp%10;  //remainder
count += (num_1*num_1*num_1);  //power of that number and adding with count
temp = temp/10;     // decreasing number
}
if(num == count)        //Checking condition for Armstrong Number
{
printf("%d is Armstrong Number",num);
}
else
{
printf("%d is not an Armstrong Number",num);
}
getch();
return 0;
}

Output

Output for Armstrong Number.

Fig:- 1

Output for non Armstrong Number

Fig:-2

This is only three digit number you can't use if for 4 digit number. When you will enter 4 digit number it will never show that it is an Armstrong Number.
If you face any problem then comment in comment section. Go to my YouTube channel Video.

Now the general solution.

This is general solution means this programming can be used for ever digit number means this programming is valid for any natural number. 

Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int num,temp,temp_1 ,count=0,count_1=0;
/*
Data type deceleration.
num = It is for user input number.
temp = it is for temporary variable.
temp_1 = It is for temporary variable to store remainder.
count = It is for counting the digit of the number.
count_1 = it is for storing power of digit with sum. 
*/
printf("Enter the number");
scanf("%d",&num);   // User input.
temp=num;
while(temp>0)      // This loop is for counting the digit of the number.
{
temp = temp/10;
count++;
}
temp = num;
while(temp>0)      // This loop is for adding the power of that number
{
temp_1= temp%10;
count_1+= pow(temp_1, count);
temp = temp/10;
}
if(count_1 == num)   // Condition for either number if Armstrong number or not. 
{
printf("%d is an Armstrong Number",num);
}
else
{
printf("%d is not an Armstrong Number",num);
}
getch();
return 0;
}

Output
Output for the general solution with 4 digit number

Fig:-3

Output for the general solution with 3 digit number

Fig:-4

If you face any problem then comment in comment section. Go to my YouTube channel Video.


Comments

Post a Comment

Popular posts from this blog

Abundant Number in C Programming || Learning Duniya

Automorphic Number in C Programming || Learning Duniya

How to Print Given Star Pattern