Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS151 / KCS251 Programming for Problem Solving - Using C Language
Lab Exercises
8. Write a program to find whether the number is Armstrong number.
/*
File: Prgrm08.c
Author: Aditya Saini
Date: Jan 16, 2021
Description: Program to find whether the number is Armstrong number.
*/
#include <stdio.h>
#include <math.h>
int main (void)
{
int num;
int number_of_digits;
int ArmStrong;
int temp;
//Input number
printf ("Input number: ");
scanf ("%d", &num);
//Count number of digits
temp = num;
number_of_digits = 0;
while (temp != 0)
{
temp = temp / 10;
number_of_digits++;
}
//Finding if the number is Armstrong or not
temp = num;
ArmStrong = 0;
while (temp != 0)
{
ArmStrong = ArmStrong + pow (temp % 10, number_of_digits);
temp = temp / 10;
}
if (num == ArmStrong)
printf ("%d is an Armstrong number.", num);
else
printf ("%d is not an Armstorng number.", num);
};
Output
Input number: 407
407 is an Armstrong number.
No comments:
Post a Comment
Please do not post spam links.