Computer >> Máy Tính >  >> Lập trình >> lập trình C

Kiểm tra số là Armstrong hay không sử dụng C

Vấn đề

Làm cách nào để kiểm tra xem số đã cho có phải là số Armstrong hay không bằng ngôn ngữ lập trình C?

Giải pháp

Số Armstrong là số bằng tổng các hình lập phương của các chữ số của nó.

Cú pháp

pqrs………=pow(p,n)+pow(q,n)+pow(r,n)+……….

Ví dụ:153,371,1634, v.v., là số Armstrong.

153=1*1*1 + 5*5*5 + 3*3*3
   =1+125+27
   =153 (Armstrong number)

Chương trình

#include<stdio.h>
int main(){
   int number,remainder,total=0,temp;
   printf("enter the number=");
   scanf("%d",&number);
   temp=number;
   while(number>0){
      remainder=number%10;
      total=total+(remainder*remainder*remainder);
      number=number/10;
   }
   if(temp==total)
      printf("This number is Armstrong number");
   else
      printf("This number is not Armstrong number");
   return 0;
}

Đầu ra

enter the number=371
This number is Armstrong number
Check: 371=3*3*3 +7*7*7 + 1*1*1
           =27 + 343 +1
           =371
enter the number=53
This number is not Armstrong number

Giải thích

53 = 5*5*5 + 3*3*3
   =125 +27
   = 152 != 53