Single File Programming QuestionProblem StatementArun wants to implement a function to check whether an integer n is an Armstrong number or not using a call-by-reference function. Assist him in completing the program.An Armstrong number is a number that is equal to the sum of its digits each raised to the power of the number of digits.Function Specifications: void checkArmstrong(int *n)Note: This question was asked in companies like Gemini Solution,TCS, Infosys, and Unthinkable Solutions.Input format :The input consists of an integer n.Output format :If n is an Armstrong number, the output prints "Armstrong number".If n is not an Armstrong number, the output prints "Not an Armstrong number".Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 1000Sample test cases :Input 1 :153Output 1 :Armstrong numberInput 2 :29Output 2 :Not an Armstrong numberInput 3 :1Output 3 :Armstrong numberInput 4 :1000Output 4 :Not an Armstrong number
Question
Single File Programming QuestionProblem StatementArun wants to implement a function to check whether an integer n is an Armstrong number or not using a call-by-reference function. Assist him in completing the program.An Armstrong number is a number that is equal to the sum of its digits each raised to the power of the number of digits.Function Specifications: void checkArmstrong(int *n)Note: This question was asked in companies like Gemini Solution,TCS, Infosys, and Unthinkable Solutions.Input format :The input consists of an integer n.Output format :If n is an Armstrong number, the output prints "Armstrong number".If n is not an Armstrong number, the output prints "Not an Armstrong number".Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 1000Sample test cases :Input 1 :153Output 1 :Armstrong numberInput 2 :29Output 2 :Not an Armstrong numberInput 3 :1Output 3 :Armstrong numberInput 4 :1000Output 4 :Not an Armstrong number
Solution
Here is a C++ solution for the problem:
#include <iostream>
#include <cmath>
using namespace std;
void checkArmstrong(int *n) {
int originalNum, remainder, result = 0, count = 0;
originalNum = *n;
// find the number of digits
while (originalNum != 0) {
originalNum /= 10;
++count;
}
originalNum = *n;
while (originalNum != 0) {
remainder = originalNum%10;
result += pow(remainder, count);
originalNum /= 10;
}
if(result == *n)
cout<<"Armstrong number";
else
cout<<"Not an Armstrong number";
}
int main() {
int num;
cin>>num;
checkArmstrong(&num);
return 0;
}
This program first counts the number of digits in the number. Then it calculates the sum of the digits each raised to the power of the number of digits. If the sum is equal to the original number, it is an Armstrong number. Otherwise, it is not.
Similar Questions
Arun wants to implement a function to check whether an integer n is an Armstrong number or not using a call-by-reference function. Assist him in completing the program.An Armstrong number is a number that is equal to the sum of its digits each raised to the power of the number of digits.Function Specifications: void checkArmstrong(int *n)Note: This question was asked in companies like Gemini Solution,TCS, Infosys, and Unthinkable Solutions.Input format :The input consists of an integer n.Output format :If n is an Armstrong number, the output prints "Armstrong number".If n is not an Armstrong number, the output prints "Not an Armstrong number".Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 1000Sample test cases :Input 1 :153Output 1 :Armstrong numberInput 2 :29Output 2 :Not an Armstrong numberInput 3 :1Output 3 :Armstrong numberInput 4 :1000Output 4 :Not an Armstrong numberNote :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10
Armstrong numbers? 6.5 marks Problem Statement Any number n having k digits is an Armstrong number, if the sum of the digits of n each raised to the power k is equal to n. If d1, d2, d3 ... dm be the digits of the number n, then n is an Armstrong number if, d1k + d2k + d3k ... dmk = n You are given a function, int DetectArmstrongNumber(int n); The function takes an integer n as input. You need to implement the function to detect if n is an Armstrong number or not. If n is an Armstrong number then return '1', else return the sum of digits, each digit raised to the power of the number of digits, i.e. d1k + d2k + d3k ... dmk Note: All calculation lie within integral range. Example: Input: 13 Output: 10 Explanation: 12 + 32 = 10, which is not equal to 13, hence 13 is not an Armstrong number. So the output is 10. The custom input format for the above case: 13 (The line represents 'n') Sample input 153 Sample Output 1 The custom input format for the above case: 153 (The line represents 'n') Instructions : This is a template based question, DO NOT write the "main" function. Your code is judged by an automated system, do not write any additional welcome/greeting messages. "Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring. Additional score will be given for writing optimized code both in terms of memory and execution time. Now let's start coding : Language: C (Gcc 11.3) Day Mode Read-only code below . . . 1 int DetectArmstrongNumber(int n); 2 int main() 3 { 4 //Input read from STDIN 5 int result = DetectArmstrongNumber(n); 6 //Value in result printed to STDOUT 7 return 0; 8 } 9 Write your code below . . . 10 int DetectArmstrongNumber(int n) 11 { 12 13 } 14 15
Write a C program to check whether a number is Armstrong number or not.
Write a C program to determine if a given integer is an Armstrong number or not. An Armstrong number (also known as a narcissistic number or pluperfect digital invariant) is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 13+53+33=15313+53+33=153.Design a function called isArmstrong that takes an integer num as input and returns 1 if num is an Armstrong number, and 0 otherwise. Your program should prompt the user to enter an integer, call the isArmstrong function to check if it is an Armstrong number, and display an appropriate message indicating whether the entered number is an Armstrong number or not.inputEnter a number: 153output153 is an Armstrong number.
#include<math.h> int checkArmstrongNumber(int n) { int originalNumber, remainder, result = 0, count = 0; originalNumber = n; // To get the number of digits while (originalNumber != 0) { originalNumber /= 10; ++count; } originalNumber = n; // Check if the number is an Armstrong number while (originalNumber != 0) { remainder = originalNumber % 10; result += pow(remainder, count); originalNumber /= 10; } if (result == n) return 1; else return result; } Input Your Output Expected Output 371 970554640 1
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.