//Q.
1 palindrome String………………………………………………………………………
System.out.println("Enter a String");
String s1 = sc.nextLine();
String newString="";
for(int i=(s1.length()-1);i>=0;i--)
{
newString = newString+s1.charAt(i);
}
if(newString.equals(s1))
{
System.out.println("Reverse String is : "+ newString);
System.out.println("It's a palindrome String");
}
else
{
System.out.println("Reverse String is : "+ newString);
System.out.println("It's not a palindrome String");
}
Output :-
Enter a String
abccba
Reverse String is : abccba
It's a palindrome String
Enter a String
jayesh
Reverse String is : hseyaj
It's not a palindrome String
…………………………………………………………………………………………………………………………………………………………………
Q.2 Blank spaces from string
System.out.println("Enter a String of your choice : ");
String s1 = sc.nextLine();
int count = 0;
for(int i=0;i<=(s1.length()-(1));i++)
{
if(s1.charAt(i)==' ')
{
count++;
}
}
System.out.println(count);
OUTPUT :-
Enter a String of your choice :
j a y e s h
count of space : 5
…………………………………………………………………………………………………………………………………………………………………………….
//Q.3 Occurance of character a in a string
System.out.println("Enter a String of your choice : ");
String s1 = sc.next();
int count = 0;
for(int i=0;i<=(s1.length()-(1));i++)
{
if(s1.charAt(i)=='a')
{
count++;
}
}
System.out.println(count);
Output :-
Enter a String of your choice :
jayeshkumar
count ofcharacter a in string : 2
……………………………………………………………………………………………………………………………………………………………………………………..
//Q.4 Print the number of vowels in string
System.out.println("Enter a String : ");
String s1 = sc.next();
int count = 0;
for(int i =0;i<s1.length();i++)
{
if(s1.charAt(i)=='a' || s1.charAt(i)=='e' || s1.charAt(i)=='i' || s1.charAt(i)=='o' ||
s1.charAt(i)=='u' || s1.charAt(i)=='A' || s1.charAt(i)=='E' || s1.charAt(i)=='I' ||
s1.charAt(i)=='O' || s1.charAt(i)=='U')
{
count++;
}
}
System.out.println(count);
OUTPUT:-
Enter a String :
EUNOIA
5
Enter a String :
eunomia
5
………………………………………………………………………………………………………………………………………………………………………………….
//Q.5 Palindrome number
int reverseNo =0;
int oldNo;
System.out.println("Enter a number");
int num = sc.nextInt();
oldNo = num;
for(;num>0;)
{
int mod = num % 10;
reverseNo = (reverseNo * 10) + mod;
num = num / 10;
}
if(reverseNo == oldNo)
{
System.out.println("It's a palindrome number");
}
else
{
System.out.println("It's not a palindrome number");
}
OUTPUT:-
Enter a number
121
It's a palindrome number
Enter a number
12345
It's not a palindrome number
…………………………………………………………………………………………………………………………………………………………………………………………………….
//Q.6 String reverse program
System.out.println("Enter a String :");
String s1 = sc.nextLine();
String rev = " ";
for(int i = s1.length()-1;i>=0;i--)
{
rev = rev + s1.charAt(i);
}
System.out.println("Reverse of String is : " +rev);
Output:-
Enter a String :
jayeshkumar
Reverse of String is : ramukhseyaj
……………………………………………………………………………………………………………………………………………………………………………………………………..
// Q.8 query to fetch 5 highest salaries
mysql> create database assignments;
Query OK, 1 row affected (0.02 sec)
mysql> use assignments
Database changed
mysql> show tables;
Empty set (0.01 sec)
mysql> desc emp;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| age | int | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| salary | int | YES | | NULL | |
+--------+-------------+------+-----+---------+----------------+
mysql> select * from emp order by salary desc;
+----+------+------+--------+
| id | age | name | salary |
+----+------+------+--------+
| 10 | 61 | jjj | 93000 |
| 7 | 57 | ggg | 91000 |
| 11 | 55 | kkk | 88000 |
| 18 | 64 | rrr | 84000 |
| 6 | 42 | fff | 79000 |
| 16 | 31 | ppp | 71000 |
| 15 | 45 | ooo | 67000 |
| 17 | 39 | qqq | 55000 |
| 21 | 48 | uuu | 53000 |
| 12 | 38 | lll | 49000 |
| 9 | 33 | iii | 46000 |
| 20 | 37 | ttt | 38000 |
| 5 | 31 | eee | 36000 |
| 19 | 33 | sss | 36000 |
| 14 | 30 | nnn | 30000 |
| 13 | 26 | mmm | 25500 |
| 4 | 32 | ddd | 22000 |
| 2 | 27 | bbb | 20000 |
| 8 | 24 | hhh | 18000 |
| 3 | 30 | ccc | 15000 |
| 1 | 25 | aaa | 10000 |
+----+------+------+--------+
//mysql> select * from emp order by salary desc limit 5;
+----+------+------+--------+
| id | age | name | salary |
+----+------+------+--------+
| 10 | 61 | jjj | 93000 |
| 7 | 57 | ggg | 91000 |
| 11 | 55 | kkk | 88000 |
| 18 | 64 | rrr | 84000 |
| 6 | 42 | fff | 79000 |
+----+------+------+--------+
……………………………………………………………………………………………………………………………………………………………………………………………..
//Q.9 How do you reverse 123 = 321
int i =123;
int reverse = 0;
while(i!=0)
{
int mod = i % 10;
reverse = reverse*10 + mod;
i = i / 10;
}
System.out.println("Reverse of 123 : " +reverse);
Output:-
Reverse of 123 : 321
………………………………………………………………………………………………………………………………………………………………………………………………………
//Q.10 Qurey for 1st 10 records from table
mysql> select * from emp order by salary desc;
+----+------+------+--------+
| id | age | name | salary |
+----+------+------+--------+
| 10 | 61 | jjj | 93000 |
| 7 | 57 | ggg | 91000 |
| 11 | 55 | kkk | 88000 |
| 18 | 64 | rrr | 84000 |
| 6 | 42 | fff | 79000 |
| 16 | 31 | ppp | 71000 |
| 15 | 45 | ooo | 67000 |
| 17 | 39 | qqq | 55000 |
| 21 | 48 | uuu | 53000 |
| 12 | 38 | lll | 49000 |
| 9 | 33 | iii | 46000 |
| 20 | 37 | ttt | 38000 |
| 5 | 31 | eee | 36000 |
| 19 | 33 | sss | 36000 |
| 14 | 30 | nnn | 30000 |
| 13 | 26 | mmm | 25500 |
| 4 | 32 | ddd | 22000 |
| 2 | 27 | bbb | 20000 |
| 8 | 24 | hhh | 18000 |
| 3 | 30 | ccc | 15000 |
| 1 | 25 | aaa | 10000 |
+----+------+------+--------+
//Q.10 mysql> select * from emp limit 10;
+----+------+------+--------+
| id | age | name | salary |
+----+------+------+--------+
| 1 | 25 | aaa | 10000 |
| 2 | 27 | bbb | 20000 |
| 3 | 30 | ccc | 15000 |
| 4 | 32 | ddd | 22000 |
| 5 | 31 | eee | 36000 |
| 6 | 42 | fff | 79000 |
| 7 | 57 | ggg | 91000 |
| 8 | 24 | hhh | 18000 |
| 9 | 33 | iii | 46000 |
| 10 | 61 | jjj | 93000 |
+----+------+------+--------+
……………………………………………………………………………………………………………………………………………………………………………………………………
//Q.11 mysql> select * from emp where age > 30 order by age asc limit 10;
+----+------+------+--------+
| id | age | name | salary |
+----+------+------+--------+
| 5 | 31 | eee | 36000 |
| 16 | 31 | ppp | 71000 |
| 4 | 32 | ddd | 22000 |
| 9 | 33 | iii | 46000 |
| 19 | 33 | sss | 36000 |
| 20 | 37 | ttt | 38000 |
| 12 | 38 | lll | 49000 |
| 17 | 39 | qqq | 55000 |
| 6 | 42 | fff | 79000 |
| 15 | 45 | ooo | 67000 |
+----+------+------+--------+
……………………………………………………………………………………………………………………………………………………………………………………………………
Q.13 WAP Encapsulated class with getter and setter method on notepad :-
public class Jayesh
private int id;
private char character;
private String name;
public void setName(int eId)
id = eId;
}
public int getId()
return Id;
public void setName(String eName)
name = eName;
public String getName()
return name;
public void setCharacter(char eCharacter)
character = eCharacter;
public char getCharacter()
return character;
…………………………………………………………………………………………………………………………………………………………………………………………………….
//Q.14 Swapping 2 numbers with & withous third Variable
//With Third Varaible
System.out.println("Enter 1st number : ");
int n1 = sc.nextInt();
System.out.println("Enter 2nd number : ");
int n2 = sc.nextInt();
int n3 = n1;
n1 = n2;
n2 = n3;
System.out.println("1st number : "+n1);
System.out.println("2nd number : "+n2);
Output:
Enter 1st number :
20
Enter 2nd number :
30
1st number : 30
2nd number : 20
………………………………………………………….
//Without Third Variable
System.out.println("Enter 1st number : ");
int num1 = sc.nextInt();
System.out.println("Enter 2nd number : ");
int num2 = sc.nextInt();
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1-num2;
System.out.println("num1 is : "+ num1);
System.out.println("num2 is : "+ num2);
Output:
Enter 1st number :
50
Enter 2nd number :
60
num1 is : 60
num2 is : 50
…………………………………………………………………………………………………………………………………………………………………………………….
//Q.18 Print Frequency of characters in word
System.out.println("Enter a String : ");
String s1 = sc.nextLine();
System.out.println("Enter a character you whose frequency you want : ");
char c = sc.next().charAt(0);
int count = 0;
for(int i =0;i<=s1.length()-1;i++)
{
if(s1.charAt(i)== c)
{
count++;
}
}
System.out.println(count);
Output:
Enter a String :
jayeshkumar
Enter a character you whose frequency you want :
j
1
……………………s
Enter a String :
jayeshkumar
Enter a character you whose frequency you want :
j
1
……………………………………………………………………………………………………………………………………………………..
}