0% found this document useful (0 votes)
43 views14 pages

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)
43 views14 pages

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

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

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

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

[Link]("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 [Link].*;

class EvenOddSum
{
static void main()
{

Scanner sc = new Scanner([Link]);


int arr[] = new int[10];
int k,sumeven=0,sumodd=0;
[Link]("Enter 10 numbers");
for (k=0;k<[Link];k++)
{
arr[k] = [Link]();
}

for (k=0;k< [Link];k++)


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

[Link]("Sum of even numbers = " + sumeven);


[Link]("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 [Link].*;

class NegPosNumbers
{
static void main()
{

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

for(k=0;k<[Link];k++)
{
if(arr[k] >= 0)
[Link](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 [Link].*;
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<[Link];k++)
{
if(arr[k]<min)
min=arr[k];
if(arr[k]>max)
max=arr[k];
sum= sum + arr[k];
}
[Link]("Minimum value: " +min);
[Link]("Maximum value: " +max);
[Link]("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 [Link].*;

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

for(k=0;k<20;k++)
{
if(arr[k]<min)
min=arr[k];
if(arr[k]>max)
max=arr[k];
sum=sum+arr[k];
}
[Link]("Largest Number = " +max);
[Link]("Smallest Number = " +min);
[Link]("Sum = " +sum);
}
}
8 Write a program that reads ten integers and displays them in the reverse
order in which they were read.
import [Link].*;
class SDAReverse
{
static void main()
{
Scanner sc= new Scanner([Link]);
int arr[]= new int[10];
int k;
[Link]("Enter 10 integers:");
for(k=0;k<10;k++)
{
arr[k]=[Link]();
}
[Link]("Integers in reverse order:");
for(k=9;k>=0;k--)
{
[Link](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 [Link].*;
class PQRArrays
{
static void main()
{
Scanner sc = new Scanner([Link]);
int P[] = new int[6];
int Q[] = new int[4];
int R[] = new int[10];
int i = 0;
[Link]("Enter 6 elements of array P:");
for (i=0;i<[Link];i++)
{
P[i]= [Link]();
}
[Link]("Enter 4 elements of array Q:");
for (i=0;i<[Link];i++)
{
Q[i]= [Link]();
}
for(i=0;i<6;i++)
{
R[i]=P[i];
}
for(int j=0;j<4;j++)
{
R[i++]=Q[j];
}

[Link]("Elements of Array R:");


for (i=0;i<[Link];i++)
{
[Link](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 [Link].*;
class sumquestion12
{
static void main()
{
Scanner sc= new Scanner([Link]);
int arr[]= new int[50];
int k;
int sum=0;
[Link]("Enter 5 numbers:");
for(k=0;k<50;k++)
{
arr[k]=[Link]();
}

for(k=0;k<50;k++)
{
if(arr[k]>0)
{
sum =sum+arr[k];
}
}
[Link]("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 [Link].*;
class range
{
static void main()
{
Scanner sc=new Scanner([Link]);
double num[]=new double[20], temp, range=0;
int k,p;
[Link]("Enter 20 Numbers");
for(k=0;k<20;k++)
{
num[k]=[Link]();
}
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];
[Link]("Range=" + range);
}
}
12 Accept ten integers using a SDA and sort the elements in ascending order
using bubble sort technique.
import [Link].*;
class SDBS
{
static void main()
{
Scanner sc=new Scanner([Link]);
int arr[]=new int[10];
int k,p,temp;
[Link]("Enter the elements of the array:");
for(k=0;k<10;k++)
{
arr[k]=[Link]();
}

//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;
}
}
}

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

//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;
}
}
}
[Link]("Sorted Array:");
for (k=0;k<10;k++)
{
[Link](arr[k] + " ");
}
}
}
14 Accept ten string elements using a SDA and sort the elements in ascending
order using bubble sort technique.
import [Link].*;
class BubbleSString
{
static void main()
{
Scanner sc=new Scanner([Link]);
String arr[]=new String[10];
int k,p;
String temp;
[Link]("Enter the elements of the array:");
for(k=0;k<10;k++)
{
arr[k]=[Link]();
}

//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;
}
}
}

[Link]("Sorted Array:");
for (k=0;k<10;k++)
{
[Link](arr[k] + " ");
}
}
}

15 Accept ten character elements using a SDA and sort the elements in
ascending order using bubble sort technique.
import [Link].*;
class BubbleSCharacter
{
static void main()
{
Scanner sc=new Scanner([Link]);
char arr[]=new char[10];
int k,p;
char temp;
[Link]("Enter the elements of the array:");
for(k=0;k<10;k++)
{
arr[k]=[Link]().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;
}
}
}

[Link]("Sorted Array:");
for (k=0;k<10;k++)
{
[Link](arr[k] + " ");
}
}
}

You might also like