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

Solved Programs On Library Class

This document contains 14 Java programs that demonstrate various operations on characters and strings. Program 1 takes a character input and outputs the 10th character in the ASCII table. Program 2 outputs the next 5 characters after an input character. Program 3 generates alternate letters from A to Z. The remaining programs perform tasks like counting vowels/consonants, mapping numbers to letters, comparing character codes, concatenating numbers, generating letter patterns, and encoding characters. The programs provide examples of character manipulation in Java using concepts like ASCII codes, loops, conditionals, and string operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Solved Programs On Library Class

This document contains 14 Java programs that demonstrate various operations on characters and strings. Program 1 takes a character input and outputs the 10th character in the ASCII table. Program 2 outputs the next 5 characters after an input character. Program 3 generates alternate letters from A to Z. The remaining programs perform tasks like counting vowels/consonants, mapping numbers to letters, comparing character codes, concatenating numbers, generating letter patterns, and encoding characters. The programs provide examples of character manipulation in Java using concepts like ASCII codes, loops, conditionals, and string operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

COMPUTER APPLICATION

CLASS X
Library classes
Solved programs
Program 1
Write a program in Java to input a character. Find and display the next 10th
character in the ASCII table.
import java.util.*;
public class Q1
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = in.next().charAt(0);
char ch1 = (char)((int)ch + 10);
System.out.println("Tenth character from "
+ ch + " is " + ch1);
}
}
Program 2

Write a program in Java to input a character. Display next 5 characters.

import java.util.*;
public class Q2
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = in.next().charAt(0);

System.out.println("Next 5 characters from " + ch + " are:");

for (int i = 1; i <= 5; i++)


{
++ch;
System.out.println(ch);
}
}
}
Program 3

Write a program in Java to generate all the alternate letters in the range of
letters from A to Z.

public class Q3
{
public static void main(String args[])
{
for (int ch = 'A'; ch <= 'Z'; ch = (ch + 2))
{
System.out.println((char)ch);
}
}
}
Program 4

Write a program to input a set of 20 letters. Convert each letter into upper
case. Find and display the number of vowels and number of consonants
present in the set of given letters.

import java.util.*;

public class Q4
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter any 20 letters");
int v = 0, con = 0;
for (int i = 1; i <=20; i++) {
char ch = sc.next().charAt(0);
ch = Character.toUpperCase(ch);
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
v++;
else

con++;
}
System.out.println("Number of Vowels = " + v);
System.out.println("Number of Consonants = " + con);
}
}
Program 5

Write a program in Java to accept an integer number N such that 0<N<27.


Display the corresponding letter of the alphabet (i.e. the letter at position N).
[Hint: If N =1 then display A]

import java.util.*;
public class Q5
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter integer No.: ");
int n = sc.nextInt();

if (n > 0 && n < 27) {


char ch = (char)(n + 64);
System.out.println("Corresponding letter = " + ch);
}
else {
System.out.println(" Enter a number in 1 to 26 range");
}
}
}
Program 6

Write a program to input two characters from the keyboard. Find the
difference (d) between their ASCII codes. Display the following messages:
If d=0 : both the characters are same.
If d<0 : first character is smaller.
If d>0 : second character is smaller.
Sample Input :
D
P
Sample Output :
d= (68-80) = -12
First character is smaller

import java.util.*;
public class Q6
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter 1st character: ");
char c1 = in.next().charAt(0);
System.out.print("Enter 2nd character: ");
char c2 = in.next().charAt(0);

int d = (int)c1 - (int)c2;


if (d > 0)
System.out.println("Second character is smaller");
else if (d < 0)
System.out.println("First character is smaller");
else
System.out.println("Both the characters are same");
}
}
Program 7

Write a program to input a set of any 10 integer numbers. Find the sum and
product of the numbers. Join the sum and product to form a single number.
Display the concatenated number.
[Hint: let sum=245 and product = 1346 then the number after joining sum
and product will be 2451346]

import java.util.*;

public class Q7
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter 10 nos.");
long sum = 0, p = 1;String s; long r;
for (int i = 1; i <=10; i++) {
int n = in.nextInt();
sum =sum+ n;
p =p* n;
}

s = Long.toString(sum) + Long.toString(p);
r = Long.parseLong(s);

System.out.println("Concatenated Number = " + r);


}
}
Program 8

Write a menu driven program to generate the upper case letters from Z to A
and lower case letters from 'a' to 'z' as per the user's choice.
Enter '1' to display upper case letters from Z to A and Enter '2' to display
lower case letters from a to z.
import java.util.*;
public class Q8
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter '1' to display upper case letters from Z to A");
System.out.println("Enter '2' to display lower case letters from a to z");

System.out.print("Enter your choice: ");


int ch = sc.nextInt();
int c = 0;char p;

switch (ch) {

case 1:
for (int i = 'Z'; i >='A'; i--) {

System.out.print((char)i+" ");

break;

case 2:
for (int i = 97; i <=122; i++) {
p = (char)i;
System.out.print(p+" ");

break;

default:
System.out.println("wrong Choice");
}
}
}
Program 9

