Thursday, July 9, 2020
Program to Check Armstrong Number In C
Program to Check Armstrong Number In C How To Implement Armstrong Number in C? Back Home Categories Online Courses Mock Interviews Webinars NEW Community Write for Us Categories Artificial Intelligence AI vs Machine Learning vs Deep LearningMachine Learning AlgorithmsArtificial Intelligence TutorialWhat is Deep LearningDeep Learning TutorialInstall TensorFlowDeep Learning with PythonBackpropagationTensorFlow TutorialConvolutional Neural Network TutorialVIEW ALL BI and Visualization What is TableauTableau TutorialTableau Interview QuestionsWhat is InformaticaInformatica Interview QuestionsPower BI TutorialPower BI Interview QuestionsOLTP vs OLAPQlikView TutorialAdvanced Excel Formulas TutorialVIEW ALL Big Data What is HadoopHadoop ArchitectureHadoop TutorialHadoop Interview QuestionsHadoop EcosystemData Science vs Big Data vs Data AnalyticsWhat is Big DataMapReduce TutorialPig TutorialSpark TutorialSpark Interview QuestionsBig Data TutorialHive TutorialVIEW ALL Blockchain Blockchain TutorialWhat is BlockchainHyperledger FabricWhat Is EthereumEthereum TutorialB lockchain ApplicationsSolidity TutorialBlockchain ProgrammingHow Blockchain WorksVIEW ALL Cloud Computing What is AWSAWS TutorialAWS CertificationAzure Interview QuestionsAzure TutorialWhat Is Cloud ComputingWhat Is SalesforceIoT TutorialSalesforce TutorialSalesforce Interview QuestionsVIEW ALL Cyber Security Cloud SecurityWhat is CryptographyNmap TutorialSQL Injection AttacksHow To Install Kali LinuxHow to become an Ethical Hacker?Footprinting in Ethical HackingNetwork Scanning for Ethical HackingARP SpoofingApplication SecurityVIEW ALL Data Science Python Pandas TutorialWhat is Machine LearningMachine Learning TutorialMachine Learning ProjectsMachine Learning Interview QuestionsWhat Is Data ScienceSAS TutorialR TutorialData Science ProjectsHow to become a data scientistData Science Interview QuestionsData Scientist SalaryVIEW ALL Data Warehousing and ETL What is Data WarehouseDimension Table in Data WarehousingData Warehousing Interview QuestionsData warehouse architectureTalend T utorialTalend ETL ToolTalend Interview QuestionsFact Table and its TypesInformatica TransformationsInformatica TutorialVIEW ALL Databases What is MySQLMySQL Data TypesSQL JoinsSQL Data TypesWhat is MongoDBMongoDB Interview QuestionsMySQL TutorialSQL Interview QuestionsSQL CommandsMySQL Interview QuestionsVIEW ALL DevOps What is DevOpsDevOps vs AgileDevOps ToolsDevOps TutorialHow To Become A DevOps EngineerDevOps Interview QuestionsWhat Is DockerDocker TutorialDocker Interview QuestionsWhat Is ChefWhat Is KubernetesKubernetes TutorialVIEW ALL Front End Web Development What is JavaScript â" All You Need To Know About JavaScriptJavaScript TutorialJavaScript Interview QuestionsJavaScript FrameworksAngular TutorialAngular Interview QuestionsWhat is REST API?React TutorialReact vs AngularjQuery TutorialNode TutorialReact Interview QuestionsVIEW ALL Mobile Development Android TutorialAndroid Interview QuestionsAndroid ArchitectureAndroid SQLite DatabaseProgramming aria-current=page>Uncat egorizedHow To Implement Armstrong Num... AWS Global Infrastructure C Programming Tutorial: The Basics you Need to Master C Everything You Need To Know About Basic Structure of a C Program How to Compile C Program in Command Prompt? How to Implement Linear Search in C? How to write C Program to find the Roots of a Quadratic Equation? Everything You Need To Know About Sorting Algorithms In C Fibonacci Series In C : A Quick Start To C Programming How To Reverse Number In C? How To Implement Armstrong Number in C? How To Carry Out Swapping of Two Numbers in C? C Program To Find LCM Of Two Numbers Leap Year Program in C Switch Case In C: Everything You Need To Know Everything You Need To Know About Pointers In C How To Implement Selection Sort in C? How To Write A C Program For Deletion And Insertion? How To Implement Heap Sort In C? How To Implement Bubble Sort In C? Binary Search In C: Everything You Need To Know Binary Search Introduction to C P rogramming-Algorithms What is Objective-C: Why Should You Learn It? How To Implement Static Variable In C? How To Implement Queue in C? How To Implement Circular Queue in C? What is Embedded C programming and how is it different? How To Implement Armstrong Number in C? Last updated on May 07,2020 8.2K Views edureka Bookmark How To Implement Armstrong Number in C? Finding Armstrong numbers using can a tricky task. Hence many interviews or exams feature this problem. In this article we will see how to implement Armstrong Number in C.This article will focus on following pointers,Armstrong Number in CProgram From An ArmstrongNumberArmstrong Number for N digitsArmstrong Number using FunctionsSo let us start with the first topic of this article,Armstrong Number in CAn Armstrong number of a three-digit number is a number in which the sum of the cube of the digits is equal to the number itself.Consider the example:153 is an Armstrong numberSo, 1*1*1+5*5*5+3*3*3=1+125+27=153 Hence 153 is an Armstrong number.Now let us continue with this article on Armstrong Number in C and take a look at how to implement a program for the same,Program From An ArmstrongNumber #include stdio.h int main() { int num, original, rem, sum = 0; //rem is remainder and original is the original number printf(Enter a three-digit Number: ); scanf(%d, num); original = num; while(original != 0) { rem = original%10; sum =sum + rem*rem*rem; original=original/ 10; } if(sum == num) printf(%d is an Armstrong number.,num); else printf(%d is not an Armstrong number.,num); return 0; } OUTPUT:If it is an Armstrong number.If it is not an Armstrong number:In the program shown above, we first declare all the variables that we are going to use in the program. We use a num to hold the number entered by the user. The original variable is used to hold the original number. rem is used to hold the remainder, that is required for calculation. Last we have the sum variable, which is assigned to zero.We first accept a 3-digit number from the user and store it in num. We then assign this to the original. We do this so that we can do changes to the original in the code above and keep the user entered number safe, to use it to compare with the number we get after calculation.Next, we have the while loop. This runs till the value of original is zero. Inside the while loop, there are three steps to perform. First, we get the last digit of the number and store it in rem. This is done by using mod operator on original. Mod operator gives the remainder when used. If we consider the ex ample, 153% 10 will give us 3 and that will be stored in the rem variable.In the next step, we add the cube of the rem variable to sum and assign it to the sum variable. In the last step, we divide the original by 10. This is done because we no longer require the last digit as it is already been used. Since it is an integer value the numbers after decimal points wont be considered. This is repeated until the original is equal to zero. Then it exits while loop.After this, we compare the sum to the num variable containing the user input number inside an if statement. If the sum is equal to num then it is an Armstrong number and the if part will be executed. Otherwise, the else part will be executed.Let us take a look at the other ways of implementing this program,Armstrong Number for N digitsWe can write the same code to find Armstrong number for n digits number with one slight modification.CODE: #include stdio.h #include math.h int main() { int num, original, rem, sum = 0, n = 0 ; printf(Enter a Number: ); scanf(%d, num); original = num; while (original != 0) { original /= 10; ++n; } original = num; while (original != 0) { rem = original%10; sum += pow(rem, n); original /= 10; } if(sum == num) printf(%d is an Armstrong number., num); else printf(%d is not an Armstrong number., num); return 0; } Output:In this program, there is a slight change as compared to the 3-digit Armstrong number detection program. The first change is that: while (original != 0) { original /= 10; ++n; } This is done to count the number of digits of the input. This is done until original is zero. N is incremented till original is zero. This is how we get the total digits.After this, we again assign the number to original before it enters the second loop.Inside the second loop, we cubed the number and added it to sum in the 3-digit code.However, for n digits, we must use math.h function called pow to perform this exercise.sum += pow(rem, n);In this statement, we raise the remainder rem to the of n and add it to the sum variable. These are all the changes we need to make in this program.Let us start with the final bit of this Armstrong number in C,Armstrong Number using FunctionsThe same program can be executed using functions. #include stdio.h #include math.h int armstrongNumberFinder(int n); int main() { int n, flag; printf(Enter a positive integer: ); scanf(%d, n); flag = armstrongNumberFinder(n); if (flag == 1) printf(%d is an Armstrong number., n); else printf(%d is not an Armstrong number.,n); return 0; } int armstrongNumberFinder(int num) { int original, rem, sum = 0, n = 0, flag; original = num; while(original != 0) { original /= 10; ++n; } original = num; while(original != 0) { rem = original%10; sum += pow(rem, n); original /= 10; } if(sum == num) flag = 1; else flag = 0; return flag; } Output:The output is exactly the same but, the structure of the program is changed. Here a function is called to calculate the Armstrong number and the result is returned to the main program. This increases the usability of the function.These are the various type of programs for finding Armstrong number.With this we come to the end of this blog on Armstrong number In C. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you canenroll herefor live online training with 24/7 support and lifetime access.Got a question for us? Mention them in the comments section of this blog and we will get back to you.Recommended blogs for you What are the 7 Principles of Software Testing? Read Article Scrum Board: Everything You Need to Know Read Article How to Install Appium: Step-by-Step Complete Tutorial Read Article How To Implement Armstron g Number in C? Read Article All You Need to Know About Inheritance in C++ Read Article Vol. XXIV â" Edureka Career Watch â" Jan 2020 Read Article Vol. VII â" Edureka Career Watch â" 23rd Feb. 2019 Read Article 7 Habits of Highly Effective DevOps Read Article Career Trends in 2019 A Survey by Edureka Read Article How To Best Implement Radix Sort Program In C? Read Article Top 10 Trending Technologies To Master In 2020 Read Article Vol. XII â" Edureka Career Watch â" 27th Apr. 2019 Read Article Java Exception Handling A Complete Reference to Java Exceptions Read Article How to Develop Android App using Kotlin? Read Article What is Static Member Function in C++? Read Article #IndiaITRepublic â" Top 10 Facts about Wipro Read Article Big Data Engineer Resume Building an Impressive Data Engineer Resume Read Article Vol. XIX â" Edureka Career Watch â" 24th Aug 2019 Read Article What is ITIL ®? One Stop Solution to IT Infrastructure Library Read Article Ui Path Salary: How Much Would You Earn? Read Article Comments 0 Comments Trending Courses Python Certification Training for Data Scienc ...66k Enrolled LearnersWeekend/WeekdayLive Class Reviews 5 (26200)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.