File tree Expand file tree Collapse file tree
src/number_Programming/medium/day_05 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package number_Programming .medium .day_05 ;
2+
3+ import java .util .Scanner ;
4+
5+ public class BinaryToDecimalRepresentation
6+ {
7+ public static void decimal (int binary )
8+ {
9+ int temp = binary ;
10+ int decimal = 0 ;
11+ int power = 0 ;
12+ if (binary == 0 )
13+ {
14+ System .out .println ("The decimal form of " + 0 + " is " + 0 );
15+ }
16+ else
17+ {
18+ while (binary >0 )
19+ {
20+ int ld = binary %10 ;
21+ if (ld == 1 )
22+ {
23+ int expo = 1 ;
24+ for (int i = 1 ;i <=power ;i ++)
25+ {
26+ expo = expo * 2 ;
27+ }
28+ decimal = decimal + expo ;
29+ }
30+ power ++;
31+ binary = binary /10 ;
32+ }
33+ System .out .println ("The number representation of " + temp + " is " + decimal );
34+ }
35+ }
36+ public static void main (String [] args ) {
37+ Scanner sc = new Scanner (System .in );
38+ System .out .println ("Enter a binary number : " );
39+ int binary = sc .nextInt ();
40+ decimal (binary );
41+ sc .close ();
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ package number_Programming .medium .day_05 ;
2+
3+ import java .util .Scanner ;
4+
5+ public class CoPrimeNumbers
6+ {
7+ public static int gcd (int a , int b )
8+ {
9+ int gcd = 1 ;
10+ for (int i = 1 ; i <=a && i <=b ;i ++)
11+ {
12+ if (a %i == 0 && b % i == 0 )
13+ gcd = i ;
14+ }
15+ return gcd ;
16+ }
17+ public static void coprime (int num1 , int num2 )
18+ {
19+ int result = gcd (num1 , num2 );
20+ if (result == 1 )
21+ System .out .println ("Numbers are co-prime" );
22+ else
23+ System .out .println ("Numbers are not co-prime" );
24+ }
25+ public static void main (String [] args )
26+ {
27+ Scanner sc = new Scanner (System .in );
28+ System .out .println ("Enter first number : " );
29+ int num1 = sc .nextInt ();
30+ System .out .println ("Enter second number : " );
31+ int num2 = sc .nextInt ();
32+ coprime (num1 ,num2 );
33+ sc .close ();
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ package number_Programming .medium .day_05 ;
2+
3+ import java .util .Scanner ;
4+
5+ public class DemicalToBinaryRepresentation
6+ {
7+ public static void binaray (int num )
8+ {
9+ int temp = num ;
10+ int binary = 0 ;
11+ int place = 1 ;
12+ while (num >0 )
13+ {
14+ int rem = num %2 ;
15+ binary = binary +(rem *place );
16+ place = place * 10 ;
17+ num = num /2 ;
18+ }
19+ System .out .println ("The binary form of " + temp + " is " + binary );
20+ }
21+ public static void main (String [] args ) {
22+ Scanner sc = new Scanner (System .in );
23+ System .out .println ("Enter a number : " );
24+ int num = sc .nextInt ();
25+ binaray (num );
26+ sc .close ();
27+ }
28+
29+ }
Original file line number Diff line number Diff line change 1+ package number_Programming .medium .day_05 ;
2+
3+ import java .util .Scanner ;
4+
5+ public class GCDOrHCF
6+ {
7+ public static void hcf (int n1 ,int n2 )
8+ {
9+ int range = 0 ;
10+ if (n1 <=n2 )
11+ range = n1 ;
12+ else
13+ range = n2 ;
14+ int hcf =0 ;
15+ for (int i = range ;i >=1 ;i --)
16+ {
17+ if (n1 %i ==0 && n2 % i ==0 )
18+ {
19+ hcf =i ;
20+ break ;
21+ }
22+ }
23+ System .out .println ("The highest common factor of " + n1 + " and " + n2 + " is " + hcf );
24+ }
25+ public static void main (String [] args ) {
26+ Scanner sc = new Scanner (System .in );
27+ System .out .println ("Enter first number : " );
28+ int n1 = sc .nextInt ();
29+ System .out .println ("Enter second number : " );
30+ int n2 = sc .nextInt ();
31+ hcf (n1 , n2 );
32+ sc .close ();
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ package number_Programming .medium .day_05 ;
2+
3+ import java .util .Scanner ;
4+
5+ public class LCM
6+ {
7+ public static void lcm (int n1 ,int n2 )
8+ {
9+ int range = 0 ;
10+ if (n1 <=n2 )
11+ range = n1 ;
12+ else
13+ range = n2 ;
14+ int hcf =0 ;
15+ for (int i = 1 ;i <=range ;i ++)
16+ {
17+ if (n1 %i ==0 && n2 %i ==0 )
18+ {
19+ hcf = i ;
20+ }
21+ }
22+ int lcm = (n1 *n2 )/hcf ;
23+ System .out .println ("The lcm of " + n1 + " and " + n2 + " is " + lcm );
24+ }
25+ public static void main (String [] args ) {
26+ Scanner sc = new Scanner (System .in );
27+ System .out .println ("Enter num1: " );
28+ int n1 = sc .nextInt ();
29+ System .out .println ("Enter num2 : " );
30+ int n2 = sc .nextInt ();
31+ lcm (n1 , n2 );
32+ sc .close ();
33+ }
34+
35+ }
Original file line number Diff line number Diff line change 1+ package number_Programming .medium .day_05 ;
2+
3+ import java .util .Scanner ;
4+
5+ public class NthLargestDigit
6+ {
7+ public static int largest (int num )
8+ {
9+ int large = 0 ;
10+ int temp = num ;
11+ while (num > 0 )
12+ {
13+ int ld = num % 10 ;
14+ if (ld > large )
15+ {
16+ large = ld ;
17+ }
18+ num = num /10 ;
19+ }
20+ return large ;
21+ }
22+ public static int removeDigit (int num , int digitToRemove )
23+ {
24+ int rev = 0 ;
25+ while (num > 0 )
26+ {
27+ int ld = num % 10 ;
28+ if (ld != digitToRemove )
29+ {
30+ rev = rev * 10 + ld ;
31+ }
32+ num = num /10 ;
33+ }
34+ int result = 0 ;
35+ while (rev >0 )
36+ {
37+ int d = rev %10 ;
38+ result = result * 10 +d ;
39+ rev = rev /10 ;
40+ }
41+ return result ;
42+ }
43+ public static int nthLargestNumber (int num ,int n )
44+ {
45+ if (n <=0 )
46+ return -1 ;
47+ int temp = num ;
48+ for (int k =1 ;k <=n ;k ++)
49+ {
50+ if (temp == 0 )
51+ {
52+ return -1 ;
53+ }
54+ int largest = largest (temp );
55+ if (k == n )
56+ {
57+ return largest ;
58+ }
59+ temp = removeDigit (temp ,largest );
60+ }
61+ return -1 ;
62+ }
63+ public static void main (String [] args )
64+ {
65+ Scanner sc = new Scanner (System .in );
66+ System .out .println ("Enter a number : " );
67+ int num = sc .nextInt ();
68+ System .out .println ("Enter nth number: " );
69+ int nth = sc .nextInt ();
70+ int result = nthLargestNumber (num , nth );
71+ if (result == -1 )
72+ {
73+ System .out .println ("Invalid number " + nth + "th largest digit in " +num );
74+ }
75+ else
76+ {
77+ System .out .println (nth + "th largest digit in " +num + " is " + result );
78+ }
79+ sc .close ();
80+ }
81+
82+ }
Original file line number Diff line number Diff line change 1+ package number_Programming .medium .day_05 ;
2+
3+ import java .util .Scanner ;
4+
5+ public class NthSmallestDigit
6+ {
7+ public static int Smallest (int num )
8+ {
9+ int smallest = 9 ;
10+ int temp = num ;
11+ while (num >0 )
12+ {
13+ int ld = num % 10 ;
14+ if (ld < smallest )
15+ {
16+ smallest = ld ;
17+ }
18+ num = num / 10 ;
19+ }
20+ return smallest ;
21+ }
22+ public static int removeDigit (int num , int digitToRemove )
23+ {
24+ int rev = 0 ;
25+ while (num > 0 )
26+ {
27+ int ld = num % 10 ;
28+ if (ld != digitToRemove )
29+ {
30+ rev = rev * 10 + ld ;
31+ }
32+ num = num / 10 ;
33+ }
34+ int result = 0 ;
35+ while (rev >0 )
36+ {
37+ result = result *10 +(rev %10 );
38+ rev = rev / 10 ;
39+ }
40+ return result ;
41+ }
42+ public static int nthSmallestDigit (int num , int n )
43+ {
44+ if (num <=0 )
45+ return -1 ;
46+ int temp = num ;
47+ for (int k = 1 ;k <=n ;k ++)
48+ {
49+ if (temp == 0 )
50+ {
51+ return -1 ;
52+ }
53+ int smallest = Smallest (temp );
54+ if (k == n )
55+ {
56+ return smallest ;
57+ }
58+ temp = removeDigit (temp , smallest );
59+ }
60+ return -1 ;
61+ }
62+ public static void main (String [] args ) {
63+ Scanner sc = new Scanner (System .in );
64+ System .out .println ("Enter a number : " );
65+ int num = sc .nextInt ();
66+ System .out .println ("Enter nth number : " );
67+ int n = sc .nextInt ();
68+ int result = nthSmallestDigit (num , n );
69+ if (result == -1 )
70+ {
71+ System .out .println ("Invalid number " + n + " is not avaialable in the number" );
72+ }
73+ else
74+ {
75+ System .out .println ("The smallest digit in the number is " + n );
76+
77+ }
78+ sc .close ();
79+ }
80+ }
Original file line number Diff line number Diff line change 1+ package number_Programming .medium .day_05 ;
2+
3+ import java .util .Scanner ;
4+
5+ public class SumOfCommonDigits
6+ {
7+ public static int sumOfCommonDigits (int num1 , int num2 ) {
8+ int sum = 0 ;
9+
10+ while (num1 > 0 ) {
11+ int digit1 = num1 % 10 ;
12+ int temp = num2 ;
13+
14+ while (temp > 0 ) {
15+ int digit2 = temp % 10 ;
16+
17+ if (digit1 == digit2 ) {
18+ sum = sum + digit1 ;
19+ break ;
20+ }
21+
22+ temp = temp / 10 ;
23+ }
24+ num1 = num1 / 10 ;
25+ }
26+
27+ return sum ;
28+ }
29+
30+ public static void main (String [] args ) {
31+ Scanner sc = new Scanner (System .in );
32+ System .out .println ("Enter first number : " );
33+ int num1 = sc .nextInt ();
34+ System .out .println ("Enter second number : " );
35+ int num2 = sc .nextInt ();
36+ int result = sumOfCommonDigits (num1 , num2 );
37+ System .out .println ("Sum of common digits: " + result );
38+ sc .close ();
39+ }
40+
41+ }
You can’t perform that action at this time.
0 commit comments