Armstrong Number Program in C

Google Advertisements

Prev Tutorial Next Tutorial

C Program to check Armstrong or Not

A Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits is equal to the number itself.

For example:
Three Digits Armstrong number is 153, 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153
Four Digits Armstrong number is 1634, 1 ^ 4 + 6 ^ 4 + 3 ^ 4 + 4 ^ 4 + = 1634

armstrong number program in c

3 Digits Armstrong number

Armstrong number program in C

#include<stdio.h>
#include<conio.h>

void main()
{
int temp, arm=0,a,b,c,d,num; 
clrscr();
printf("Enter any number: ");
scanf("%d",&num);
temp=num;
while(temp>0)
{
a=temp%10;
temp=temp/10;
arm=arm+a*a*a;
}
if(arm==num)
{
printf("%d is armstrong number",num);
}
else
{
printf("%d is not armstrong number", num);
}
getch();
}

Output

Enter any number: 23
23 is not armstrong number

Any number of digits Armstrong number program in C

Armstrong number program in C

#include<stdio.h>
#include<conio.h>
void main()
{
 int num;
 int f,rem,sum=0,temp,a=0;
 clrscr();
 printf("Enter any number: ");
 scanf("%d",&num);
 temp=num;
 while(temp != 0)
 {
 temp=temp/10;
 a=a+1;
 }
 f=num;
 while(f!=0)
 {
 rem=f%10;
 sum = sum + power(rem,a);
 f=f/10;
 }

 if( sum == num )
 printf("%d is an armstrong number ",num);
 else
 printf("%d is not an armstrong number",num);
 getch();
}

int power(int c, int d)
{
 int pow=1;
 int i=1;
 while(i<=d)
 {
  pow=pow*c;
  i++;
 }
  return pow;
}

Output

Enter any number: 153
153 is an armstrong number

Explanation of code

temp=num;
 while(temp != 0)
  {
  temp=temp/10;
  a=a+1;
  }

Above code is, for calculate the number of digits in given/entered number by user. Here temp variable assign the value of entered number by user (temp=num). Every time temp divided by 10 and result store in temp if temp become zero loop will be terminated. And each time value of variable a will be increased by 1 (a=a+1), when loop will be terminated variable a store maximum value which is number of digits.


Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. For Advertisement contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar. Difference between path and classpath in Java

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483 Try this Keyword C++ Programs