Write a program to check armstrong number in c?



Write a program to check armstrong number in c?..

Answer / Rebat Singh

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of three. Here's an example C program that checks if a given number is an Armstrong number:

```c
#include <stdio.h>

int calcSum(int n);

int main() {
int num, sum, temp;

printf("Enter a number: ");
scanf("%d", &num);

temp = num;
sum = calcSum(num);

if (sum == temp) {
printf("%d is an Armstrong number.n", num);
} else {
printf("%d is not an Armstrong number.n", num);
}

return 0;
}

int calcSum(int n) {
int sum = 0, rem;

while (n != 0) {
rem = n % 10;
sum += rem * rem * rem;
n /= 10;
}

return sum;
}
```
In this code, the calcSum function calculates the sum of the digits of a given number raised to the power of three. The main function then compares the original number with the calculated sum to determine if it's an Armstrong number.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

implement general tree using link list

1 Answers   Wipro,


void main() { int *ptr; ptr = (int *) 0x400 ; printf("ptr=%d",ptr); } output?

1 Answers  


how to print electricity bill according to following charges first 100 units -1rs per unit for next 200 units-1.50 rs per unit without using conditions

1 Answers  


How can my program discover the complete pathname to the executable from which it was invoked?

1 Answers  


Explain how do you determine the length of a string value that was stored in a variable?

1 Answers  


How do you override a defined macro?

1 Answers  


What is the output of the program given below #include<stdio.h> main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

21 Answers   ADITI, Student, TCS,


What are local and global variables?

3 Answers  


Write a c program using for loop in switch case?

1 Answers   Infosys,


value = 0xabcd; for (loop = 1; (value >> 1) & 1 | loop & 1; loop++) { foo(); if (loop & 1) value >>= 1; } how many times is foo() executed?

6 Answers   Google,


how can i include my own .h file EX:- alex.h like #include<alex.h>, rather than #include"alex.h"

1 Answers  


what will be the output of this program? void main() { int a[]={5,10,15}; int i=0,num; num=a[++i] + ++i +(++i); printf("%d",num); }

6 Answers   Microsoft,


Categories