Computer Science, asked by thanujvenugopal, 1 year ago

write a menu driven program to accept a number from the user and check it is an amstrong number or a niven number (in java)


thanujvenugopal: it should be done using scanner class

Answers

Answered by QGP
9
import java.util.Scanner;

public class Armstrong_Niven
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        MainLoop: while (true)
        {

            System.out.println("---------MENU---------");
            System.out.println("1. Check whether a given number is an Armstrong Number");
            System.out.println("2. Check whether a given number is a Niven Number");
            System.out.println("3. Exit");

            System.out.print("\nEnter your choice: ");
            int ch = sc.nextInt();
            System.out.println();
            switch (ch)
            {
                case 1:
                {
                    long n,sum,d,p;
                    System.out.println("A number is an Armstrong number, if the sum of cubes of the digits is equal to the original number.\n");
                    System.out.print("Enter a number: ");
                    n = sc.nextLong();
                    System.out.println();
                    p=n;
                    sum=0;

                    do
                    {
                        d=n%10;
                        sum+=d*d*d;
                        n=n/10;
                    }
                    while(n!=0);

                    if (sum==p)
                        System.out.println(p+" is an Armstrong number");
                    else
                        System.out.println(p+" is not an Armstrong number");
                   
                    System.out.println();
                    break;
                }
                case 2:
                {
                    System.out.println("A Niven Number is a number which is divisible by the sum of its digits.\n");
                    System.out.print("Enter a number: ");
                    long n = sc.nextLong();
                    System.out.println();
                   
                    long m=n;
                    long d=0;
                    while(m!=0)
                    {
                        d += m%10;
                        m = m/10;
                    }
                    if(n%d==0)
                    {
                        System.out.println(n+" is a Niven Number");
                    }
                    else
                    {
                        System.out.println(n+" is not a Niven Number");
                    }
                    System.out.println();
                    break;
                }
               
                case 3:
                {
                    break MainLoop;
                }
                default:
                {
                    System.out.println("Invalid Choice");
                }
            }
        }
        System.out.println("Program ends.");
    }
}


QGP: ICSE Board??
thanujvenugopal: yeah
thanujvenugopal: where are you from??
QGP: Thought so.. Only they ask quality questions like this. My state board asks stupid questions like "Write program to convert a string to Uppercase" , which are hardly 7-8 lines
QGP: Gujarat State Friend
QGP: I was in Gujarat State Board
thanujvenugopal: oh
thanujvenugopal: i am from kerala
thanujvenugopal: nice to meet you
QGP: Nice to meet you too. Now let me start work on that other question :) I have posted a comment there. Please respond to my doubt there
Answered by varshneysamyakoxg8tj
7
import java.util.*;

class eg
{

public static void main(String args[])

{
int n,s=0,r,n1,ch;

Scanner s=new Scanner(System.in);
ch=s.nextInt();
switch(ch)
{
case 1:
n=s.nextInt();
n1=n
while(n!=0)
{

r=n%10;
s=s+Math.pow(r,3);
n=n/10;

}
if(s==n1)
System.out.println("amstrong");
else
System.out.println("not amstrong");
break;
case 2:
//for niven
n=s.nextInt();
n1=n;
while(n!=0)
{
r=n%10;
s+=r;
n=n/10;

}
if(n1%s==0)
System.out.println("niven number");
else
System.out.println("not a niven number");
break;
default:
System.out.println("invalid choice");
}
}

thanujvenugopal: ☺☺
varshneysamyakoxg8tj: this is some short code i have just written
QGP: Hmm Fine. It will work :)
thanujvenugopal: hmm
varshneysamyakoxg8tj: add this answer as brainliest
varshneysamyakoxg8tj: if any code required in c,c++,java,python message me
QGP: Wow! I need to learn Python and C++ now :))
QGP: You know so many languages brother!
thanujvenugopal: one of my relative is great he knows most of the languages
thanujvenugopal: but he is un available right now and i am in trouble
Similar questions