[go: up one dir, main page]

0% found this document useful (0 votes)
6 views5 pages

Untitled Document

Uploaded by

n190455
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)
6 views5 pages

Untitled Document

Uploaded by

n190455
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/ 5

ASSIGNMENT-4

Name:Chameswari
Id No:N190455

1)Write a program to initialize an integer array and print the sum and
average of the array
public class ProgramDemo{
public static void main(String args[])
{
int[] arr={1,2,3,4,5,6,7,8,9,10};
int sum=0;
float average;
for(int i=0;i<arr.length;i++)
{
sum+=arr[i];
}
System.out.println("sum of the array elements is:"+sum);
average=(float)sum/arr.length;
System.out.println("Average of the array elements is:"+average);
}
}
2)Write a program to initialize an integer array and find the maximum and
minimum value of the array
public class ProgramDemo{
public static void main(String args[])
{
int[] arr={5,6,2,8,9,1,7};
int temp;
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr.length;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("The minimum element is:"+arr[0]);
System.out.println("The maximum element is:"+arr[arr.length-1]);
}
}
3)Write a program to initialize an integer array with values and check if
a given number is present in the array or not.if the number is not found
,it will print -1 else it will print the index value of the given number
in the array
import java.util.Scanner;
public class ProgramDemo{
public static void main(String args[])
{
int[] arr={1,4,34,56,7};
Scanner sc=new Scanner(System.in);
System.out.println("enter the number you want to search:");
int key=sc.nextInt();
int index=-1;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==key)
{
index=i;
break;
}
}
System.out.println(index);
}
}
4)Initialize an integer array with ascii values and print the
corresponding character values in a single row
public class ProgramDemo{
public static void main(String args[])
{
int[] arr={65,66,67,68,69};
for(int i=0;i<arr.length;i++)
{
System.out.println((char)arr[i]);
}
}
}
5)Write a program to find the largest 2 numbers and the smallest 2 numbers
in the given array
public class ProgramDemo{
public static void main(String args[])
{
int[] arr={5,6,2,8,9,1,7};
int temp;
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr.length;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("the smallest two numbers in the
array:"+arr[0]+","+arr[1]);
System.out.println("the largest two numbers in the
array:"+arr[arr.length-2]+","+arr[arr.length-1]);
}
}
6)Write a program to initialize an array and print them in a sorted order
public class ProgramDemo{
public static void main(String args[])
{
int[] arr={5,6,2,8,9,1,7};
int temp;
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr.length;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
7)Write a program to remove the duplicate elements in an array and print
the same
public class ProgramDemo {
public static void main(String[] args) {
int[] arr = {12, 34, 12, 45, 67, 89};
int[] temp = new int[arr.length];
int j = 0;

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


if (arr[i]!= arr[i + 1]) {
temp[j++] = arr[i];
}
}
temp[j++] = arr[arr.length - 1];
for (int i = 0; i < j; i++)
{
System.out.print(temp[i] + " ");
}
}
}

You might also like