Solved Programs On Library Class
Solved Programs On Library Class
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
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);
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
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();
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);
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);
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");
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;
}
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*
aaaaa
bbbbb
AAAAA
BBBBB
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
Program 14
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