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

Array Practice Programs

The document contains 10 programming questions that involve working with arrays in Java. The questions cover topics like initializing arrays, searching arrays, calculating statistics of array elements like sum and average, and manipulating string and integer values within arrays. For each question, the document provides the full code of a Java program that implements the required functionality to solve that question.

Uploaded by

Sudhir Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Array Practice Programs

The document contains 10 programming questions that involve working with arrays in Java. The questions cover topics like initializing arrays, searching arrays, calculating statistics of array elements like sum and average, and manipulating string and integer values within arrays. For each question, the document provides the full code of a Java program that implements the required functionality to solve that question.

Uploaded by

Sudhir Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Programs to be practice……..

Question :1

Write a program to initialize an array of 5 names and initialize another array with their
respective telephone numbers. Search for a name input by the user, in the list. If found,
display “Search Successful” and print the name along with the telephone number,
otherwise display “Search unsuccessful, Name not listed”.
Program :
import java.util.Scanner ;
public class namephone{
String name [] = {"dhoni","kohli","kapil","babji","rahuil"};
String phone[]={"9490444303","9912343233","8500786365" ,"9848022782",
"8500456321"};
String search ;
int c ;
int i ;
public void main()
{
Scanner st = new Scanner (System.in);
System.out.println ( " ENTER NAME OF PERSON TO BE SEARCH " ) ;
search = st.next();
c=0;
for ( i=0; i<5 ; i++)
{
if (search.equals ( name[i] ))
{
System.out.println ( " SEARCH SUCCESSFUL " ) ;
System.out.println ( " Name of the person ..." +name[i]);
System.out.println ( " Mobile number ...."+ phone[i]);
c++;
}
}
if (c==0)
{
System.out.println ( " SEARCH UNSUCCESSFUL " ) ;
System.out.println ( search + " NOT FOUND IN THE LIST " ) ;
}
}
}
Question 2
Define a class to accept and store 10 strings into the array and print the strings with
even number of characters.

Program :
import java.util.Scanner;
class EvenNumberChars
{
String str[] = new String [10];
int es;
int len;
int i;
public void main()
{
Scanner st = new Scanner (System.in);
for (i=0;i<10;i++)
{
System.out.println ( " Enter a String " ) ;
str[i]= st.next();
}
System.out.println ( " EVEN NUMBER OF STRINGS ENTERED IN THE LIST " ) ;
for (i=0; i<10 ;i++)
{
len = str[i].length();
if (len%2==0)
{
System.out.println ( str[i] ) ;
}
}
}
}
Question :3
Write a program to accept 10 integers into an array to print even number positioned
elements.

Program :
import java.util.Scanner ;
public class evenposition
{
int x[] = new int[10];
int i ;
public void main()
{
Scanner st = new Scanner (System.in);
for ( i=0;i<10;i++)
{
System.out.println ( " Enter a number " ) ;
x[i] = st.nextInt();
}

for ( i=0;i<10 ; i++)


{
if (i%2==0)
{
System.out.println (x[i]);
}
}
}
}

Question :4
Write a program to accept 20 integers in to an array to calculate and print sum of even
numbers and sum of odd numbers separately.

Program :
import java.util.Scanner ;
public class sumOfEvenOdd
{
int x[] = new int [20];
int i ;
int se ;
int so;
public void main()
{
Scanner st = new Scanner (System.in);
for ( i=0;i<20;i++)
{
System.out.println ( " Enter a number " ) ;
x[i] = st.nextInt();
}

for ( i=0;i<20;i++)
{
if(x[i]%2==0)
{
se=se+x[i];
}
else
{
so=so+x[i];
}
}
for (i=0;i<20;i++)
{
System.out.println (x[i]);
}
System.out.println ( " Sum of Even " + se );
System.out.println ( " Sum of Odd" + so);
}
}
Question 5
Write a program to accept a string and print the string replacing the vowels with ‘*’ ,
consonants with ‘@’ and other characters with ‘#’
Ex : BEAUTIFUL@123
OUTPUT : @***@*@*@####
Program :
import java.util.Scanner;
public class vow
{
public static void main()
{
Scanner st = new Scanner (System.in );
String x ;
char ch =' ';
int len = 0 ;
int i=0;
System.out.println ( " Enter a string " ) ;
x =st.next();
len = x.length();
String y = x.toLowerCase();
for (i=0;i<len ; i++)
{
ch = y.charAt(i);
if ( ch >= 'a' && ch <='z')
{
if (( ch=='a' || ch=='e' || ch=='i' || ch=='o' ||ch=='u'))
System.out.print ('*');
else
System.out.print('@');
}
else
{
System.out.print('#');
}
}
}
}
Question:6
Write a program to accept 10 integers into an array and store their squared elements
into another array to print both arrays.

Program :
import java.util.Scanner;
public class SquareElements
{
int x[] = new int [10];
int i ;
int sq[] = new int [10];
public void main()
{
Scanner st = new Scanner (System.in);
for ( i=0;i<10;i++)
{
System.out.println ( " Enter a number " ) ;
x[i] = st.nextInt();
}
for (i=0;i<10;i++)
{
sq[i] = x[i] * x[i];
}

for ( i=0 ;i<10; i++)


{
System.out.println ( x[i] + " squared element " + sq[i]);
}
}
}

