In: Computer Science
Problem 1: More than 2500 years ago, mathematicians got interested in numbers.
Armstrong Numbers: The number 153 has the odd property that 13+53 + 33 = 1 + 125 + 27 = 153. Namely, 153 is equal to the sum of the cubes of its own digits.
Perfect Numbers: A number is said to be perfect if it is the sum of its own divisors (excluding itself). For example, 6 is perfect since 1, 2, and 3 divide evenly into 6 and 1+ 2 + 3 = 6.
Input File
The input is taken from a file named number.in and which a sequence of numbers, one per line, terminated by a line containing the number 0.
Output File
All the numbers read from the input file, are printed one per line followed by a sentence indicating whether the number is or is not Armstrong number and whether it is or is not a perfect number.
Sample Input
153
6
0
Sample Output
153 is an Armstrong number but it is not a perfect number.
6 is not an Armstrong number but it is a perfect number.
#include<stdio.h>
#include<math.h>
//function for Armstrong number
int Armstrong(int number){
int t,reminder,tot=0;
t=number;
while(number>0){
reminder=number%10;
tot=tot+(pow(reminder,3));
number=number/10;
}
if(t==tot)
return 1;
else
return 0;
}
//function for perfect nubmer
int Perfect(int number){
int tot=0,i;
for(i=1;i<number;i++){
if(number%i==0){
tot=tot+i;
}
}
if(tot==number){
return 1;
}
else{
return 0;
}
}
int main(){
//file referencing objects
FILE *file1;
FILE *file2;
//opening both files according to their required
modes
file1 = fopen("number.in", "r"); //opening file1 in
read mode
file2 = fopen("output.txt", "w"); //opening file1 in
write mode
int numArr[1000]; //array creation for storing numbers
from file
int i;
for(i=0;i<1000;i++){
fscanf(file1, "%d", &numArr[i]);
if(numArr[i]==0){
break;
}
int arm =Armstrong(numArr[i]);
int perf =Perfect(numArr[i]);
//writing output in to file2 named
output.txt
if(arm==1 && perf==1)
fprintf(file2,"%d is Armstrong number and Perfect
number\n",numArr[i]);
else if(arm==1 &&
perf==0)
fprintf(file2,"%d is Armstrong but it is not a Perfect
number\n",numArr[i]);
else if(arm==0 &&
perf==1)
fprintf(file2,"%d is not a Armstrong number but it is a Perfect
number\n",numArr[i]);
else
fprintf(file2,"%d is not a Armstrong number and not a Perfect
number\n",numArr[i]);
}
//closing files
fclose(file1);
fclose(file2);
return 0;
}
------------------------------------------------------------------------------------------------------
Note :please maintain .c file , number.in and output.txt 3 files in one folder
if any queries please comment me...