×

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C.

An Armstrong number is a three-digit number that is the sum of its separate digits' cubes.

Armstrong Program In C Using Function

Pseudo Code:-

Take the input as number
last_dig = 0
WHILE number != 0
last_dig ← number % 10
pow ← last_dig * last_dig * last_dig
sum ← sum +pow
divide / 10
END WHILE
IF sum == number
PRINTF armstrong
ELSE
PRINTF not an armstrong
END IF
end procedure

Code: -

#include<stdio.h>
int CheckArmstrong(int num) 
{
  int last_dig = 0;
  int pow = 0;
  int sum = 0;
 int n = num;
while(n!=0)
 {
 last_dig = n % 10;
 power = last_dig *last_dig * last_dig;
  sum =sum+ pow;
    n /= 10;
  }
if(sum == num) 
return 0;
  else return 1;
}
int main()
{
  int num;
printf("Enter number: ");
  scanf("%d",&number);
  if(CheckArmstrong (num) == 0)
  printf("%d is an Armstrong number.\n", num);
  else
  printf("%d is not an Armstrong number.", num);
 return 0;
}

Output: -

Armstrong Program In C Using Function
Armstrong Program In C Using Function

Explanation:-

  • First we have the integer variables ,
  • Then assign the value to the respective variables.
  • After that we have to split all the digits of number of which we want to find the armstrong.
  • Then find the cube of each digit of number.
  • After that store the output to a variable like named sum.
  • Then check if sum is equal to the function created equals to zero then print it is armstrong number

If  the sum is not equal to the function then print that it is not armstrong number.


Related Topics

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.