Question

In: Computer Science

Problem 1: More than 2500 years ago, mathematicians got interested in numbers. Armstrong Numbers: The number...

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.

Solutions

Expert Solution

#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...


Related Solutions

Problem 1: More than 2500 years ago, mathematicians got interested in numbers. Armstrong Numbers: The number...
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...
1. Thirty years ago, the mean number of rides that were disabled (broken) for more than...
1. Thirty years ago, the mean number of rides that were disabled (broken) for more than two hours at Disneyland was 10.2 per month. The current CEO, Bob Iger, believes that number has gone down and randomly selects 11 months from the past three years and checks on the number of disabled rides. If the mean has decreased, he will give the members of the maintenance staff a $50K bonus this year. If not, they will all be immediately fired....
Assignment 1 Instructions A. You have 2500 words to utilize B. No more than 10% above...
Assignment 1 Instructions A. You have 2500 words to utilize B. No more than 10% above the maximum nor 10% below the maximum word count C. Use the Harvard referencing system D. The deadline in the 20/09/2018 NO EXTENSIONS AVAILABLE E. Submissions are to be made via the portal and no email submissions will be accepted The Federal Reserve System is the central bank of the United States. It performs five general functions to promote the effective operation of the...
1. A financial manager should be more interested / concerned with the book balance than the...
1. A financial manager should be more interested / concerned with the book balance than the net float and bank cash. True&False 2. ..........refers to the time required to clear a cheque through the banking system. a. Mail float b. Availability float c. Disbursement float 3. Short term marketable securities are usually referred and considered as cash equivalents? True&False 4. The differnce between bank cash and book cash is called float. True&False 5. .................are incurred with a firm runs out...
(1 point) A researcher is interested in whether the number of years of formal education is...
(1 point) A researcher is interested in whether the number of years of formal education is related to a person's decision to never smoke, continue to smoke, or quit smoking cigarettes. The data below represent the smoking status by level of education for residents of the United States 18 years or older from a random sample of 475 residents. Round all numeric answers to four decimal places. Smoking Status Education Level Current Former Never Less than high school 25 21...
(1 point) A researcher is interested in whether the number of years of formal education is...
(1 point) A researcher is interested in whether the number of years of formal education is related to a person's decision to never smoke, continue to smoke, or quit smoking cigarettes. The data below represent the smoking status by level of education for residents of the United States 18 years or older from a random sample of 475 residents. Round all numeric answers to four decimal places. Smoking Status Education Level Current Former Never Less than high school 40 21...
(1 point) A researcher is interested in whether the number of years of formal education is...
(1 point) A researcher is interested in whether the number of years of formal education is related to a person's decision to never smoke, continue to smoke, or quit smoking cigarettes. The data below represent the smoking status by level of education for residents of the United States 18 years or older from a random sample of 350 residents. Round all numeric answers to four decimal places. Smoking Status Education Level Current Former Never Less than high school 14 19...
Management Accounting: It’s more than just crunching numbers. Discuss. (more than 500 words)
Management Accounting: It’s more than just crunching numbers. Discuss. (more than 500 words)
Explain how Brazil was able to reduce the rate of inflation from from more than 2500%to...
Explain how Brazil was able to reduce the rate of inflation from from more than 2500%to less than 10% between 1994 and 1997.
A few years ago, you got married and bought a house with an adjustable rate mortgage...
A few years ago, you got married and bought a house with an adjustable rate mortgage with the following terms: Loan: $240,000 Term: 20 years Initial Rate: 4% Margin: 2% over the Index Rate Lifetime Max: 4.5% The index rate was 2% in year 1, 1.5% in year 2, 4% in year 3, 1% in year 4, and 1% in year 5. a) What is your loan balance at year 5? (5pts) b) What is the effective interest rate is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT