C Exercises: Check Armstrong and perfect numbers
C Function : Exercise-9 with Solution
Write a program in C to check Armstrong and perfect numbers using the function.
Pictorial Presentation:
Sample Solution:
C Code:
#include <stdio.h>
int checkArmstrong(int n1);
int checkPerfect(int n1);
int main()
{
int n1;
printf("\n\n Function : check Armstrong and perfect numbers :\n");
printf("-----------------------------------------------------\n");
printf(" Input any number: ");
scanf("%d", &n1);
//Calls the isArmstrong() function
if(checkArmstrong(n1))
{
printf(" The %d is an Armstrong number.\n", n1);
}
else
{
printf(" The %d is not an Armstrong number.\n", n1);
}
//Calls the checkPerfect() function
if(checkPerfect(n1))
{
printf(" The %d is a Perfect number.\n\n", n1);
}
else
{
printf(" The %d is not a Perfect number.\n\n", n1);
}
return 0;
}
// Checks whether a three digits number is Armstrong number or not.
//An Armstrong number is an n-digit number that is equal
//to the sum of the n-th powers of its digits.
int checkArmstrong(int n1)
{
int ld, sum, num;
sum = 0;
num = n1;
while(num!=0)
{
ld = num % 10; // find the last digit of the number
sum += ld * ld * ld; //calculate the cube of the last digit and adds to sum
num = num/10;
}
return (n1 == sum);
}
// Checks whether the number is perfect number or not.
//a perfect number is a positive integer that is equal to
//the sum of its positive divisors excluding the number itself
int checkPerfect(int n1)
{
int i, sum, num;
sum = 0;
num = n1;
for(i=1; i<num; i++)
{
/* If i is a divisor of n1 */
if(num%i == 0)
{
sum += i;
}
}
return (n1 == sum);
}
Sample Output:
Function : check armstrong and perfect numbers : ----------------------------------------------------- Input any number: 371 The 371 is an Armstrong number. The 371 is not a Perfect number.
Flowchart:
s
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a program in C to get largest element of an array using the function.
Next: Write a program in C to print all perfect numbers in given range using the function.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
Static variable inside of a function in C
The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().
The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.
The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().
Ref : https://bit.ly/3fOq7XP
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework