Comp
Comp
Source Code :
import.java.util.*;
class Reverse_Array
{
public static void main()
{
Scanner in=new Scanner(System.in);
int a[]=new int[5];
System.out.println("\tProgram to print the elements in the reverse order");
for(int i=0;i<5;i++)
{
Source Code:
import java.util.*;
class Sum_Arrays
{
public static void main()// main method
{
Scanner in=new Scanner(System.in);
int a[]=new int[6];
int sum=0;
System.out.println("\t\t Program to print the sum of elements");
for(int i=0;i<6;i++)
{
System.out.print("\nEnter the element number "+(i+1)+":");
a[i]=in.nextInt();// Getting input
sum+=a[i];//Finding sum
}
System.out.println("\nThe sum of the elements are : "+sum);
}
}
OUTPUT
Minimum : 6
Maximum : 30
Program to find the maximum and the minimum element.(22.10.21)
Source Code:
import java.util.*;
class High_Low
{
public static void main()// main method
{
Scanner in=new Scanner(System.in);
int a[]=new int[5];
System.out.println("\t\t Program to find the maximum and the minimum
element.");
for(int i=0;i<5;i++)
{
System.out.print("\nEnter the element number "+(i+1)+":");
a[i]=in.nextInt();// Taking input
}
int min=a[0];
int max=a[0];
for(int i=0;i<5;i++)
{
if(a[i]<min)// Finding the lowest element
min=a[i];
if(a[i]>max)// Finding the highest element
max=a[i];
System.out.println("\nMinimum : "+min);
System.out.println("Maximum : "+max);
}
}
OUTPUT
Source Code:
import java.util.*;
class Even_Odd_Array
{
public static void main()// main method
{
Scanner in=new Scanner(System.in);
int a[]=new int[10];
System.out.println("\t\t Program to count the number of even and odd
elements");
int ce=0;
int co=0;
for(int i=0;i<10;i++)
{
System.out.print("\nEnter the element number "+(i+1)+":");
a[i]=in.nextInt();// Receiving input
}
for(int i=0;i<10;i++)
{
if(a[i]%2==0)
ce++;// Counting number of even elements
else
co++;// Counting number of odd elements
}
System.out.println("\nNumber of even elements : "+ce);
System.out.println("\nNumber of odd elements : "+co);
}
}
OUTPUT
Source Code:
import java.util.*;
class Array_Linear_Search
{
public static void main()// main method
{
Scanner in=new Scanner(System.in);
int a[]=new int[10];
System.out.println("\t\t Program to search for an element using Linear
Search technique");
int ce=0;
int co=0;
for(int i=0;i<10;i++)
{
System.out.print("\nEnter element number "+(i+1)+": ");
a[i]=in.nextInt();// Receiving input
}
System.out.print("\nEnter the element to be searched : ");
int s=in.nextInt();
int flag=0;
for(int i=0;i<10;i++)// Searching for element
{
if(a[i]==s)
{
flag++;
System.out.println("\nThe number "+s+" is found at position "+(i+1));
break;
}
}
if(flag==0)
{
System.out.println("\nThe number "+s+" is not found ");
}
}
}
OUTPUT
Source Code:
import java.util.*;
class Array_Binary_Search
{
for(int i=0;i<10;i++)
{
System.out.print("\nEnter element number "+(i+1)+": ");
a[i]=in.nextInt();// Receiving input
}
int l=0,h=9,m=(l+h)/2;
System.out.print("\nEnter the element to be searched : ");
int s=in.nextInt();
int flag=0;
while(l<=h)// Searching for element
{
if(a[m]==s)
{
flag=1;
System.out.println("\nThe number "+s+" is found at position
"+(m+1));
break;
}
else if(a[m]<s)
l=m+1;
else
h=m-1;
m=(l+h)/2;
}
if(flag==0)
{
System.out.println("\nThe number "+s+" is not found ");
}
}
}
OUTPUT
racecar is palindrome
Program to check whether a string is Palindrome or not
Source code:
import java.util.*;
class Palindrome
{
public static void main()
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("\tProgram to check whether a string is Palindrome or
not");
System.out.print("\nEnter a word to check:");
original = in.next();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
{
reverse = reverse + original.charAt(i);
}
if (original.equals(reverse))
System.out.println("\n"+original+" is palindrome");
else
System.out.println("\n"+original+" is not palindrome");
}
}
OUTPUT
Program to convert a word into a PigLatin word
Enter a word:TROUBLE
PigLatin word:OUBLETRAY
Program to convert a word into PigLatin word
Source code:
import java.util.Scanner;
class Piglatin
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("\tProgram to convert a word into a PigLatin word");
System.out.print("\nEnter a word:");
String n = in.next();
int p = 0,i;
for(i = 0;i<n.length();i++)
{
char c = n.charAt(i);
if(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
break;
}
if(i==0)
System.out.println (n);
else
System.out.println ("\nPigLatin word:" + n.substring(i) + n.substring(0,i) +"AY");
}
}
OUTPUT
Program to count the number of words present in a string
Source code:
import java.util.*;
class Count_Words
{
public static void main()
{
String text;
int countWords=0;
Scanner sc=new Scanner(System.in);
System.out.println("\tProgram to count the number of words present in a
string");
System.out.print("\nEnter string:");
text=sc.nextLine();
//word count
for(int i=0; i<text.length()-1; i++)
{
if(text.charAt(i)==' ' && text.charAt(i+1)!=' ')
countWords++;
}
System.out.println("\nTotal number of words:"+ (countWords+1));
}
}
OUTPUT
Program to check whether a string is unique or not
Source code:
import java.util.Scanner;
class Unique
{
public static void main()
{
System.out.println("\t Program to check whether a string is unique or not");
Scanner in = new Scanner(System.in);
System.out.print("\nEnter a word:");
String s = in.next();
int flag=0;
char ch;
for(int i=0;i<s.length();i++)
{
ch=s.charAt(i);
if(s.indexOf(ch)!=s.lastIndexOf(ch))
{
flag=1;
break;
}
}
if(flag==0)
System.out.println("\n"+s+" is a Unique word");
else
System.out.println("\n"+s+" is not a Unique word");
}
}
OUTPUT
Program to print the first letter of each word in the string
Source code:
import java.util.Scanner;
class New_Word
{
public static void main()
{
Scanner in=new Scanner(System.in);
System.out.println("\tProgram to print the first letter of each word in the
string");
System.out.print("\nEnter any string:");
String str=in.nextLine();
str=" "+str;
String first="";
System.out.print("\nNew Word:");
for(int i=0;i<str.length();i++)
{
char ch=str.charAt(i);
if(ch==' ')
{
System.out.print(str.charAt(i+1));
}
}
}
}
OUTPUT
Source code:
import java.util.*;
class ASCII
{
public static void main()
{
Scanner in=new Scanner(System.in);
System.out.println("\tProgram to find the ASCII value of each character in a
string");
System.out.print("\nEnter a word:");
String s=in.next();
for(int i=0;i<s.length();i++)
{
System.out.println("ASCII value of "+(s.charAt(i))+" =
(int)(s.charAt(i)));;
}
}
}
OUTPUT
Program to print the longest and shortest word in a sentence
Enter any sentence: Hardships often prepare ordinary people for an extraordinary destiny
Source code:
import java.util.*;
class Long_Short
{
public static void main()
{
Scanner in=new Scanner(System.in);
System.out.println("\tProgram to print the longest and shortest word in a sentence");
System.out.print("\nEnter any sentence:");
String s=in.nextLine();
s=s+" ";
String max="",min=s,w="";
int index=0;
while(index<s.length())
{
int position=s.indexOf(' ',index);
w=s.substring(index,position);
if(w.length()>max.length())
max=w;
if(w.length()<min.length())
min=w;
index=position+1;
w="";
}
System.out.println("\nLongest word:"+max+" Length:"+max.length());
System.out.println("Shortest word:"+min+" Length:"+min.length());
}
}
OUTPUT
Program to search for a string in an array