0% found this document useful (0 votes)
18 views

Array Programs - Computers

Uploaded by

mespoydo
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)
18 views

Array Programs - Computers

Uploaded by

mespoydo
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/ 14

1 Write a program to accept 5 numbers in a Single Dimensional Array (SDA)

import java.util.*;
class arrya1
{
static void main( )
{
int k;
Scanner sc=new Scanner(System.in);
int number[ ]=new int[5];
System.out.println("Enter five numbers");
for(k=0;k<5;k++)
{
number[k]=sc.nextInt( );
}
System.out.println("Entered numbers are ");
for(k=0;k<5;k++)
{
System.out.println(number[k]);
}
}
}
2 Write a program to accept 5 names using SDA
import java.util.*;
class array2
{
static void main( )
{
Scanner sc=new Scanner(System.in);
int k;
String name[ ]= new String[5];
System.out.println("Enter five names");
for(k=0;k<5;k++)
{
name[k]=sc.next( );
}
System.out.println("Entered names are :");
for(k=0;k<5;k++)
{
System.out.println(name[k]);
}
}
}
3 Write a program to accept 5 characters using SDA
import java.util.*;
class array3
{
static void main( )
{
Scanner sc=new Scanner(System.in);
int k;
char c[]= new char[5];
System.out.println("Enter five characters");
for(k=0;k<5;k++)
{
c[k]=sc.next( ).charAt(0);
}
System.out.println("Entered characters are :");
for(k=0;k<5;k++)
{
System.out.println(c[k]);
}
}
}
4 WAP to input integer elements into an array of size 10 and display the sum
of all the elements.
import java.util.*;
class Addition
{
static void main()
{
Scanner sc = new Scanner(System.in);
int k,sum=0;
int arr[] = new int[10];
System.out.println("Enter 10 numbers");
for (k=0;k<10;k++)
{
arr[k]=sc.nextInt();
}

for (k=0;k<10;k++)
{
sum= sum+arr[k];
}

System.out.println("Sum of 10 numbers = " + sum);

}
}
4 Write a program in Java to store 10 numbers (even and odd numbers) in a
Single Dimensional Array (SDA). Calculate and display the sum of all
even numbers and all odd numbers separately.
import java.util.*;

class EvenOddSum
{
static void main()
{

Scanner sc = new Scanner(System.in);


int arr[] = new int[10];
int k,sumeven=0,sumodd=0;
System.out.println("Enter 10 numbers");
for (k=0;k<arr.length;k++)
{
arr[k] = sc.nextInt();
}

for (k=0;k< arr.length;k++)


{
if (arr[k] % 2 == 0)
sumeven = sumeven+arr[k];
else
sumodd = sumodd+arr[k];
}

System.out.println("Sum of even numbers = " + sumeven);


System.out.println("Sum of odd numbers = " + sumodd);
}
}
5 Write a program in Java to store 10 numbers (including positive and
negative numbers) in a Single Dimensional Array (SDA). Display all the
negative numbers followed by the positive numbers without changing the
order of the numbers.
Sample Input:

n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8]

15 21 -32 -41 54 61 71 -19 -44

Sample Output: -32, -41, -19, 44, 15, 21, 54, 61, 71, 52

import java.util.*;

class NegPosNumbers
{
static void main()
{

int arr[]= {-32, -41, -19, 44, 15, 21, 54, 61, 71, 52};
int k;
for(k=0;k<arr.length;k++)
{
if(arr[k] < 0)
System.out.print(arr[k] + ", ");
}

for(k=0;k<arr.length;k++)
{
if(arr[k] >= 0)
System.out.print(arr[k] + ", ");
}
}
}
6 Write a program to initialise the given data in an array and find the
minimum and maximum values along with the sum of the given elements.
Numbers: 2, 5, 4, 1, 3
Output:
Minimum value: 1
Maximum value: 5
Sum of the elements: 15
import java.util.*;
class question1MinMaxSum
{
static void main()
{
int arr[]={2, 5, 4, 1, 3};
int min=arr[0];
int max=arr[0];
int sum=0;
int k;
for(k=0;k<arr.length;k++)
{
if(arr[k]<min)
min=arr[k];
if(arr[k]>max)
max=arr[k];
sum= sum + arr[k];
}
System.out.println("Minimum value: " +min);
System.out.println("Maximum value: " +max);
System.out.println("Sum of the elements: " +sum);
}
}
7 Write a program to input integer elements into an array of size 20 and
perform the following operations:
1. Display largest number from the array
2. Display smallest number from the array
3. Display sum of all the elements of the array
import java.util.*;

