You are on page 1of 19

COMPUTER

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

2] Write a program to input an array of 10 integers and print those elements


which are even.

Program code;

public class question2


{
public static void display(int ar[])
{
for(int i=0;i<10;i++)
{
if(ar[i]%2==0)
System.out.print(ar[i]+" ");
}
}
}

Output

Entered elements: {60,69,91,100,1,2,5,34,35,28}


60 100 2 34 28

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

SUM OF EVEN NUMBERS FROM ARRAY = 370

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

LARGEST INTEGER FROM THE ARRAY = 132

5] Write a program to initialize a single dimensional array A[] of 10 integers.


Arrange the array in Ascending order using Bubble sort method. Print the array
before and after sorting. Each array should be printed in a single line with
suitable space between them.

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

ARRAY BEFORE SORTING :


132 72 69 55 32 28 27 21 13 10
ARRAY AFTER SORTING :
10 13 21 27 28 32 55 69 72 132

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;

public class question7


{
public void display()
{
for(char ch='A';ch<='Z';ch++)
{
System.out.print(ch+" = " +(int)ch+" ");
}
}
}

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;

public class question8


{
public void display()

7|PAGE
{
for(char ch='a';ch<='z';ch++)
{
System.out.print(ch+" = " +(int)ch+" ");
}
}
}
Output

a = 97 b = 98 c = 99 d = 100 e = 101 f = 102 g = 103 h = 104 i = 105 j = 106 k =


107 l = 108 m = 109 n = 110 o = 111 p = 112 q = 113 r = 114 s = 115 t = 116 u
= 117 v = 118 w = 119 x = 120 y = 121 z = 122

9] Write a program to print ASCII codes of all digits from 0-9 along with the
digits.

Program code;

public class question9


{
public static void main(String[] args)
{
for(int i = 48; i <= 57; i++)
{
System.out.println(" The ASCII value of " + (char)i + " = " + i);
}
}
}

Output

The ASCII value of 0 = 48


The ASCII value of 1 = 49
The ASCII value of 2 = 50
The ASCII value of 3 = 51
The ASCII value of 4 = 52
The ASCII value of 5 = 53
The ASCII value of 6 = 54
The ASCII value of 7 = 55
The ASCII value of 8 = 56
The ASCII value of 9 = 57

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:");

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


System.out.print("Enter City Name: ");
cities[i] = in.nextLine();
System.out.print("Enter its STD Code: ");
stdCodes[i] = in.nextLine();
}

System.out.print("Enter name of city to search: ");


String city = in.nextLine();

int idx;
for (idx = 0; idx < SIZE; idx++) {
if (city.compareToIgnoreCase(cities[idx]) == 0) {
break;
}
}

if (idx < SIZE) {


System.out.println("Search Successful");
System.out.println("City: " + cities[idx]);
System.out.println("STD Code: " + stdCodes[idx]);
}
else {
9|PAGE
System.out.println("Search Unsuccessful");
}
}
}
Output

Enter 5 cities and their STD codes:


Enter City Name: seoul
Enter its STD Code: 8569
Enter City Name: busan
Enter its STD Code: 8945
Enter City Name: ganganam
Enter its STD Code: 693435
Enter City Name: daegu
Enter its STD Code: 89
Enter City Name: ulsan
Enter its STD Code: 69784
Enter name of city to search: seoul

Search Successful
City: seoul
STD Code: 8569

11] Write a program in Java to store 5 words in a Single Dimensional Array.


Display only those words which are Palindrome.

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:");

for (int i = 0; i < words.length; i++) {


words[i] = in.nextLine();
}

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;

for (int j = 0; j < strLen / 2; j++) {


if (str.charAt(j) != str.charAt(strLen - 1 - j)) {
isPalin = false;
break;
}
}
if (isPalin)
System.out.println(words[i]);
}
}
}

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

15] Write a program to accept an alphabet in upper case or in lower case.


Display the next alphabet accordingly.

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++)
{

if(arr[j] > arr[j + 1])


{
temp = arr[j];
arr[j] = arr [j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("Elements in ascending order are:");
for (i = 0; i<15; i++)
{
System.out.print(arr[i]);
}
}
}
}

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];

if (arr[i] > max)


max = arr[i];

sum += arr[i];
}

System.out.println("Largest Number = " + max);


System.out.println("Smallest Number = " + min);
System.out.println("Sum = " + sum);
}
}

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();
}

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


int idx = i;
for (int j = i + 1; j < n; j++) {
if (a[j].compareToIgnoreCase(a[idx]) < 0) {
idx = j;
}
}
String t = a[idx];
a[idx] = a[i];
a[i] = t;
}
System.out.println("Sorted Names");
for (int i = 0; i < n; i++) {
System.out.println(a[i]);
}
}
}

Output

Enter 5 Names:
zayn
liam
harry
louis
niall

Sorted Names
harry
liam
louis
niall
zayn

*********************************

19 | P A G E

You might also like