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
Answers
Answered by
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.");
}
}
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.");
}
}
Similar questions