Hello everyone, im having some problems with a program regarding the Armstrong number. The requirements i have to fill out are:
-Write a program that finds the Armstrong numbers from 0 to a certain number given by the user in the line of commands;
-The program must write these numbers on the screen and in a file at my choice;
-The program must execute in separate functions the following tasks:
i)Receive the number of algarisms of a certain positive integer (which will be the Armstrong number);
ii)Test if a given positive integer is or not an Armstrong number.
I appreciate any help, and here's the program i wrote:
(Don't mind the attached file, i don't if it is working or not so)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int nalgarisms(int p)
{
int n=0;
while(p > 0)
{
p=p/10;
++n;
}
return n;
}
int armstrongtest(int p1)
{
int remainder,result,n,p2,test;
n=0;
result=0;
p2=p1;
while(p2>0)
{
p2=p2/10;
++n;
}
while(p1 > 0)
{
remainder=p1%10;
resul+=pow(remainder,n);
p1=p1/10;
}
if(p1 == result)
{
test=1;
}
else
{
test=0;
}
return test;
}
int main(int argc, char **argv)
{
int x,i,n;
FILE *f1;
f1=fopen("Armstrong.txt","wt");
i=0;
while(i<= argv[1])
{
n=armstrongtest(i);
if(n==1)
{
x=nalgarisms(i);
printf("The number %d is an Armstrong number, and it has %d algarisms.\n",i,x);
fprintf(f1,"%d\n",i);
}
++i;
}
fclose(f1);
return 0;
}