Solution:
isArmstrong.c
#include<stdio.h>
int main()
{
int num;
// Enter number
printf(“Enter the number in the range 100-999: “);
scanf(“%d”,&num);
int val=num;
int arm=0;
do
{
// Take least significant digit and add by cubing it
arm=arm+((num%10)*(num%10)*(num%10));
// Divide the number by 10
num=num/10;
}while(num!=0);
// If the number equals arm
if(val==arm)
// Number is armstrong
printf(“1”);
else
// Number is not armstrong
printf(“0”);
getchar();
getchar();
}
Output:
Enter the number in the range 100-999: 1
1
isArmstrong.m
num=input(‘Enter the number in the range 100-999: ‘);
% Convert number to string
num_str=num2str(num);
tot=0;
for i=1:length(num_str)
% convert each character to number
v=str2num(num_str(i));
% Find the cube
tot=tot+v*v*v;
end
% Check number is armstrong
if(tot==num)
disp(‘1’)
else
disp(‘0’)
end
Output:
Enter the number in the range 100-999: 407
1
Challenges:
1)
AN.c
#include<stdio.h>
int isArmstrong(int num)
{
int val=num;
int arm=0;
do
{
// Take least significant digit and add by cubing it
arm=arm+((num%10)*(num%10)*(num%10));
// Divide the number by 10
num=num/10;
}while(num!=0);
// If the number equals arm
if(val==arm)
// Number is armstrong
return 1;
else
// Number is not armstrong
return 0;
}
int main()
{
int num;
while(true)
{
// Enter number
printf(“Enter the number in the range 100-999: “);
scanf(“%d”,&num);
if(num<100)
break;
if(isArmstrong(num)==1)
// Number is armstrong
printf(“1n”);
else
// Number is not armstrong
printf(“0n”);
};
getchar();
getchar();
}
Output:
Enter the number in the range 100-999: 407
1
Enter the number in the range 100-999: 253
0
Enter the number in the range 100-999: 98
2)
#include<stdio.h>
int isArmstrong(int num)
{
int val=num;
int arm=0;
do
{
// Take least significant digit and add by cubing it
arm=arm+((num%10)*(num%10)*(num%10));
// Divide the number by 10
num=num/10;
}while(num!=0);
// If the number equals arm
if(val==arm)
// Number is armstrong
return 1;
else
// Number is not armstrong
return 0;
}
int main()
{
int num;
num=100;
int i=0;
while(i<5)
{
if(isArmstrong(num)==1)
{
// Number is armstrong
printf(“%dn”,num);;
i++;
}
num++;
};
getchar();
getchar();
}
Output:
153
370
371
407