Computer Science, asked by thanujvenugopal, 1 year ago

write a program to check weather .1)given number is special number or not.2)given number is pronic number or not using scanner class (in java)


QGP: Yeah. Starting work with this one now
QGP: Is it okay if I use functions? Or should I include it all in the public static void main() class ??
QGP: Hey!! Can I use separate functions for checking specific types of numbers??
thanujvenugopal: do it using switch
QGP: Yeah That I am going to use. But, have you studied Functions in JAVA?
thanujvenugopal: yeah
thanujvenugopal: ihave
QGP: So can I use Functions to keep things a bit organised? Or should I make it like the last one: A single main() function containing everything
thanujvenugopal: do it using functions
QGP: Ahh. I didn't read it earlier. I have made it all in a single main() function :(

Answers

Answered by QGP
5
import java.util.Scanner;

public class Special_Pronic
{
    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 Special Number");
            System.out.println("2. Check whether a given number is a Pronic 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;
                    System.out.println("A number is an Special Number, if the sum of factorials of the digits is equal to the original number.\n");
                    System.out.print("Enter a number: ");
                    n = sc.nextLong();
                    System.out.println();
                    
                    long p,d;
                    
                    p=n;
                    
                    long fact=1;
                    long sum=0;
                    while(p!=0)
                    {
                        fact=1;
                        
                        d=p%10;
                        p=p/10;
                        
                        for(int i=1;i<=d;i++)
                        {
                            fact = fact*i;
                        }
                        sum += fact;
                    }
                    
                    
                    if (sum==n)
                        System.out.println(n+" is an Special Number");
                    else
                        System.out.println(n+" is not an Special Number");
                    
                    System.out.println();
                    break;
                }
                case 2:
                {
                    System.out.println("A Pronic Number is a number which is a product of two consecutive integers.\n");
                    System.out.print("Enter a number: ");
                    long n = sc.nextLong();
                    
                    System.out.println();
                    
                    
                    boolean isPronic=false;
                    for(int i=0;i<n;i++)
                    {
                        if(i*(i+1)==n)
                        {
                            isPronic = true;
                            break;
                        }
                    }
                    
                    
                    if(isPronic)
                    {
                        System.out.println(n+" is a Pronic Number");
                    }
                    else
                    {
                        System.out.println(n+" is not a Pronic Number");
                    }
                    System.out.println();
                    break;
                }
                
                case 3:
                {
                    break MainLoop;
                }
                default:
                {
                    System.out.println("Invalid Choice");
                }
            }
        }
        System.out.println("Program ends.");
    }
}

thanujvenugopal: when we post them
QGP: You should be able to see an "Attach" or "Add Photo" kind of option.
thanujvenugopal: ohh
QGP: Are you using App?
thanujvenugopal: yeah
QGP: Then there is a "Paperclip" kind of symbol, when you type your full question. That is the attach option
thanujvenugopal: oh i will try
thanujvenugopal: ok bye then its late
thanujvenugopal: and thanks for following me
QGP: I followed you? I am not on App. I sent a Friend Invitation from website. Didn't know it meant "following"
Similar questions