Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
64 views

Array Programs 01

The document contains 10 programs related to arrays in Java. Each program demonstrates different operations that can be performed on arrays such as: 1) Storing data in arrays and displaying it in reverse order or with calculations like sum and average 2) Storing country names and capitals in separate arrays and displaying them together 3) Converting temperatures from Fahrenheit to Celsius and displaying the results 4) Finding the highest score and name from an array of cricket scores 5) Separating even and odd numbers in an array and displaying their sums 6) Displaying prime numbers from an array 7) Calculating subject average and highest marks from student name and mark arrays 8) Displaying perfect squares from an input
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Array Programs 01

The document contains 10 programs related to arrays in Java. Each program demonstrates different operations that can be performed on arrays such as: 1) Storing data in arrays and displaying it in reverse order or with calculations like sum and average 2) Storing country names and capitals in separate arrays and displaying them together 3) Converting temperatures from Fahrenheit to Celsius and displaying the results 4) Finding the highest score and name from an array of cricket scores 5) Separating even and odd numbers in an array and displaying their sums 6) Displaying prime numbers from an array 7) Calculating subject average and highest marks from student name and mark arrays 8) Displaying perfect squares from an input
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

ICSE COMPUTER APLICATION

CHAPTER:: ARRAY
SOLVED PROGRAM
Paper 1

Program 1.Write a program to store 10 different numbers in a single Dimensional


Array(SDA)
I. To display all the number in reverse order
ii. To display the sum and average of all the numbers

import java.util.*;
public class Sum_Average
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[10];
int sum=0;
System.out.println("Enter 10 Numbers :");
for( int i=0;i<10;i++)
{

arr[i] = in.nextInt();
sum=sum+arr[i];
}
System.out.println("Reverse :");
for(int i=9;i>=0;i--) {
System.out.println(arr[i]);
}
System.out.println("Sum :"+sum);
System.out.println("average= :"+sum/10);
}
}

1
Program 2.Write a program to java to store 10 different country names and their
capitals in two different S.D.As. Display the names among with their capitals in the
given format.
Country Name Capital
xxxxx xxx
xxxxx xxx

import java.util.*;
public class Capital
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String country [] = new String [4];
String capital [] = new String [4];
for (int i=0;i<4;i++)
{
System.out.print ("Enter Country :");
country[i] = in.nextLine ();
System.out.print("Enter Capital :");
capital[i] = in.nextLine ();
}
System.out.println("Country Name"+"\t"+"Capital");
for(int i=0;i<4;i++) {
System.out.println(country[i]+"\t"+capital[i]);
}
}
}

2
Program 3.Write a program to store 20 temperatures in F in a S.D.A and display
all the temperatures after converting them into Centigrate.
import java.util.*;
class Temp {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double temp[] = new double[20];
System.out.println("Enter 20 Temperatures in Farenhite :");
for(int i=0;i<20;i++) {
temp[i]=in.nextInt();
}
System.out.println("Converting to Celcius Scale :");
for(int i=0;i<20;i++) {
System.out.println(((temp[i]-32)*5.0/9.0));
}
}
}

Program 4.Write a program to store the runs Scored by 11 Indian Cricket Players in
an innings along with names. Now display the name of the cricketer who has made
the highest score in that innings along with the runs.
import java.util.*;
public class criket
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String n[] = new String[11];
int r[] = new int[11];
String name=""; int highest_run=0;
for(int i=0;i<11;i++) {
System.out.print("Enter Cricketer :");
n[i]=in.nextLine();
System.out.print("Enter Runs :");
r[i]=in.nextInt();

3
in.nextLine();//This line is used to prevent nextline() skipping error.
}
for(int i=0;i<11;i++)
{
if(r[i]>highest_run) {
highest_run=r[i];
name=n[i];
}
}
System.out.println("Player with Highest Run :");
System.out.println("Name :"+name);
System.out.println("Runs :"+highest_run);
}
}

Program 5.Write a program to store 20 numbers (including even and odd numbers)
in a S.D.A and display the sum of even numbers and all odd numbers
separately stored in the cell.

import java.util.*;
class Odd_Num {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int num[] = new int[20];
int sum_e=0,sum_o=0;
for(int i=0;i<20;i++) {
System.out.print("Enter Number :");
num[i] = in.nextInt();}

for(int i=0;i<20;i++){
if(num[i]%2==0) {
sum_e=sum_e+num[i];
}
else {
sum_o=sum_o+num[i]; } }
System.out.println("Sum of Odds :"+sum_o);
System.out.println("Sum of Even :"+sum_e); }}

