2. Write a program to input a sentence. Find and display the following:
(i) Number of words present in the sentence
(ii)Number of letters present in the sentence
Assume that the sentence has neither included any digit nor a special character.
Write a program to input a sentence. Find and display the following:
(i) Number of words present in the sentence
(ii)Number of letters present in the sentence
Assume that the sentence has neither included any digit nor a special character.
import java.util.*;
public class WordsNLetters
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int wc,lc,l,i;
char ch;
String st;
System.out.println("Enter a sentence:");
st = in.nextLine();
wc=0;
lc=0;
st=st+' ';
l = st.length();
for (i = 0; i < l; i++)
{
ch = st.charAt(i);
if (ch == ' ')
wc++;
else
lc++;
}
System.out.println("No. Of Words = " + wc);
System.out.println("No. Of Letters="+lc);
}
}
Output:
Enter a sentence:
Welcome To BeeOwn
No. Of Words = 3
No. Of Letters=15
3. Write a program in Java to accept a String in upper case and replace all the vowels present in the String with Asterisk (*) sign.
Sample Input: “RELIANCE INDUSTRIES”
Sample output: R*L**NC* *ND*STR**S
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String st,st1;
char ch;
int i,l;
System.out.println("Enter a word or sentence:");
st = in.nextLine();
st=st.toUpperCase();
l = st.length();
st1 = "";
for (i = 0; i < l; i++)
{
ch=st.charAt(i);
if (ch == 'A'||ch == 'E'||ch == 'I'||ch == 'O'||ch == 'U')
st1 = st1 + '*';
else
st1 = st1 + ch;
}
System.out.println("String with vowels removed");
System.out.println(st1);
}
}
Output:
Enter a word or sentence:
RELIANCE INDUSTRIES
String with vowels removed
R*L**NC* *ND*STR**S
4. Write a program in Java to enter a sentence. Frame a word by joining all the first characters of each word of the sentence. Display the word.
Sample Input: INDIAN SPACE AGENCY
Sample Output: ISA
import java.util.*;
public class FirstLetter
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String st, st1;
int l,i;
char ch,ch1;
System.out.println("Enter a name having more than 3 words:");
st = in.nextLine();
st1="";
st = ' '+st;
l = st.length();
for (i = 0; i < l; i++)
{
ch = st.charAt(i);
if (ch ==' ')
{
ch1 = st.charAt(i+1);
st1 = st1+ch1+' ';
}
}
System.out.print("First Letter Of Each Word " + st1);
}
}
Output:
Enter a name having more than 3 words:
INDIAN SPACE AGENCY
First Letter Of Each Word I S A
5. Write a program to accept a list of 20 integers. Sort the first 10 numbers in ascending order and next the 10 numbers in descending order by using ‘Bubble Sort’ technique. Finally, print the complete list of integers.
import java.util.*;
class SwapNums
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i,j,p,t,n;
System.out.println("Enter the no of term");
n= in.nextInt();
int arr[] = new int[n];
p=n/2; //Dividing in two equal half
System.out.print("Enter numbers:");
for (i = 0; i < n; i++)
{
arr[i] = in.nextInt();
}
//Sort first half in ascending order
for (i = 0; i < p - 1; i++)
{
for (j = 0; j < p - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}
//Sort second half in descending order
for (i = 0; i < p - 1; i++)
{
for (j = p; j < n - i - 1; j++)
{
if (arr[j] < arr[j + 1])
{
t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}
System.out.print("After swap list are:");
for (i = 0; i < n; i++)
{
System.out.print(" " + arr[i]);
}
}
}
Output:
Enter the no of term6
Enter 6 numbers:12
25
47
789
10
36
After swap list are:12 2547 789 36 10
6. Write a program in Java to accept 20 numbers in a single dimensional array arr[20]. Transfer and store all the even numbers in an array even[ ] and all the odd numbers in another array odd[ ]. Finally, print the elements of both the arrays.
import java.util.*;
class SwapNums
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i,j,p,t,n,ne,no;
System.out.println("Enter the no of term");
n= in.nextInt();
int arr[] = new int[n];
int even[] = new int[n];
int odd[] = new int[n];
ne=0; //To Count Number Of Odd In Array
no=0; //To Count Number Of Even In Array
System.out.println("Enter numbers:");
for (i = 0; i < n; i++)
{
arr[i] = in.nextInt();
}
for (i = 0; i < n; i++)
{
if(arr[i]%2==0)
{
even[ne++]=arr[i];
}
else
{
odd[no++]=arr[i];
}
}
System.out.println("Array You Entered:");
for (i = 0; i < n; i++)
{
System.out.print(" " + arr[i]);
}
System.out.println();
System.out.println("Even Array:");
for (i = 0; i < ne; i++)
{
System.out.print(" " + even[i]);
}
System.out.println();
System.out.println("Odd Array:");
for (i = 0; i < no; i++)
{
System.out.print(" " + odd[i]);
}
}
}
Output:
Enter the no of term
6
Enter numbers:
12
45
71
93
06
54
Array You Entered:
12 4571 93 6 54
Even Array:
12 6 54
Odd Aarray:
45 7193
7. Write a program to store 20 numbers in a Single Dimensional Array . Now, display only those numbers that are perfect squares.
n[0] | n[1] | n[2] | n[3] | n[4] | n[5] | …. | n[16] | n[17] | n[18] | n[19] |
45 | 65 | 77 | 71 | 90 | 67 | …. | 82 | 19 | 31 | 52 |
import java.util.*;
import java.lang.Math;
class Main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i, p,n,num;
System.out.println("Enter the no of term");
n= in.nextInt();
int arr[] = new int[n];
System.out.println("Enter numbers:");
for (i = 0; i < n; i++)
{
arr[i] = in.nextInt();
}
System.out.println("Perfect Square:");
for (i = 0; i < n; i++)
{
num=arr[i];
p = (int)Math.sqrt(num);
if(p*p==num)
System.out.println(" " + arr[i]);
}
}
}
Output:
Enter the no of term
5
Enter numbers:
36
25
89
81
24
Perfect Square:
36
25
81