Program to check whether a number is amstrong or not in C

Source Code

// 14.ii. Program to check whether a number is amstrong or not
// Arpan Das - CST 2nd Year (VOCLET)
// 153 = (1^3) + (5^3) + (3^3) = 1 + 125 + 27 = 153

#include <stdio.h>
//#include <conio.h>
#include <math.h>
//#include <stdlib.h>

void main () {
	int num, num2, num3, temp=0, digits=0;
	//system("cls");
	//clrscr();

	printf("\t\t..:: check whether a number is amstrong or not ::..\n\n");
	printf("pleae enter any number: ");
	scanf("%d", &num);
	num3 = num2 = num;

	while (num3 != 0)	{
		num3 /= 10;
		digits++;
	}

	while (num2 != 0)	{
		//temp += (int)(pow(num2%10, digits));
		temp += (int)(pow(num2%10, digits) + 0.5);
		num2 /= 10;
	}

	if (temp == num)
		printf("%d is an amstrong number.", num);
	else
		printf("%d is not an amstrong number.", num);

	//getch();
}

Output

		..:: check whether a number is amstrong or not ::..

pleae enter any number: 199
199 is not an amstrong number.

		..:: check whether a number is amstrong or not ::..

pleae enter any number: 153
153 is an amstrong number.

Leave a Reply

Note: Your email address will not be published. Required fields are marked *