Sunday, June 8, 2025
News PouroverAI
Visit PourOver.AI
No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
News PouroverAI
No Result
View All Result

Armstrong Number in C : Examples and Programs

April 26, 2024
in AI Technology
Reading Time: 4 mins read
0 0
A A
0
Share on FacebookShare on Twitter


What is Armstrong Number in C?

An Armstrong number (also known as a narcissistic number, plenary number, or pluperfect number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because:

It’s named after Michael F. Armstrong, who used it as an example in a programming problem in 1969. These numbers are intriguing in mathematics and programming because they are relatively rare and often used in exercises to test programming skills.

Armstrong Number Example

If the Armstrong number is a positive integer of order n then it can be defined as,

abcde…. = pow (a, n) + pow (b, n) + pow (c, n) + pow (d, n) + pow (e, n) + ………

For example : 0, 1, 153, 370, 371, 1634 etc.

Let’s check whether 370 is an Armstrong number or not

370 = (3 * 3 * 3) + (7 * 7 * 7) + (0 * 0 * 0)

Here,

(3 * 3 * 3) = 27

(7 * 7 * 7) = 343

(0 * 0 * 0) = 0

27 + 343 + 0 = 370, which is equal to the given number; hence it is an Armstrong number.

Let’s check four digits Armstrong number:

1634 = (1 * 1 * 1 * 1) + (6 * 6 * 6 * 6) + (3 * 3 * 3 * 3) + (4 * 4 * 4 * 4)

Here,

(1 * 1 * 1 * 1) = 1

(6 * 6* 6 * 6) = 1296

(3 * 3 * 3 * 3) = 81

(4 * 4 * 4 * 4) = 256

1 + 1296 + 81 + 256 = 1634, which is equal to the given number; hence it is an Armstrong number.

Algorithm of Armstrong Number in C

Take input from the user

Initialize sum = 0 and take temporary variable to temporarily store user input (var = num)

Now find out the total number of digits in the given number

Total number of digits get stored in a

Repeat the loop till var > 0

Store the output of while loop in sum

Check whether the user number is equal to sum or not

If it is equal than print “It is an Armstrong number”

Else print “It is not an Armstrong number”

Program of Armstrong Number in C

#include
#include

int main ()
{
int num, var, rem, sum = 0, a = 0 ;

printf ( “ Please enter an integer: “ ); // Taking the user input
scanf ( “%d”, &num );

var = num;

while (var != 0) // Finding the numbers of digits in a given number
{
var = var / 10;
++a;
}

var = num;

while (var > 0 ) // Calculate the number to check it is Armstrong or not
{
rem = var % 10;
sum = sum + pow( rem, a );
var = var / 10;
}

if ( sum == num ) // Check whether the sum is equal to the given number of not
{
printf ( “ %d is an Armstrong number n ”, num );
}
else
{
printf ( “ %d is not an Armstrong number n ”, num );
}
return 0;
}

Output 1:

Please enter an integer: 371

371 is an Armstrong number

Output 2:

Please enter an integer: 1045

1045 is an Armstrong number

Explanation

In the above code, we have first declared all the variables that are needed in the program. The num is declared to hold the user input. The var is used for the temporary storage, and then rem is used to hold the remainder that is required for calculation. At last, we have the sum and a, which are assigned to zero.

We are accepting user input and then assigning that number to num. Then we assign this number to var so that we can make our calculation and keep the user input safe in the num variable.

Now we are calculating the number of digits that user input has. For that, we have assigned num value to var, and we have taken a while loop. The while loop will be running until (var !=0) and then divide var by ten so that we can count the total number of digits in that number. Since var is of integer type, it will not store a decimal number, and then each time, the value of an increases by 1. This process is repeated until var is equal to zero. After that, it exits the loop.

Now again, we assign num value to var. Then we start the loop, and this loop will run till var is greater than zero. We have three steps to perform inside the loop. First, we find the remainder of the number, and then we calculate the power of the remainder using pow(rem, a), where rem represents the remainder, and a represents the power, i.e., total number of digits in a number, which was calculated above. Now we divide the var by 10, and this is done because we don’t require the last digit of the number because we have already used it. Since var is an integer type, it neglects the decimal value and stores the integer value. This process is repeated till var is greater than zero; after that, it exits from the loop, and the final value is stored in sum.

Once the control moves out of the loop, the if statement checks whether sum is equal to the num or not, where the num is the input given by the user. If the sum is equal to num, then it is an Armstrong number. Else it is not an Armstrong number.

So, here is all about “Armstrong Number in C.” We hope you find this implementation informative. Stay tuned for more upcoming blogs. Take up the Free Online C Programming for Beginners Course and learn more such concepts!



Source link

Tags: ArmstrongExamplesNumberPrograms
Previous Post

Meta Opens Up Horizon OS to Third-Party XR Headsets

Next Post

Synthesia Reveals First AI Avatar with Human Expressions

Related Posts

How insurance companies can use synthetic data to fight bias
AI Technology

How insurance companies can use synthetic data to fight bias

June 10, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset
AI Technology

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
Decoding Decoder-Only Transformers: Insights from Google DeepMind’s Paper
AI Technology

Decoding Decoder-Only Transformers: Insights from Google DeepMind’s Paper

June 9, 2024
How Game Theory Can Make AI More Reliable
AI Technology

How Game Theory Can Make AI More Reliable

June 9, 2024
Buffer of Thoughts (BoT): A Novel Thought-Augmented Reasoning AI Approach for Enhancing Accuracy, Efficiency, and Robustness of LLMs
AI Technology

Buffer of Thoughts (BoT): A Novel Thought-Augmented Reasoning AI Approach for Enhancing Accuracy, Efficiency, and Robustness of LLMs

June 9, 2024
Deciphering Doubt: Navigating Uncertainty in LLM Responses
AI Technology

Deciphering Doubt: Navigating Uncertainty in LLM Responses

June 9, 2024
Next Post
Synthesia Reveals First AI Avatar with Human Expressions

Synthesia Reveals First AI Avatar with Human Expressions

Bigger isn’t always better: How hybrid AI pattern enables smaller language models

Bigger isn’t always better: How hybrid AI pattern enables smaller language models

Top 5 Generative AI Use Cases Transforming the FinTech Industry

Top 5 Generative AI Use Cases Transforming the FinTech Industry

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
23 Plagiarism Facts and Statistics to Analyze Latest Trends

23 Plagiarism Facts and Statistics to Analyze Latest Trends

June 4, 2024
Accenture creates a regulatory document authoring solution using AWS generative AI services

Accenture creates a regulatory document authoring solution using AWS generative AI services

February 6, 2024
Managing PDFs in Node.js with pdf-lib

Managing PDFs in Node.js with pdf-lib

November 16, 2023
Graph neural networks in TensorFlow – Google Research Blog

Graph neural networks in TensorFlow – Google Research Blog

February 6, 2024
13 Best Books, Courses and Communities for Learning React — SitePoint

13 Best Books, Courses and Communities for Learning React — SitePoint

February 4, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

June 10, 2024
AI Compared: Which Assistant Is the Best?

AI Compared: Which Assistant Is the Best?

June 10, 2024
How insurance companies can use synthetic data to fight bias

How insurance companies can use synthetic data to fight bias

June 10, 2024
5 SLA metrics you should be monitoring

5 SLA metrics you should be monitoring

June 10, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

June 10, 2024
Facebook Twitter LinkedIn Pinterest RSS
News PouroverAI

The latest news and updates about the AI Technology and Latest Tech Updates around the world... PouroverAI keeps you in the loop.

CATEGORIES

  • AI Technology
  • Automation
  • Blockchain
  • Business
  • Cloud & Programming
  • Data Science & ML
  • Digital Marketing
  • Front-Tech
  • Uncategorized

SITEMAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2023 PouroverAI News.
PouroverAI News

No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing

Copyright © 2023 PouroverAI News.
PouroverAI News

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In