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

Comp

The document contains code snippets for several Java programs that perform array operations like reversing elements, finding sum and maximum/minimum. It also includes programs to count even/odd elements, search using linear/binary search, check palindromes and convert words to Pig Latin.

Uploaded by

Neeil Shetty
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Comp

The document contains code snippets for several Java programs that perform array operations like reversing elements, finding sum and maximum/minimum. It also includes programs to count even/odd elements, search using linear/binary search, check palindromes and convert words to Pig Latin.

Uploaded by

Neeil Shetty
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

OUTPUT :

PROGRAM TO OVERLOAD METHOD NUMBER


PROGRAM TO FIND THE FREQUENCY OF DIGITS
The Frequency of the digit 5556453 in the number 5 is : 4
PROGRAM TO DISPLAY SUM OF EVEN DIGITS OF A NUMBER
The sum of the even digits of the number is : 12
//Program to overload Method number (30.06.2021)
class Overloading
{
void number(int num,int d)
{
System.out.println("\t\tPROGRAM TO OVERLOAD METHOD
NUMBER ");
System.out.println("\tPROGRAM TO FIND THE FREQUENCY OF
DIGITS ");
int f=0;
int temp=num;
while(temp!=0)
{
int r = temp%10;
if (r==d)
f+=1;
temp=temp/10;
}
System.out.println("The Frequency of the digit "+d+" in the number
"+num+" is : "+f);
}
void number(int n1)
{
System.out.println("\tPROGRAM TO DISPLAY SUM OF EVEN DIGITS
OF A NUMBER ");
int sum=0;
while(n1!=0)
{
int r = n1%10;
if(r%2==0)
sum=sum+r;
n1=n1/10;
}
System.out.println("The sum of the even digits of the number is : "+sum);
}
}
OUTPUT

Program to print the elements in the reverse order

Enter the element number 1:6

Enter the element number 2:15

Enter the element number 3:24

Enter the element number 4:33

Enter the element number 5:42

Elements in the reverse order


42
33
24
15
6
Program to print the elements in the reverse order.(21.10.21)

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++)
{

System.out.print("\nEnter the element number "+(i+1)+":");


a[i]=in.nextInt();
}
System.out.println("\nElements in the reverse order ");
for(int i=4;i>=0;i--)
{
System.out.println(a[i]);
}
}
}
OUTPUT

Program to print the sum of elements

Enter the element number 1:4

Enter the element number 2:8

Enter the element number 3:12

Enter the element number 4:16

Enter the element number 5:20

Enter the element number 6:24

The sum of the elements are : 84


Program to print the sum of elements (21.10.21)

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

Program to find the maximum and the minimum element.

Enter the element number 1:6

Enter the element number 2:12

Enter the element number 3:18

Enter the element number 4:24

Enter the element number 5:30

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

Program to count the number of even and odd elements

Enter the element number 1:5

Enter the element number 2:10

Enter the element number 3:15

Enter the element number 4:20

Enter the element number 5:25

Enter the element number 6:30

Enter the element number 7:35

Enter the element number 8:40

Enter the element number 9:45

Enter the element number 10:50

Number of even elements : 5

Number of odd elements : 5


Program to count the number of even and odd elements(22.10.21)

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

Program to search for an element using Linear Search technique

Enter element number 1: 2

Enter element number 2: 4

Enter element number 3: 6

Enter element number 4: 8

Enter element number 5: 10

Enter element number 6: 12

Enter element number 7: 13

Enter element number 8: 16

Enter element number 9: 18

Enter element number 10: 20

Enter the element to be searched : 6

The number 6 is found at position 3


Program to search for an element using Linear Search technique(22.10.21)

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

Program to search for an element using Binary Search technique

Enter element number 1: 3

Enter element number 2: 6

Enter element number 3: 9

Enter element number 4: 12

Enter element number 5: 15

Enter element number 6: 18

Enter element number 7: 21

Enter element number 8: 24

Enter element number 9: 27

Enter element number 10: 30

Enter the element to be searched : 24

The number 24 is found at position 8


Program to search for an element using Binary Search technique(26.10.21)

Source Code:

import java.util.*;
class Array_Binary_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 Binary
Search technique");

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

Program to check whether a string is Palindrome or not

Enter a word to check: racecar

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

Enter string: Programs in Java

Total number of words: 3


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

Enter a word: computer

computer is a Unique word


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

Enter any string: Vital Information Resource Under Seize

New Word: VIRUS


Program to print the form a new word by joining 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

Program to find the ASCII value of each character in a string


Enter a word: BLUEJ
ASCII value of B = 66
ASCII value of L = 76
ASCII value of U = 85
ASCII value of E = 69
ASCII value of J = 74
Program to print the ASCII value of each character in a string

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

Longest word: extraordinary Length: 13


Shortest word: an Length: 2
Program to print the longest and shortest word in a sentence

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

Enter the word at position 1: square

Enter the word at position 2: rectangle

Enter the word at position 3: triangle

Enter the word at position 4: circle

Enter the word at position 5: sphere

Enter the word to be searched: triangle

The word is found at the position: 3


Program to search for a string in an array
Source code:
import java.util.Scanner;
class LinearSearch_String
{
public static void main()
{
Scanner in = new Scanner(System.in);
String arr[]=new String[5];
System.out.println("\tProgram to search for a string in an array");
for(int i=0;i<5;i++)
{
System.out.print("\nEnter the word at position "+(i+1)+":");
arr[i]=in.next();
}
System.out.print("\nEnter the word to be searched:");
String search=in.next();
int flag=0;
for(int i=0;i<5;i++)
{
if(arr[i].equalsIgnoreCase(search))
{
System.out.println("\nThe word is found at the position:"+(i+1));
flag=1;
break;
}
}
if(flag==0)
System.out.println("\nWord is not found");
}
}

You might also like