4
Program 6.Write a program to store 20 numbers in a S.D.A and display only those
numbers
which are prime.
import java.util.*;
public class Prime
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int num[] = new int[20];
System.out.println("Enter 20 Numbers : ");

for(int i=0;i<20;i++) {
num[i] = in.nextInt();
}
System.out.println("Prime Numbers :");
for(int i=0;i<20;i++)
{
int c=0;
for(int j=1;j<=num[i];j++)
{
if(num[i]%j==0)
{
c=c+1;
}
}
if(c==2)
{
System.out.println(num[i]);
}
}
}
}

5
Program 7.The marks obtained by 50 students in a subject are tabulated as
follows:-
Name Marks
..... .....
..... .....
..... .....
Write a program to input the names and marks of the students in the subject.
Calculate and display:
(a) The subject average marks (subject average marks = subject total/50).
(b) The highest marks in the subject and the name of the student.
(The maximum marks in the subject are 100.)

import java.util.*;
public class Average {
public static void main(String agrs[]) {
Scanner in = new Scanner(System.in);
String name[] = new String[50];
int marks[] = new int[50];
int sum=0,high=0;String hname="";
for(int i=0;i<50;i++) {
System.out.print("Enter Name :");
name[i] = in.nextLine();
System.out.print("Enter Marks :");
marks[i] = in.nextInt();
in.nextLine();//Line is added to prevent nextLine() Skip Error.
}
for(int i=0;i<50;i++){
sum=sum+marks[i];
if(marks[i]>high)
{
high=marks[i];
hname=name[i];
}
}

6
System.out.println("Average Marks :"+(sum/50.0));
System.out.println("Highest Marks :"+high+" by "+hname); }}

Program 8.Write a program to store 20 numbers in a Single Dimensional Array


(SDA).
Now, display only those numbers that are perfect squares.
input:
12 45 49 78 64 77 ... 81 99 45 33
output: 49 64 81

*/
import java.util.Scanner;

public class Perfect_Squares


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];

System.out.println("Enter 20 numbers");
for (int i = 0; i <20; i++) {
arr[i] = in.nextInt();
}

System.out.println("Perfect Squares are:");


for (int i = 0; i < 20; i++) {
double sr = Math.sqrt(arr[i]);
if ((sr - Math.floor(sr)) == 0)
System.out.print(arr[i] + ", ");
}
}
}

7
Program 9.Write a program to accept name and total marks of N number of
students in two single subscript arrays name[ ] and totalmarks[ ].
Calculate and print:
(i) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student's total marks with the average.
[deviation = total marks of a student - average]

import java.util.Scanner;

public class deviation


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();
String name[] = new String[n];
int totalmarks[] = new int[n];
int grandTotal = 0;

for (int i = 0; i < n; i++)


{
in.nextLine();
System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter total marks of student " + (i+1) + ": ");
totalmarks[i] = in.nextInt();
grandTotal =grandTotal+ totalmarks[i];
}

double avg = grandTotal / (double)n;


System.out.println("Average = " + avg);

for (int i = 0; i < n; i++) {


System.out.println("Deviation for " + name[i] + " = " + (totalmarks[i] - avg));
}

8
}
}
Program 10.Write a program in Java using arrays:
(a) To store the Roll No., Name and marks in six subjects for 100 students.
(b) Calculate the percentage of marks obtained by each candidate.
The maximum marks in each subject are 100.
(c) Calculate the Grade as per the given criteria:

Percentage Marks Grade


From 80 to 100 A
From 60 to 79 B
From 40 to 59 C
Less than 40 D

import java.util.*;
public class Marks
{
public static void main(String agrs[])
{
Scanner in = new Scanner(System.in);
String name[] = new String[100];
double percent[] = new double[100];
char grade[]= new char[100];
for(int i=0;i<100;i++) {
int sum=0;
System.out.print("Enter Name :");
name[i] = in.nextLine();
for(int j=1;j<=6;j++) {
System.out.print("Enter Marks of Subject "+j+":");

9
int n=in.nextInt();
sum=sum+n;
}
in.nextLine();
percent[i]=(sum/600.0)*100;
if(percent[i]>=80) {
grade[i]='A';
}
else if(percent[i]>=60) {
grade[i]='B';
}
else if(percent[i]>=40) {
grade[i]='C';
}
else {
grade[i]='D';
}
}
System.out.println("Name\tPercentage\tGrade");
for(int i=0;i<100;i++) {
System.out.println(name[i]+"\t"+percent[i]+"\t"+grade[i]);
}
}
}