Write a program to input a letter. Find its ASCII code. Reverse the ASCII code
and display the equivalent character.
Sample Input: Y
Sample Output: ASCII Code = 89
Reverse the code = 98
Equivalent character: b

import java.util.Scanner;
public class Q9
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a letter: ");
char l = in.next().charAt(0);

int a = (int)l;
System.out.println("ASCII Code = " + a);

int s = 0;
while (a > 0) {
int d = a % 10;
s= s * 10 + d;
a /= 10;
}

System.out.println("Reverse Code = " + s);


System.out.println("Equivalent character = " + (char)s);
}
}
Program 10

Write a menu driven program to display


(i) first five upper case letters
(ii) last five lower case letters as per the user's choice.
Enter '1' to display upper case letters and enter '2' to display lower case
letters.
import java.util.*;

public class Q10


{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter '1' to display upper case letters");
System.out.println("Enter '2' to display lower case letters");

System.out.print("Enter your choice: ");


int ch = sc.nextInt();

switch (ch) {
case 1:
for (int i = 65; i <= 69; i++)
System.out.println((char)i);
break;

case 2:
for (int i = 118; i <= 122; i++)
System.out.println((char)i);
break;

default:
break;
}
}
}
Program 11(i)

A
ab
ABC
abcd
ABCDE
public class Q11_i
{
public static void main(String args[])
{
for (int i = 65; i <=69; i++)
{
for (int j = 65; j <= i; j++)
{
if (i % 2 == 0)
System.out.print((char)(j+32));
else
System.out.print((char)j);
}
System.out.println();
}
}
}

Program 11(ii)

ZYXWU
ZYXW
ZYX
ZY
Z
public class Q11_ii
{
public static void main(String args[])
{
for (int i = 86; i <= 90; i++)
{
for (int j = 90; j >= i; j--)
{
System.out.print((char)j);
}
System.out.println();
}
}
}

Program 11(iii)

ABCDE
ABC
A
public class Q11_iii
{
public static void main(String args[])
{
for (int i = 69; i >= 65; i = i - 2)
{
for (int j = 65; j <= i; j++)
{
System.out.print((char)j);
}
System.out.println();
}
}
}
Program 11(iv)

PRTV
PRT
PR
P
public class Q11_iv
{
public static void main(String args[])
{
for (int i = 86; i >= 80; i = i- 2)
{
for (int j = 80; j <= i; j = j + 2)
{
System.out.print((char)j);
}
System.out.println();
}
}
}

Program 11(v)

A*B*C*D*E*
A*B*C*D*
A*B*C*
A*B*
A*

public class Q11_v


{
public static void main(String args[])
{
for(int i = 69; i >= 65; i--)
{
for (int j = 65; j <= i; j++)
{
System.out.print((char)j + "*");
}
System.out.println();
}
}
}
Program 11(vi)

aaaaa
bbbbb
AAAAA
BBBBB

public class Q11_vi


{
public static void main(String args[])
{
int a = 97;
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 5; j++)
{
System.out.print((char)a + " ");
}
a++;
if (i == 2)
a = 65;
System.out.println();
}
}
}
Program 12

To generate the given pattern


A
AB
ABC
ABCD
ABCDE

class Q12
{
public static void main()
{
int i,j;
for(i=65;i<=69;i++)
{
for(j=65;j<=i;j++)
System.out.print((char)j);
System.out.println();
}}}
Program 13

Write a program in java to input a letter. If it is an upper case letter then


encode it by next 5th letter, otherwise encode it with 3rd previous letter in
the ASCII table
import java.util.*;
public class Q13
{
public static void main()
{
Scanner in=new Scanner(System.in);
char c,ch;
int t;
System.out.println("Enter a letter");
c=in.next().charAt(0);
if(Character.isUpperCase(c))
t=(int)c+5;
else
t=(int)c-3;
ch=(char)t;
System.out.println("Encoded letter is\t"+ch);
}}

Program 14

To generate 10 random characters between A and J randomly


class Q14
{
public static void main()
{
char c;
int n;
System.out.println("The random chracters are;");
for(int i=1;i<=10;i++)
{
n=(int)(Math.random()*9)+1;
c=(char)(n+64);
System.out.println(c);
}}}

Program 15

Write a program to input a set of N characters. Find and display the number
of upper case and lower case characters. assume that none of the characters
is a digit or a special character.
import java.util.*;
class Q15
{
public static void main()
{
Scanner in=new Scanner(System.in);
char c;
int i,n,nuc=0,nlc=0;
System.out.println("Enter the value of N");
n=in.nextInt();
System.out.println("Enter"+n+ "characters");
for(i=1;i<=n;i++)
{
c=in.next().charAt(0);
if(Character.isUpperCase(c))
nuc++;
else
nlc++;
}
System.out.println("Numbers of upper case characters\t"+nuc);
System.out.println("Numbers of lower case characters\t"+nlc);
}}
Program 16

Program in Java to print the following pattern:


ABCDE
BCDE
CDE
DE
E
class Q16
{ public static void main(String args[])
{
int i,j,sum=0;
for(i='A';i<='E';i++)
{
for(j=i;j<='E';j++)
{
System.out.print((char)j+" ");
}
System.out.println();
}
}
}

You might also like