Friday, 22 March 2019

CP: Armstrong no check


Que: Write a program to accept one number from the user and Find if it is Armstrong or not.

Armstrong :
An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself. Hence 153 because 13 + 53 + 33 = 1 + 125 + 27 = 153.

Code:

#include <stdio.h>
#include <math.h>

int main()
{
    int number,temp,remainder, n = 0 ;
    double result=0;

    printf("\nEnter an integer: ");
    scanf("%d", &number);

  temp = number;

    while (temp > 0)
    {
        temp /= 10;
        n++;
    }

    temp = number;

    while (temp != 0)
    {
        remainder = temp%10;
        result =result+ (pow(remainder, n));

        temp /= 10;
    }

    if(result == number)
        printf("\n%d is an Armstrong number.\n", number);
    else
        printf("\n%d is not an Armstrong number.\n", number);

    return 0;
}


Output :








No comments:

Post a Comment

LAB 7 Arduino with Seven Segment Display || Arduino Tutorial || Code and Circuit Diagram || Project

  LAB 7 Arduino with Seven Segment Display || Arduino Tutorial || Code and Circuit Diagram || Project Dear All We will learn how to Connec...