×

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

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

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

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

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

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 use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

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.