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 [Link] ;
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 ([Link]);
[Link] ( " ENTER NAME OF PERSON TO BE SEARCH " ) ;
search = [Link]();
c=0;
for ( i=0; i<5 ; i++)
{
if ([Link] ( name[i] ))
{
[Link] ( " SEARCH SUCCESSFUL " ) ;
[Link] ( " Name of the person ..." +name[i]);
[Link] ( " Mobile number ...."+ phone[i]);
c++;
}
}
if (c==0)
{
[Link] ( " SEARCH UNSUCCESSFUL " ) ;
[Link] ( 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 [Link];
class EvenNumberChars
{
String str[] = new String [10];
int es;
int len;
int i;
public void main()
{
Scanner st = new Scanner ([Link]);
for (i=0;i<10;i++)
{
[Link] ( " Enter a String " ) ;
str[i]= [Link]();
}
[Link] ( " EVEN NUMBER OF STRINGS ENTERED IN THE LIST " ) ;
for (i=0; i<10 ;i++)
{
len = str[i].length();
if (len%2==0)
{
[Link] ( str[i] ) ;
}
}
}
}
Question :3
Write a program to accept 10 integers into an array to print even number positioned
elements.
Program :
import [Link] ;
public class evenposition
{
int x[] = new int[10];
int i ;
public void main()
{
Scanner st = new Scanner ([Link]);
for ( i=0;i<10;i++)
{
[Link] ( " Enter a number " ) ;
x[i] = [Link]();
}
for ( i=0;i<10 ; i++)
{
if (i%2==0)
{
[Link] (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 [Link] ;
public class sumOfEvenOdd
{
int x[] = new int [20];
int i ;
int se ;
int so;
public void main()
{
Scanner st = new Scanner ([Link]);
for ( i=0;i<20;i++)
{
[Link] ( " Enter a number " ) ;
x[i] = [Link]();
}
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++)
{
[Link] (x[i]);
}
[Link] ( " Sum of Even " + se );
[Link] ( " 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 [Link];
public class vow
{
public static void main()
{
Scanner st = new Scanner ([Link] );
String x ;
char ch =' ';
int len = 0 ;
int i=0;
[Link] ( " Enter a string " ) ;
x =[Link]();
len = [Link]();
String y = [Link]();
for (i=0;i<len ; i++)
{
ch = [Link](i);
if ( ch >= 'a' && ch <='z')
{
if (( ch=='a' || ch=='e' || ch=='i' || ch=='o' ||ch=='u'))
[Link] ('*');
else
[Link]('@');
}
else
{
[Link]('#');
}
}
}
}
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 [Link];
public class SquareElements
{
int x[] = new int [10];
int i ;
int sq[] = new int [10];
public void main()
{
Scanner st = new Scanner ([Link]);
for ( i=0;i<10;i++)
{
[Link] ( " Enter a number " ) ;
x[i] = [Link]();
}
for (i=0;i<10;i++)
{
sq[i] = x[i] * x[i];
}
for ( i=0 ;i<10; i++)
{
[Link] ( 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 [Link];
public class MaxMin
{
int x[] = new int [10];
int i ;
int max;
int min;
public void main()
{
Scanner st = new Scanner ([Link]);
for ( i=0;i<10;i++)
{
[Link] ( " Enter a number " ) ;
x[i] = [Link]();
}
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++)
{
[Link] (x[i]);
}
[Link] ( "MAXIMUM ..." + max ) ;
[Link] ( "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 [Link];
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 ([Link]);
for (i=0;i<40;i++)
{
[Link] ( "Enter Physic marks of the student " ) ;
phy[i]=[Link]();
[Link] ( "Enter Chemistry marks of the student " ) ;
che[i]=[Link]();
[Link] ( "Enter Math marks of the student " ) ;
math[i]=[Link]();
}
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++;
}
[Link] ( " Number of Students scored above 80 " + n80);
[Link] ( " 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 [Link];
public class palinArray
{
String x[] = new String [5];
int np;
String Reverse (String str )
{
int len ;
len = [Link]() ;
char x ;
x='';
String rev=" " ;
for ( int i= len-1; i>=0;i-- )
{
x = [Link](i);
rev = rev + x ;
}
rev = [Link]();
return rev;
}
public void main()
{
Scanner st = new Scanner ([Link]);
for ( int i=0;i<5 ;i++)
{
[Link] ( " Enter a string " ) ;
x[i] = [Link]();
}
for ( int i=0 ;i<5;i++)
{
if (x[i].equals(Reverse(x[i])))
{
[Link] (x[i] + " is palindrome");
np++;
}
}
[Link] ( " 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
[Link] Remark
85 – 100 Excellent
75 – 84 Distinction
60 - 74 First class
40 – 59 Pass
Less than 40 Poor
Program
import [Link];
public class student2
{
public static void main()
{
Scanner st = new Scanner ([Link]);
[Link] ( " Enter number of students .............." ) ;
int n= [Link]();
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++)
{
[Link] ( " Enter Roll Number " ) ;
roll[i] = [Link]();
[Link] ( " Enter name of the student " ) ;
name[i]=[Link]();
[Link] ( " Enter marks in Mathematics" );
mat[i] = [Link]();
[Link] ( " Enter marks in physics" );
phy[i] = [Link]();
[Link] ( " Enter marks in Chemistry " );
che [i] = [Link]();
avg[i] = ( mat[i] + phy [i] + che [i] ) / 3f;
}
for ( int i=0 ;i<n ;i++)
{
[Link] ( "ROLL NUMBER " + roll[i]);
[Link] ( "NAME OF THE STUDENT " + name[i]);
[Link] ( "AVERANGE MARK " + avg[i]);
if ( avg[i]>=85)
[Link] ( "COMMENT ..... EXCELLENT " ) ;
if ( avg[i] >=75 && avg[i]<85)
[Link] ( "COMMENT...... DISTENSION " ) ;
if ( avg[i]>=60 && avg[i] <75)
[Link] ( "COMMENT..... FIRST CLASS " ) ;
if ( avg[i] >=40 && avg[i] < 60 )
System .[Link] ( "COMMENT....... PASS " ) ;
if ( avg[i] <40 )
[Link] ( "COMMENT .......POOR" ) ;
}
}
}