0% found this document useful (0 votes)
45 views4 pages

This Study Resource Was: Mehar Girdhar 19BIT0113 Java Lab Fat

1. The document describes a Java program to count and display magic numbers in an integer array. It defines magic numbers as numbers that retain their original index after two sorting operations: (i) sorting while ignoring numbers with zeros, and (ii) sorting after removing odd digits from each number with more than one digit. 2. It provides Java code to implement the sorting operations and removing odd digits using packages. The code performs the two sorting operations on a sample array, prints the sorted arrays, and determines the array contains one magic number. 3. The output shows the magic number is 1.

Uploaded by

Majety S Lskshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

This Study Resource Was: Mehar Girdhar 19BIT0113 Java Lab Fat

1. The document describes a Java program to count and display magic numbers in an integer array. It defines magic numbers as numbers that retain their original index after two sorting operations: (i) sorting while ignoring numbers with zeros, and (ii) sorting after removing odd digits from each number with more than one digit. 2. It provides Java code to implement the sorting operations and removing odd digits using packages. The code performs the two sorting operations on a sample array, prints the sorted arrays, and determines the array contains one magic number. 3. The output shows the magic number is 1.

Uploaded by

Majety S Lskshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MEHAR GIRDHAR

19BIT0113
JAVA LAB FAT
QUES 2

Write a java code to count and show the Magic numbers of given array of integer numbers

(i) Sort(ascending) the given array by ignoring the numbers which contains zero’s(Ex: 12030

-number contains two zero’s, so ignore this number) [15 Mark]

(ii) Sort(ascending) the given array after remove the odd digits in each items. If number

contains more than two digits, then only remove odd digits otherwise ignore that particular

number(Ex1- 1235867 - after removed odd digits from 1235867 you will get 256. Ex2- 33 no

m
er as
need to remove any digits from this number) [25 Mark]

co
Magic Number: If any index item has not changed its original inputted index item after

eH w
o.
performed (i) and (ii) operation. rs e
Note: Implement the above operations using package concepts. [5 Mark]
ou urc
Final Output:

Your array contains one magic number


o
aC s

1 is the magic number


vi y re

FUNCTIONS FOR
ed d

SORTING IN ASCENDING ORDER


ar stu

PACKAGE JAVA3
is

package java3;
import java.util.*;
Th

public class sorting {


public void ascending(int [] arr) {

int temp = 0;
sh

//Sort the array in ascending order


for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

This study source was downloaded by 100000805033309 from CourseHero.com on 06-02-2021 23:43:17 GMT -05:00

https://www.coursehero.com/file/84129912/19BIT0113-VL2020210103971-FATpdf/
System.out.println();

//Displaying elements of array after sorting


System.out.println("Elements of array sorted in ascending order:
");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args) {
sorting x = new sorting();
int [] a = {1,5,4,3};
x.ascending(a);
}
}

OUTPUT

m
er as
co
eH w
o.
rs e
ou urc
o
aC s

REMOVING ODD DIGITS FROM NUMBER


vi y re

CODE
package java3;
ed d

import java.util.*;
ar stu

public class odd_digits{

public void odd(int n) {


int result=0;
int digit;
is

int power = 0;
Th

while (n>0)
{
digit =(int) n%10;
if (digit %2 == 0)// if digit is odd
{
sh

result += digit * Math.pow(10,power);


power++;
}
n/=10;
}
System.out.println(result);
}

public static void main(String[] args) {


odd_digits x = new odd_digits();

This study source was downloaded by 100000805033309 from CourseHero.com on 06-02-2021 23:43:17 GMT -05:00

https://www.coursehero.com/file/84129912/19BIT0113-VL2020210103971-FATpdf/
x.odd(123);
}
}

OUTPUT

DELETING ELEMENTS WITH 0

m
package java3;

er as
import java.util.*;
public class zero {

co
public void ignore0(int [] arr) {

eH w
o.
int digit,j=0; rs e
for(int i =0;i<arr.length;i++) {
while(arr[i]>0) {
ou urc
digit = arr[i]%10;
if(digit==0) {
continue;
o

}
aC s

else {
vi y re

arr[j]=arr[i];
}
}
}
ed d

for (int i = 0; i < arr.length; i++) {


ar stu

System.out.print(arr[i] + " ");


}
}

}
is

FINAL CODE
Th

PACKAGE JAVA_DA
CODE
sh

package java_da;
import java.io.*;
import java3.sorting;
import java3.odd_digits;
import java3.zero;

public class Exam{


public static void main(String[] args){

This study source was downloaded by 100000805033309 from CourseHero.com on 06-02-2021 23:43:17 GMT -05:00

https://www.coursehero.com/file/84129912/19BIT0113-VL2020210103971-FATpdf/
int [] a = {1,4,5,45,2,20};
int j=0;
for(int i =0;i<a.length;i++) {
odd_digits x = new odd_digits();
int y=x.odd(a[i]);
a[j]=y;
}
sorting b = new sorting();

b.ascending(a);
zero c = new zero();
c.ignore0(a);
}

m
er as
co
eH w
o.
rs e
ou urc
o
aC s
vi y re
ed d
ar stu
is
Th
sh

This study source was downloaded by 100000805033309 from CourseHero.com on 06-02-2021 23:43:17 GMT -05:00

https://www.coursehero.com/file/84129912/19BIT0113-VL2020210103971-FATpdf/
Powered by TCPDF (www.tcpdf.org)

You might also like