1. Disarium Number
2. Amicable Pair
3. Unique Number
4. BUZZ number
My only motive is to provide you most simplest and easy way to do coding. Targeted Audience ICSE 9th and 10th Class. More numbers will be added soon.........................
Write a java program to check whether the entered number is a Disarium Number. A number is called a Disarium if sum of the digits powered with its respective positions is equal to original number itself.
For example: 175 is a Disarium number.
175 = 11+72+53 = 1+49+125 = 175
Here in this program we will be using Scanner class to take the input from the user.
public class Disarium
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int num = sc.nextInt();
int temp = num, rem = 0, sum = 0;//to keep original number intact taking temp
String s = Integer.toString(num);
int len = s.length(); //to calculate the length of number converted to string
while(temp>0)
{
rem = temp % 10;
sum = sum + (int)Math.pow(rem,len--);
temp = temp/10;
}
if(sum == num) //to check whether the original number is equal to the sum
System.out.println("Disarium Number.");
else
System.out.println("Not a Disarium Number.");
}
Question 2 : Amicable Pair
Write a java program to check whether the entered pair is a Amicable Pair. Amicable pair are so related that sum of proper divisors of each is equal to other number.
Amicable numbers are:(220,284), (1184,1210), (2620,2924), (5020,5564), etc.
For Example: 220 and 284 are amicable pair.
Divisors of 220 = 1,2,4,5,10,11,20,22,44,55,110
Sum of divisors of 220 = 1+2+4+5+10+11+20+22+44+55+110 = 284
Divisors of 284 = 1,2,4,71,142
Sum of divisors of 284 = 1+2+4+71+142 = 220
Here 284 is not equal to 220. And sum of divisors is equal to other number itself.
import java.util.Scanner;
public class AmicableNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
int sumNum1 = 0, sumNum2 = 0;
for(int i=1;i<num1;i++)
{
if (num1%i==0)
sumNum1 = sumNum1+i;
}
for(int i=1;i<num2;i++)
{
if (num2%i==0)
sumNum2 =sumNum2+i;
}
if ((sumNum1 == num2) && (sumNum2 == num1) && (num1!=num2))
System.out.println("Amicable pair");
else
System.out.println("Not amicable pair");
}
}
Output of the code:
Question 3 : Unique Number
Question 4 : BUZZ Number
No comments:
Post a Comment