NUMBER PROBLEMS3

In this time, we see the programs like armstrong number,binary to decimal,decimal to binary,fibonacci series,greatest common divisor, least common divisor,neon number,perfect number,spy number, sum of digits.

ARMSTRONG NUMBEER

It is the number with sum of each digits multiplied itself three times and its equal to the given number.

Ex:

153

3–> 3*3*3=27

5–>5*5*5=125

1–>1*1*1=1

total=27+125+1=153

package mars1;

public class Armstrong {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=153,rem=0,sum=0,armstrong=0,no2=no;

while(no>0)

{

rem=no%10;

armstrong=armstrong+(rem*rem*rem);

no=no/10;

}

if(no2==armstrong)

{

System.out.println(“ARM”);

}

}

}

RESULT

ARM

BINARY TO DECIMAL

It converts given binary number to decimal.

Ex:1001

1–>1*2^0=1

0–>1*2^1=0

0–>1*2^2=0

1–>1*2^3=8

total=1+0+0+8=9

package mars1;

public class BinaryToDecimal {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=1001,power=0,dec=0, rem=0;

while(no>0) {

rem=no%10;

dec=(int)(dec+(rem*Math.pow(2,power)));

no=no/10;

power++;

}

System.out.println(dec);

}

}

RESULT

9

DECIMAL TO BINARY

It converts decimal number into binary.

Ex:no=4

rem=no%2,no=no/2;

rem=4%2=0,no=4/2=2;

rem=2%2=0,no=2/2=1;

rem=1%2=1

package mars1;

public class DecimalToBinary {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=4;

String rem=””;

while(no>0)

{

rem=no%2+rem;

no=no/2;

}

System.out.println(rem+””);

}

}

RESULT

100

FIBONACCI SERIES

Fibonacci series moving their series from left to right by adding the first and second digit to form the third digits and neglect the first digit. The new first digit is the second one and second digit is the third one.

Ex:011

Here below F is first,S is second,T is third

F—S—T

0—1—1

1—2—3

2—3—5…,

package mars1;

public class FibonacciProblems {

public static void main(String[] args) {

// TODO Auto-generated method stub

int first=0,second=1,count=0,third=1;

while(true)

{

first=second;

second=third;

third=first+second;

count++;

if(third==233) {

System.out.println(“I got”);

break;

}

else if(third>223){

System.out.println(“not get”);

}

}

}

}

RESULT

I got

FIBONACCI SERIES USING FOR LOOP

Same concept used in above forloop used instead of while.

package mars1;

public class FibonacciSeries {

public static void main(String[] args) {

// TODO Auto-generated method stub

int first=0,second=1;

for(int i=2;i<=10;i++)

{

int third=first+second;

first=second;

second=third;

System.out.println(third);

}

}

}

RESULT

1

2

3

5

8

13

21

34

55

FIBONACCI SERIES WITHOUT USING THIRD VARIABLE

In this program we eliminate the third variable and use

first=second

second=first+second

package mars1;

public class FibonacciSerieswithoutThirdVariable {

public static void main(String[] args) {

// TODO Auto-generated method stub

int first=0,second=1;

for(int i=2;i<=10;i++)

{

first=second;

second=first+second;

System.out.println(second);

}

}

}

RESULT

2

4

8

16

32

64

128

256

512

GREATEST COMMON DIVISOR

In greatest common divisor

30–>15,10,6 ,5,3,2

18–>18,9,6 ,3,2

both have greatest common is 6.

package mars1;

public class GreatesCommonDivisor {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no1 = 30, no2 = 18;

int small = no1 < no2 ? no1 : no2;

int big = no1 > no2 ? no1 : no2;

while (small >= 2) {

if ((no1 % small == 0) && (no2 % small == 0)) {

System.out.println(“GCD IS ” + small);

break;

}

small–;

}

}

}

RESULT

GCD IS 6

LEAST COMMON MULTIPLE

example:

multiples of 2 and 3.

2–>2,4,6,8,12,14,16,18,20

3–>3,6,9,12,15,18,21

both have least common is 6.

package mars1;

public class LeastCommonMultiple {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no1 = 8, no2 = 3;

int small = no1 < no2 ? no1 : no2;

int big = no1 > no2 ? no1 : no2;

int bigcount=big;

while (true) {

if (big % small == 0) {

System.out.println(“LCM IS ” + big);

break;

}

big=big+bigcount;

}

}

}

RESULT

LCM IS 24

NEON NUMBER

The example of neon number given below.

9*9=81

8+1=9

the product of given number twice produce a number have its sum of digits equal to given number.

package mars1;

public class NeonNumber {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=9,neon=0,rem=0;

int no2=no*no;

while(no2>0) {

rem=no2%10;

neon=neon+rem;

no2=no2/10;

}

if(neon==no) {

System.out.println(“neon”);

}

else {

System.out.println(“not neon”);

}

}

}

RESULT

neon

PERFECT NUMBER

It is number of perfect divisor equal to the given number.

Ex:6

1,2,3,4,5,6 only 1,2,3 exactly divide number 6 so add the number of exact divisors 1+2+3=6.

package mars1;

public class PerfectNumber {

public static void main(String[] args) {

// TODO Auto-generated method stub

int sum=0;

int no=8;

for(int i=1;i<no;i++)

{

if (no%i==0) {

sum=sum+i;

}

}

if(sum==no) {

System.out.println(“PERFECT NUMBER”);

}

else {

System.out.println(“NOT PERFECT NUMBER”);

}

}

}

RESULT

NOT PERFECT NUMBER

SPY NUMBER

spy number means the sum of digits and product of digits of a give number to be equal.

package mars1;

public class SpyNo {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=1234,sum=0,prod=1;

while(no>0)

{

int rem=no%10;

sum=sum+rem;

prod=prod*rem;

no=no/10;

}

System.out.println(“SUM ”+sum);

System.out.println(“PROD “+prod);

if(sum==prod) {

System.out.println(“spy number”);

}

else {

System.out.println(“not spy number”);

}

}

}

RESULT

SUM 10

PROD 24

not spy number

SUM OF DIGITS

In this program we need sum of digits in a single digit number so we use do while condition and the range is below 9 will give single digit. so, we choose it.

package mars1;

public class SumOfDigits {

public static void main(String[] args) {

// TODO Auto-generated method stub

int no=123,sum=0;

do {

while(no>0)

{

int rem=no%10;

sum=sum+rem;

no=no/10;

}

no=sum;

}while(sum>9);

System.out.println(sum);

}

}

RESULT

6

Leave a comment

Design a site like this with WordPress.com
Get started