10
Program 11.Write a program to accept 10 different numbers in a single
dimensional array (SDA) display the grestest and the smallest numbers of the array
element

import java.util.*;
public class Max_Min
{
public static void main(String rgs[])
{
Scanner in=new Scanner(System.in);
int i,min,max;
int m[]=new int[10];
for(i=0;i<10;i++)
{
System.out.print("enter the no. in the cell :");
m[i]=in.nextInt();
}
max=m[0];min=m[0];
for(i=0;i<10;i++)
{
if(m[i]>max)
max=m[i];
if(m[i]<min)
min=m[i];
}
System.out.println("the greatest of the array elements ="+max);
System.out.println("the smallest of the array elements ="+min);
}}

11
Program 12.Accept 10 numbers into an array and then calculate the sum of even
numbers present in odd positions.

import java.util.*;
public class even_odd_position_number
{
public static void arr(String args[])
{
Scanner sc=new Scanner(System.in);
int n[] = new int[10];
for(int i = 0;i<10;i++)
{
System.out.print ("Enter a number : ");
n[i] = sc.nextInt();
}
int Sum = 0;
for(int i = 0;i<10;i++)
if(n[i]%2 == 0 && i%2!= 0)
Sum = Sum+n[i];
System.out.println ("Sum of Numbers : " + Sum);
}
}

Program 13.Write a program to accept 10 different names in a Single Dimensional


array SDA.
Now enter a name and search whether the name is present or not
in the list of array elements by using the "linear search" technique.
import java.util.*;
public class Search_name
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int i,k=0; String ns;
String m[]=new String[5];
for(i=0;i<5;i++) {

12
System.out.print("Enter the name in the cell:");

m[i]=in.next();
}
System.out.print("Enter the name to be searched:");
ns=in.next ();
for(i=0;i<5;i++)
{
if(m[i].equals(ns))
k=k+1;
}
if(k==1)
System.out.println("The name is present");
else
System.out.print("The name is not present");
}}

Program 14. Accept the name, physics, chemistry and math marks of 25 students.
The display a list of the given data with Total and Average.
import java.util.*;

public class sum__marks


{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String a[] = new String[5];
int b[] = new int[5];
int c[] = new int[5];
int d[]= new int[5];
int i;
for(i = 0;i<5;i++)
{
System.out.print ("enter name:");
a[i] =sc.next();
System.out.print ("enter physics marks:");
b[i] = sc.nextInt();

13
System.out.print ("enter chemistry marks:");
c[i] = sc.nextInt();
System.out.print ("enter math marks:");
d[i]=sc.nextInt();
}
System.out.println ("Name \t\t Phy \t\t Chem \t\t math\t\t Total \t\t Average");
for(i = 0;i<5;i++)
{
int total = b[i]+c[i]+d[i];
float avg = total/3;
System.out.println (a[i]+ "\t\t"+b[i]+"\t\t"+c[i]+"\t\t"+d[i]+
"\t\t"+total+"\t\t"+avg);
}
}
}

Program 15.Write a program to store 20 different names along with corresponding


Telephone numbers. Enter a name from the console and search whether the name
is present or not. If the name is present then display the name along the phone
number by linear search technique.
import java.io.*;
public class Name_telephone
{
public static void main(String args[])throws IOException
{
InputStreamReader read =new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int i,a=0,l=0,k=0;
String name,b="";
String m[]=new String[20];
long n[]=new long[20];
for(i=0;i<20;i++)
{
System.out.print("Enter name one by one :");
m[i]=in.readLine();

14
System.out.print("Enter cooresponding telephone number one by one:");
n[i]=Long.parseLong(in.readLine());
}
System.out.println("Enter the name of to be searched");
name=in.readLine();
for(i=0;i<20;i++)
{
if(m[i].equals(name))
{
k=k+1;
a=i;
b=m[i];
}}
if(k==1)
System.out.println("the name is " + b + " and the telephone number is " +n[a]);
else
System.out.println(name + "is not found at any location");
}
}

15

You might also like