C program to check Armstrong number in an array | Armstrong number in an array

 Here, in this program we will learn how to check whether the given number in an array is armstrong or not.

C program to check Armstrong number in an array
C program to check Armstrong number in an array


Before this if you don't know how to find a armstrong number learn here.

What is an Armstrong number?

 It  is a number such that the sum of the cubes of its digits is equal to the number itself. For example, 

371 is an Armstrong number since 3*3*3 + 7*7*7 + 1*1*1 = 371.

153 is an Armstrong number  since 1*1*1+5*5*5+3*3*3=153.


Algorithm:-

  1. Start. 
  2. Enter the number of elements to check in an array.
  3.  Declare the integer array.
  4. Take input in an integer array using loop.
  5.  Declare the necessary variables to carry out the program.
  6.  Run the loop from zero till n.
  7.  Inside the loop declare a temp variable which will hold the current value of an array.
  8.  Run another loop to check whether the current number is armstrong or not.
  9.  If the current number is armstrong then increment the counter.
  10.  print out the number of armstrong values.

Program to find the number of armstrong numbers in an array:-

Here, in this program first we are declaring n which is the size of the array and declaring the array of size 50 and initializing i to zero. We are taking the number of elements to be input in the array from the user and after taking n then we are inputting the elements in the array using for loop which runs from zero through n.

After taking the necessary inputs, declare the necessary variables namely, sum, temp which is to store the current element of the array and r to store the remainder and finally the count which will store the final count of the number of armstrong elements in the array.

So to check whether the number is armstrong or not run the for loop for each element and inside the loop store the current element in temp and reset the sum to zero for each element. 
Run while loop until temp is not equal to zero and inside the loop take the remainder of the current element and compute the sum as shown below in the program and then divide the element by 10. Outside the while loop check whether the element stored in the array is equal to the sum if yes then print that element on the output screen and increment the count otherwise continue with the next element present in the array. Outside the for loop print out the count.

 
#include <stdio.h>

int main()
{
    int n, arr[50], i=0;
    
    // Inputting the number of elements to be checked.
    printf("Enter the number of elements: \n");
    scanf("%d", &n);
    
    // Take the input elements in an array.
    printf("Enter the elements of the array: \n");
    
    for(i = 0; i < n; i++)
    {
         scanf("%d",&arr[i]);
    }
        
    //  Declare the necessary variables
    int temp, sum=0, r, count=0;
    
    // Logic to check whether the number is armstrong or not
    for (int j = 0; j < n; j++) {
        
        temp = arr[j]; // storing the current element in the temporary variable
        sum = 0; // reset the sum to zero each time through the loop
        
        // if the current element is less than 9 then continue with the next iteration
        if(temp < 9) {
            continue;
        }
        
        while(temp!=0) {
            r = temp % 10;
            sum = sum + (r * r * r);
            temp = temp / 10;
        }
        
        // if the current element is same as the sum then print the current as armstrong
        // and increment the count otherwise go to the next iteration
        if(arr[j] == sum) {
            printf("\n%d is an armstrong number", arr[j]);
            count++;
        }
    }
    
    // print the count
    printf("\nThere are %d number of armstrong elements in an array", count);


    return 0;
}

Output:-
Enter the number of elements: 
5
Enter the elements of the array: 
153 211 370 371 451

153 is an armstrong number
370 is an armstrong number
371 is an armstrong number
There are 3 number of armstrong elements in an array

Output:-
Enter the number of elements: 
5
Enter the elements of the array: 
23 170 153 43 77

153 is an armstrong number
There are 1 number of armstrong elements in an array

Post a Comment

0 Comments