Question : 7
Write a program to accept 10 integers into an array to find and print greatest and
smallest of array.
Program :
import java.util.Scanner;
public class MaxMin
{
int x[] = new int [10];
int i ;
int max;
int min;
public void main()
{
Scanner st = new Scanner (System.in);
for ( i=0;i<10;i++)
{
System.out.println ( " Enter a number " ) ;
x[i] = st.nextInt();
}
max=min=x[0];
for (i=0;i<10;i++)
{
if (x[i]>max)
{
max = x[i];
}
if (x[i]<min)
{
min=x[i];
}

}
for (i=0;i<10;i++)
{
System.out.println (x[i]);
}
System.out.println ( "MAXIMUM ..." + max ) ;
System.out.println ( "MINIMUM...."+ min);
}
}
Question 8
Write a program to accept the marks in physics , chemistry and maths secured by 40
students of a class in single dimensional array. Find and display the following
Number of students securing 80% and above percentage
Number of students securing 34 % and below percentage
Program :
import java.util.Scanner;
class student
{
int phy[]=new int[40];
int math[] =new int [40];
int che[] = new int [40];
int per[] = new int [40];
int n80;
int n35;
int i;
public void main()
{
Scanner st = new Scanner (System.in);
for (i=0;i<40;i++)
{
System.out.println ( "Enter Physic marks of the student " ) ;
phy[i]=st.nextInt();
System.out.println ( "Enter Chemistry marks of the student " ) ;
che[i]=st.nextInt();
System.out.println ( "Enter Math marks of the student " ) ;
math[i]=st.nextInt();
}
for (i=0;i<40;i++)
{
per[i] = ( (phy[i] + che[i] + math[i] ) /3 ) ;
if (per[i]>80)
n80++;
if (per[i]<35)
n35++;
}
System.out.println ( " Number of Students scored above 80 " + n80);
System.out.println ( " Number of Students scored below 35 " + n35);
}
}
Question 9
Write a program to accept 20 strings into an array to find and print number of
palindrome strings entered by the user .
Program :
import java.util.Scanner;
public class palinArray
{
String x[] = new String [5];
int np;
String Reverse (String str )
{
int len ;
len = str.length() ;
char x ;
x='';
String rev=" " ;
for ( int i= len-1; i>=0;i-- )
{
x = str.charAt(i);
rev = rev + x ;
}
rev = rev.trim();
return rev;
}
public void main()
{
Scanner st = new Scanner (System.in);
for ( int i=0;i<5 ;i++)
{
System.out.println ( " Enter a string " ) ;
x[i] = st.next();
}
for ( int i=0 ;i<5;i++)
{
if (x[i].equals(Reverse(x[i])))
{
System.out.println (x[i] + " is palindrome");
np++;
}
}
System.out.println ( " Number of Palindromes in the Array " + np ) ;
}
}
Question :10
Write a program to input and store roll numbers, names and marks in 3 subjects of N
number of students in 5 single dimensional arrays and display the remarks based on
average mark as given below
Avg.Marks Remark
85 – 100 Excellent
75 – 84 Distinction
60 - 74 First class
40 – 59 Pass
Less than 40 Poor

Program
import java.util.Scanner;
public class student2
{
public static void main()
{
Scanner st = new Scanner (System.in);
System.out.println ( " Enter number of students .............." ) ;
int n= st.nextInt();
int roll[] = new int [n];
String name [] = new String [n] ;
int mat[] = new int [n] ;
int phy [] = new int [n];
int che [] = new int [n] ;
float avg[] = new float [n];
for ( int i=0 ;i<n;i++)
{
System.out.println ( " Enter Roll Number " ) ;
roll[i] = st.nextInt();
System.out.println ( " Enter name of the student " ) ;
name[i]=st.next();
System.out.println ( " Enter marks in Mathematics" );
mat[i] = st.nextInt();
System.out.println ( " Enter marks in physics" );
phy[i] = st.nextInt();
System.out.println ( " Enter marks in Chemistry " );
che [i] = st.nextInt();
avg[i] = ( mat[i] + phy [i] + che [i] ) / 3f;
}
for ( int i=0 ;i<n ;i++)
{
System.out.println ( "ROLL NUMBER " + roll[i]);
System.out.println ( "NAME OF THE STUDENT " + name[i]);
System.out.println ( "AVERANGE MARK " + avg[i]);
if ( avg[i]>=85)
System.out.println ( "COMMENT ..... EXCELLENT " ) ;
if ( avg[i] >=75 && avg[i]<85)
System.out.println ( "COMMENT...... DISTENSION " ) ;
if ( avg[i]>=60 && avg[i] <75)
System.out.println ( "COMMENT..... FIRST CLASS " ) ;
if ( avg[i] >=40 && avg[i] < 60 )
System .out.println ( "COMMENT....... PASS " ) ;
if ( avg[i] <40 )
System.out.println ( "COMMENT .......POOR" ) ;
}
}
}

You might also like