class question8LSS
{
static void main()
{
Scanner sc= new Scanner(System.in);
int arr[]= new int[20];
int k,min=arr[0],max=arr[0],sum=0;
System.out.println("Enter 20 numbers:");
for(k=0;k<20;k++)
{
arr[k]=sc.nextInt();
}

for(k=0;k<20;k++)
{
if(arr[k]<min)
min=arr[k];
if(arr[k]>max)
max=arr[k];
sum=sum+arr[k];
}
System.out.println("Largest Number = " +max);
System.out.println("Smallest Number = " +min);
System.out.println("Sum = " +sum);
}
}
8 Write a program that reads ten integers and displays them in the reverse
order in which they were read.
import java.util.*;
class SDAReverse
{
static void main()
{
Scanner sc= new Scanner(System.in);
int arr[]= new int[10];
int k;
System.out.println("Enter 10 integers:");
for(k=0;k<10;k++)
{
arr[k]=sc.nextInt();
}
System.out.println("Integers in reverse order:");
for(k=9;k>=0;k--)
{
System.out.print(arr[k] + " ");
}
}
}
9 Write a program to store 6 elements in an array P and 4 elements in an
array Q.
Now, produce a third array R, containing all the elements of array P and Q.
Display the resultant array.
Input Input Output
P[ ] Q[ ] R[ ]
4 19 4
6 23 6
1 7 1
2 8 2
3 3
10 10
19
23
7
8
import java.util.*;
class PQRArrays
{
static void main()
{
Scanner sc = new Scanner(System.in);
int P[] = new int[6];
int Q[] = new int[4];
int R[] = new int[10];
int i = 0;
System.out.println("Enter 6 elements of array P:");
for (i=0;i<P.length;i++)
{
P[i]= sc.nextInt();
}
System.out.println("Enter 4 elements of array Q:");
for (i=0;i<Q.length;i++)
{
Q[i]= sc.nextInt();
}
for(i=0;i<6;i++)
{
R[i]=P[i];
}
for(int j=0;j<4;j++)
{
R[i++]=Q[j];
}

System.out.println("Elements of Array R:");


for (i=0;i<R.length;i++)
{
System.out.print(R[i] + " ");
}
}
}
10 Write a code segment to compute the sum of all positive real numbers
stored in the following array.
double numb[] = new double[50]
import java.util.*;
class sumquestion12
{
static void main()
{
Scanner sc= new Scanner(System.in);
int arr[]= new int[50];
int k;
int sum=0;
System.out.println("Enter 5 numbers:");
for(k=0;k<50;k++)
{
arr[k]=sc.nextInt();
}

for(k=0;k<50;k++)
{
if(arr[k]>0)
{
sum =sum+arr[k];
}
}
System.out.println("Sum of positive real numbers = " + sum);
}
}
11 Define a class to accept values into a double array of size 20 and print the
range of the array.
Range is the difference between the largest and the smallest element of the
array.
import java.util.*;
class range
{
static void main()
{
Scanner sc=new Scanner(System.in);
double num[]=new double[20], temp, range=0;
int k,p;
System.out.println("Enter 20 Numbers");
for(k=0;k<20;k++)
{
num[k]=sc.nextDouble();
}
for(k=0;k<20;k++)
{
for(p=0;p<19;p++)
{
if(num[p]>num[p+1])
{
temp=num[p];
num[p]=num[p+1];
num[p+1]=temp;
}
}
}
range=num[19]-num[0];
System.out.println("Range=" + range);
}
}
12 Accept ten integers using a SDA and sort the elements in ascending order
using bubble sort technique.
import java.util.*;
class SDBS
{
static void main()
{
Scanner sc=new Scanner(System.in);
int arr[]=new int[10];
int k,p,temp;
System.out.println("Enter the elements of the array:");
for(k=0;k<10;k++)
{
arr[k]=sc.nextInt();
}

//Bubble Sort
for(k=0;k<10;k++)
{
for(p=0;p<9;p++)
{
if(arr[p]>arr[p+1])
{
temp=arr[p];
arr[p]=arr[p+1];
arr[p+1]=temp;
}
}
}

System.out.println("Sorted Array:");
for (k=0;k<10;k++)
{
System.out.print(arr[k] + " ");
}
}
}
13 Accept ten integers using a SDA and sort the elements in descending order
using bubble sort technique.
import java.util.*;
class SDBS
{
static void main()
{
Scanner sc=new Scanner(System.in);
int arr[]=new int[10];
int k,p,temp;
System.out.println("Enter the elements of the array:");
for(k=0;k<10;k++)
{
arr[k]=sc.nextInt();
}

//Bubble Sort
for(k=0;k<10;k++)
{
for(p=0;p<9;p++)
{
if(arr[p]<arr[p+1])
{
temp=arr[p];
arr[p]=arr[p+1];
arr[p+1]=temp;
}
}
}
System.out.println("Sorted Array:");
for (k=0;k<10;k++)
{
System.out.print(arr[k] + " ");
}
}
}
14 Accept ten string elements using a SDA and sort the elements in ascending
order using bubble sort technique.
import java.util.*;
class BubbleSString
{
static void main()
{
Scanner sc=new Scanner(System.in);
String arr[]=new String[10];
int k,p;
String temp;
System.out.println("Enter the elements of the array:");
for(k=0;k<10;k++)
{
arr[k]=sc.next();
}

//Bubble Sort
for(k=0;k<10;k++)
{
for(p=0;p<9;p++)
{
if(arr[p].compareTo(arr[p+1])>0)
{
temp=arr[p];
arr[p]=arr[p+1];
arr[p+1]=temp;
}
}
}

System.out.println("Sorted Array:");
for (k=0;k<10;k++)
{
System.out.print(arr[k] + " ");
}
}
}

15 Accept ten character elements using a SDA and sort the elements in
ascending order using bubble sort technique.
import java.util.*;
class BubbleSCharacter
{
static void main()
{
Scanner sc=new Scanner(System.in);
char arr[]=new char[10];
int k,p;
char temp;
System.out.println("Enter the elements of the array:");
for(k=0;k<10;k++)
{
arr[k]=sc.next().charAt(0);
}

//Bubble Sort
for(k=0;k<10;k++)
{
for(p=0;p<9;p++)
{
if(arr[p]>(arr[p+1]))
{
temp=arr[p];
arr[p]=arr[p+1];
arr[p+1]=temp;
}
}
}

System.out.println("Sorted Array:");
for (k=0;k<10;k++)
{
System.out.print(arr[k] + " ");
}
}
}

You might also like