Given an integer ‘n’, Develop an algorithm and write a java program to check whether it is a Narcissistic number or not.Note: Narcissistic Number is a number that is the sum of its own digits each raised to the power of the number of digits.Boundary Condition: 0 < n <= 10000Otherwise, print ‘invalid’ Input FormatNumber ‘n’Output Formatprint a single line containing the string "yes" if the presented scenario is possible or "no" otherwise (without quotes).
Question
Given an integer ‘n’, Develop an algorithm and write a java program to check whether it is a Narcissistic number or not.Note: Narcissistic Number is a number that is the sum of its own digits each raised to the power of the number of digits.Boundary Condition: 0 < n <= 10000Otherwise, print ‘invalid’ Input FormatNumber ‘n’Output Formatprint a single line containing the string "yes" if the presented scenario is possible or "no" otherwise (without quotes).
Solution
Sure, here is a step by step guide to develop a Java program to check whether a given number is a Narcissistic number or not.
Step 1: Import the necessary libraries. In this case, we only need the Scanner class for user input.
import java.util.Scanner;
Step 2: Define the main class and the main method.
public class Main {
public static void main(String[] args) {
Step 3: Create a Scanner object to get the user input.
Scanner scanner = new Scanner(System.in);
Step 4: Get the input number from the user.
int n = scanner.nextInt();
Step 5: Check the boundary conditions. If the number is not within the range, print 'invalid' and return.
if(n <= 0 || n > 10000) {
System.out.println("invalid");
return;
}
Step 6: Calculate the number of digits in the number.
int num = n;
int digits = 0;
while(num != 0) {
digits++;
num /= 10;
}
Step 7: Check if the number is a Narcissistic number. A number is a Narcissistic number if the sum of its digits each raised to the power of the number of digits is equal to the number itself.
num = n;
int sum = 0;
while(num != 0) {
int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}
Step 8: If the sum is equal to the number, print 'yes'. Otherwise, print 'no'.
if(sum == n) {
System.out.println("yes");
} else {
System.out.println("no");
}
Step 9: Close the main method and the main class.
}
}
That's it! This is a simple Java program to check if a given number is a Narcissistic number or not.
Similar Questions
Help Alex, a new programmer, create a straightforward program. Ask for a positive number, use a function named isDuckNumber() to check if it's a duck number (having at least one zero, not as the first digit), and print the result in the main function.This tool aids Alex in spotting duck numbers, enhancing their programming journey.Note: This question helps in clearing technical coding tests for companies like Amazon and Adobe.Input format :The input consists of an integer n, representing the number to be checked.Output format :The output prints "The number is a duck number." if the entered number is a duck number or "The number is NOT a duck number." otherwise.
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.
Develop an algorithm and write a Java that must accept two integers N1 and N2 as the input. The program must print the desired pattern as shown in the Sample test cases.Input FormatThe first line contains the two integers N1 and N2 as the input.Output FormatPrint the desired pattern as shown in the Sample test cases.Boundary Condition: 0 < N1, N2 <= 1000Otherwise, print ‘invalid’
Design an algoritm and write a Java program with a class that read an integer ‘n’ as input and print the ‘n’ natural numbers in descending order sequence and their “Sum”. Print ‘invalid’ if the boundary conditions are not met.Boundary Condition: 0 < n > 1000Input Format:The first line contains an integer ‘n’Output Format:The first ‘n’ line contains the natural numbers in descending sequence or invalidThe ‘n+1’th line contains the Sum
There are many other types of numbers. Find out what these numbers are and give anexample of each.a Perfect numbers.b Palindromic numbers.c Narcissistic numbers.
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.