×

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

Garbage in C

Garbage in C The C programming language is a perfect fit for embedded systems as it provides low level control, structured programming, and portability. On the contrary, it does not provide...

3 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 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.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.