Professional Documents
Culture Documents
ASSIGNMEN
T
1|PAGE
SUBMITTED TO: RESMI
MISS
SUBMITTED BY: GAYATHRI
G KURUP
ICSE X
JAVA PROGRAMS
1] Write a program to input a single dimensional array of 5 elements and print
it.
Program code;
import java.util.*;
public class question1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int arr[]=new int[5];
for(int i=0;i<5;i++)
{
System.out.println(" INPUT ELEMENT IN "+"THE POSITION");
arr[i]=sc.nextInt();
}
System.out.println(" THE ARRAY ELEMENTS ARE: ");
for(int i=0;i<5;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Output
INPUT ELEMENT IN THE POSITION
45
2|PAGE
INPUT ELEMENT IN THE POSITION
5
INPUT ELEMENT IN THE POSITION
3
INPUT ELEMENT IN THE POSITION
6
INPUT ELEMENT IN THE POSITION
1
THE ARRAY ELEMENTS ARE:
45 5 3 6 1
Program code;
Output
3]Write a function int printSum( int Arr[]) to find and return the sum of only
even numbers from the array Arr[] passed as argument. Write a main () function
to initialize a single dimensional array of 15 integers by calling function
printSum() display sum of even numbers.
Program code;
import java.util.*;
3|PAGE
public class question3
{
public int printSum(int Arr[])
{
int sum=0;
int l=Arr.length;
for(int i=0;i<l;i++)
{
if(Arr[i]%2==0)
{
sum=sum+Arr[i];
}
}
return sum;
}
public void main()
{
int m[]= {7,10,13,21,28,32,55,69,72,96,132,555,21,41,69};
int s=printSum(m);
System.out.println(" SUM OF EVEN NUMBERS FROM ARRAY = "+s);
}
}
Output
4] Design a function void Largest( int arr[]) to print the largest from the array
arr[]. Also, write a function void display() to initialize a single dimensional
array of 10 integers and by invoking the function Largest() and by passing
array, display the largest integer.
Program code;
import java.util.*;
public class question4
{
void Largest(int arr[])
{
int large=0;
int l=arr.length;
for(int i=0;i<l;i++)
4|PAGE
{
if(arr[i]>large)
large=arr[i];
}
System.out.println(" LARGEST INTEGER FROM THE ARRAY =
"+large);
}
void display()
{
int m[]= {96,69,132,35,34,55,28,13,10,1};
Largest(m);
}
}
Output
Program code;
import java.util.*;
public class question5
{
public void bubblesort()
{
int temp;
int A[]= {132,72,69,55,32,28,27,21,13,10};
System.out.println("ARRAY BEFORE SORTING : ");
for(int j=0;j<10;j++)
{
System.out.print(A[j]+" ");
}
for(int j=0;j<10-1;j++)
{
for(int k=0;k<10-j-1;k++)
{
if(A[k]>A[k+1])
5|PAGE
{
temp=A[k];
A[k]=A[k+1];
A[k+1]=temp;
}
}
}
System.out.println("ARRAY AFTER SORTING : ");
for(int j=0;j<10;j++)
{
System.out.print(A[j]+" ");
}
}
}
Output
6] Write a program to initialize two strings and print them in different lines as
well as in the same line with onespace between them.
Program code;
import java.util.*;
public class question6
{
public void show()
{
String s1= "WELCOME TO THE WORLD";
String s2= " OF JAVA PROGRAMMING";
System.out.println("OUTPUT 1:");
System.out.println("FIRST STRING = "+s1);
System.out.println("SECOND STRING = "+s2);
System.out.println("OUTPUT 2:");
System.out.println("FIRST AND SECOND STRING TOGETHER
WITHE ONE SPACE = "+s1+" "+s2);
}
}
6|PAGE
Output
OUTPUT 1:
FIRST STRING = WELCOME TO THE WORLD
SECOND STRING = OF JAVA PROGRAMMING
OUTPUT 2:
FIRST AND SECOND STRING TOGETHER WITHE ONE SPACE =
WELCOME TO THE WORLD OF JAVA PROGRAMMING
7] Write a program to print ASCII codes of all alphabets from A-Z along with
the alphabet.
Program code;
Output
A = 65 B = 66 C = 67 D = 68 E = 69 F = 70 G = 71 H = 72 I = 73 J = 74 K = 75
L = 76 M = 77 N = 78 O = 79 P = 80 Q = 81 R = 82 S = 83 T = 84 U = 85 V =
86 W = 87 X = 88 Y = 89 Z = 90
8] Write a program to print ASCII codes of all alphabets from a-z along with
the alphabet.
Program code;
7|PAGE
{
for(char ch='a';ch<='z';ch++)
{
System.out.print(ch+" = " +(int)ch+" ");
}
}
}
Output
9] Write a program to print ASCII codes of all digits from 0-9 along with the
digits.
Program code;
Output
8|PAGE
10] Write a program to accept the names of 5 cities in a single dimension string
array and their STD (Subscribers Trunk Dialing) codes in another single
dimension integer array. Search for a name of a city input by the user in the list.
If found, display “Search Successful” and print the name of the city along with
its STD code, or else display the message “Search Unsuccessful, No such city in
the list.
Program code;
import java.util.*;
public class question10
{
public static void main(String args[]) {
final int SIZE = 5;
Scanner in = new Scanner(System.in);
String cities[] = new String[SIZE];
String stdCodes[] = new String[SIZE];
System.out.println("Enter " + SIZE +
" cities and their STD codes:");
int idx;
for (idx = 0; idx < SIZE; idx++) {
if (city.compareToIgnoreCase(cities[idx]) == 0) {
break;
}
}
Search Successful
City: seoul
STD Code: 8569
Program code;
import java.util.*;
public class question11
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String words[] = new String[5];
System.out.println("Enter 5 words:");
System.out.println("\nPalindrome Words:");
10 | P A G E
for (int i = 0; i < words.length; i++) {
String str = words[i].toUpperCase();
int strLen = str.length();
boolean isPalin = true;
Output
Enter 5 words:
malayalam
lamina
styles
toot
hello
Palindrome Words:
malayalam
toot
12] Write a program to accept a character if it is a letter then display the case
i.e lower or upper, Otherwise check whether it is digit or special character.
Program code;
import java.util.*;
public class question12
{
public static void main(String args[])
{
System.out.println("\f");
Scanner sc=new Scanner(System.in);
11 | P A G E
System.out.println("Enter a character");
char ch = sc.next().charAt(0);
if(Character.isLetter(ch))
{
System.out.println(ch+" is a letter");
if(Character.isUpperCase(ch))
System.out.println(ch +" is in upper Case");
else
System.out.println(ch +" is in lower Case");
}
else if(Character.isDigit(ch))
System.out.println(ch +" is a digit");
else
System.out.println(ch +" is a Special Character");
}
}
Output
Enter a character
g
g is a letter
g is in lower Case
13] Write a program to accept a word and display the ASCII of each character.
Program code;
import java.util.*;
public class question13
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word");
String st=sc.next();
for(int i=0;i<=st.length()-1;i++)
{
char x=st.charAt(i);
System.out.println("ASCII of "+x+" = "+(int)x);
}
}
12 | P A G E
}
Output
Enter a word
HarryStyles
ASCII of H = 72
ASCII of a = 97
ASCII of r = 114
ASCII of r = 114
ASCII of y = 121
ASCII of S = 83
ASCII of t = 116
ASCII of y = 121
ASCII of l = 108
ASCII of e = 101
ASCII of s = 115
14] A non -Palindrome word can be a palindrome word just by adding reverse
of the word with the original word. Write a program to accept a non -
palindrome word and display the new word after making it a palindrome.
Program code;
import java.util.*;
public class question14
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String st=sc.nextLine();
st=st.trim();
for(int i=st.length()-1;i>=0;i--)
{
char x=st.charAt(i);
st=st+x;
}
System.out.println(st);
}
}
13 | P A G E
Output
Enter a string
Onedirection
onedirectionnoitcerideno
Program code;
import java.util.*;
public class question15
{
public static void main(String args[])
{
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("Enter a character");
char ch=sc.next().charAt(0);
if(ch=='z')
System.out.println("a");
else if(ch=='Z')
System.out.println("A");
else
System.out.println(++ch);
}
}
Output
Enter a character
g
h
16] Write a program in Java to accept a string in lowercase and change the first
letter of every word to uppercase. Display the new string.
Program code;
import java.util.Scanner;
14 | P A G E
public class question16
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
String word = "";
for (int i = 0; i < str.length(); i++)
{
if (i == 0 || str.charAt(i - 1) == ' ')
{
word += Character.toUpperCase(str.charAt(i));
}
else {
word += str.charAt(i);
}
}
System.out.println(word);
}
}
Output
Enter a sentence:
the x factor is a british music competition series.
The X Factor Is A British Music Competition Series.
17] Write a program to input 15 integer elements in an array and sort them in
ascending order using the bubble sort technique.
Program code;
import java.util.*;
public class question17
{
public static void main(String args[])
{
int i, j, temp;
Scanner sc = new Scanner(System.in);
int arr[] = new int[15];
System.out.println("Enter 15 integers:");
15 | P A G E
for (i = 0; i<=15; i++)
{
arr[i] = sc.nextInt();
for(i = 0; i<14; i++)
{
for(j = 0; j<14-i; j++)
{
Output
Enter 15 integers:
1 2 5 7 32 28 45 0 79 36 69 78 11 48 34
Elements in ascending order are:
0 1 2 5 7 11 28 32 34 36 45 48 69 78 79
18] Write a program to input a sentence and convert it into uppercase and count
and display the total number of words starting with a letter ‘A’.
Program code;
import java.util.*;
public class question18
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
16 | P A G E
System.out.println("Enter a string: ");
String str = in.nextLine();
str = " " + str; //Add space in the begining of str
int c = 0;
int len = str.length();
str = str.toUpperCase();
for (int i = 0; i < len - 1; i++) {
if (str.charAt(i) == ' ' && str.charAt(i + 1) == 'A')
c++;
}
System.out.println("Total number of words starting with letter 'A' = " + c);
}
}
Output
Enter a string:
North America and South America are two continents.
Total number of words starting with letter 'A' = 4
19] Write a program to input integer elements into an array of size 5 and
perform the following operations:
Display largest number from the array.
Display smallest number from the array.
Display sum of all the elements of the array
Program code;
import java.util.*;
public class question19
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int arr[] = new int[5];
System.out.println("Enter 5 numbers:");
for (int i = 0; i < 5; i++)
{
arr[i] = in.nextInt();
}
int min = arr[0], max = arr[0], sum = 0;
17 | P A G E
for (int i = 0; i < arr.length; i++)
{
if (arr[i] < min)
min = arr[i];
sum += arr[i];
}
Output
Enter 5 numbers:
69
28
55
34
35
Largest Number = 69
Smallest Number = 28
Sum = 221
20] Write a program to input five words in an array. Arrange these words in
descending order of alphabets, using selection sort technique. Print the sorted
array.
Program code;
import java.util.*;
public class question20
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String a[] = new String[5];
int n = a.length;
18 | P A G E
System.out.println("Enter 5 Names: ");
for (int i = 0; i < n; i++) {
a[i] = in.nextLine();
}
Output
Enter 5 Names:
zayn
liam
harry
louis
niall
Sorted Names
harry
liam
louis
niall
zayn
*********************************
19 | P A G E