Lecture 15 | Armstrong Number in C normal Program | Using While Loop | Vikas Singh
Автор: CoDing SeeKho
Загружено: 2023-09-17
Просмотров: 3455
Описание:
Lecture 15 | Armstrong Number in C normal Program | Using While Loop | Vikas Singh
An Armstrong number (also known as a narcissistic number or pluperfect digital invariant) is a number that is equal to the sum of its own digits raised to the power of the number of digits. Here's a description of an Armstrong number checking program in C using a while loop:
int main() {
int num, originalNum, remainder, result = 0, n = 0;
// Input a number from the user
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
// Count the number of digits in the number
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
// Calculate the Armstrong number
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
// Check if the number is an Armstrong number
if (result == num)
printf("%d is an Armstrong number.\n");
else
printf("%d is not an Armstrong number.\n");
return 0;
}
In this program:
The user is prompted to enter an integer.
The program counts the number of digits in the entered number using a while loop.
It then calculates the sum of each digit raised to the power of the number of digits and checks if this sum is equal to the original number to determine whether it's an Armstrong number.
The result is printed as either "is an Armstrong number" or "is not an Armstrong number."
#CProgramming
#WhileLoop
#ArmstrongNumber
#ProgrammingInC
#CodeSnippet
#ComputerScience
#CodingChallenge
#NumericalProgramming
#Algorithm
#TechSolutions
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: