0% found this document useful (0 votes)
22 views32 pages

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views32 pages

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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)
{
[Link]("\t\tPROGRAM TO OVERLOAD METHOD
NUMBER ");
[Link]("\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;
}
[Link]("The Frequency of the digit "+d+" in the number
"+num+" is : "+f);
}
void number(int n1)
{
[Link]("\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;
}
[Link]("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 :

[Link].*;

class Reverse_Array
{
public static void main()
{
Scanner in=new Scanner([Link]);
int a[]=new int[5];
[Link]("\tProgram to print the elements in the reverse order");

for(int i=0;i<5;i++)
{

[Link]("\nEnter the element number "+(i+1)+":");


a[i]=[Link]();
}
[Link]("\nElements in the reverse order ");
for(int i=4;i>=0;i--)
{
[Link](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 [Link].*;

class Sum_Arrays
{
public static void main()// main method
{
Scanner in=new Scanner([Link]);
int a[]=new int[6];
int sum=0;
[Link]("\t\t Program to print the sum of elements");

for(int i=0;i<6;i++)
{
[Link]("\nEnter the element number "+(i+1)+":");
a[i]=[Link]();// Getting input
sum+=a[i];//Finding sum
}
[Link]("\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 [Link].*;

class High_Low
{
public static void main()// main method
{
Scanner in=new Scanner([Link]);
int a[]=new int[5];
[Link]("\t\t Program to find the maximum and the minimum
element.");

for(int i=0;i<5;i++)
{
[Link]("\nEnter the element number "+(i+1)+":");
a[i]=[Link]();// 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];

[Link]("\nMinimum : "+min);
[Link]("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 [Link].*;
class Even_Odd_Array
{
public static void main()// main method
{
Scanner in=new Scanner([Link]);
int a[]=new int[10];
[Link]("\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++)
{
[Link]("\nEnter the element number "+(i+1)+":");
a[i]=[Link]();// 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
}
[Link]("\nNumber of even elements : "+ce);
[Link]("\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 [Link].*;
class Array_Linear_Search
{
public static void main()// main method
{
Scanner in=new Scanner([Link]);
int a[]=new int[10];
[Link]("\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++)
{
[Link]("\nEnter element number "+(i+1)+": ");
a[i]=[Link]();// Receiving input
}
[Link]("\nEnter the element to be searched : ");
int s=[Link]();
int flag=0;
for(int i=0;i<10;i++)// Searching for element
{
if(a[i]==s)
{
flag++;
[Link]("\nThe number "+s+" is found at position "+(i+1));
break;
}
}
if(flag==0)
{
[Link]("\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 [Link].*;
class Array_Binary_Search
{

public static void main()// main method


{
Scanner in=new Scanner([Link]);
int a[]=new int[10];
[Link]("\t\t Program to search for an element using Binary
Search technique");

for(int i=0;i<10;i++)
{
[Link]("\nEnter element number "+(i+1)+": ");
a[i]=[Link]();// Receiving input
}
int l=0,h=9,m=(l+h)/2;
[Link]("\nEnter the element to be searched : ");
int s=[Link]();
int flag=0;
while(l<=h)// Searching for element
{
if(a[m]==s)
{
flag=1;
[Link]("\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)
{
[Link]("\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 [Link].*;
class Palindrome
{
public static void main()
{
String original, reverse = "";
Scanner in = new Scanner([Link]);
[Link]("\tProgram to check whether a string is Palindrome or
not");
[Link]("\nEnter a word to check:");
original = [Link]();
int length = [Link]();
for ( int i = length - 1; i >= 0; i-- )
{
reverse = reverse + [Link](i);
}
if ([Link](reverse))
[Link]("\n"+original+" is palindrome");
else
[Link]("\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 [Link];
class Piglatin
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("\tProgram to convert a word into a PigLatin word");
[Link]("\nEnter a word:");
String n = [Link]();
int p = 0,i;
for(i = 0;i<[Link]();i++)
{
char c = [Link](i);
if(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
break;
}
if(i==0)
[Link] (n);
else
[Link] ("\nPigLatin word:" + [Link](i) + [Link](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 [Link].*;
class Count_Words
{
public static void main()
{
String text;
int countWords=0;
Scanner sc=new Scanner([Link]);
[Link]("\tProgram to count the number of words present in a
string");
[Link]("\nEnter string:");
text=[Link]();
//word count
for(int i=0; i<[Link]()-1; i++)
{
if([Link](i)==' ' && [Link](i+1)!=' ')
countWords++;
}
[Link]("\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 [Link];
class Unique
{
public static void main()
{
[Link]("\t Program to check whether a string is unique or not");
Scanner in = new Scanner([Link]);
[Link]("\nEnter a word:");
String s = [Link]();
int flag=0;
char ch;
for(int i=0;i<[Link]();i++)
{
ch=[Link](i);
if([Link](ch)!=[Link](ch))
{
flag=1;
break;
}
}
if(flag==0)
[Link]("\n"+s+" is a Unique word");
else
[Link]("\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 [Link];
class New_Word
{
public static void main()
{
Scanner in=new Scanner([Link]);
[Link]("\tProgram to print the first letter of each word in the
string");
[Link]("\nEnter any string:");
String str=[Link]();
str=" "+str;
String first="";
[Link]("\nNew Word:");
for(int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if(ch==' ')
{
[Link]([Link](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 [Link].*;
class ASCII
{
public static void main()
{
Scanner in=new Scanner([Link]);
[Link]("\tProgram to find the ASCII value of each character in a
string");
[Link]("\nEnter a word:");
String s=[Link]();
for(int i=0;i<[Link]();i++)
{
[Link]("ASCII value of "+([Link](i))+" =
(int)([Link](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 [Link].*;
class Long_Short
{
public static void main()
{
Scanner in=new Scanner([Link]);
[Link]("\tProgram to print the longest and shortest word in a sentence");
[Link]("\nEnter any sentence:");
String s=[Link]();
s=s+" ";
String max="",min=s,w="";
int index=0;
while(index<[Link]())
{
int position=[Link](' ',index);
w=[Link](index,position);
if([Link]()>[Link]())
max=w;
if([Link]()<[Link]())
min=w;
index=position+1;
w="";
}
[Link]("\nLongest word:"+max+" Length:"+[Link]());
[Link]("Shortest word:"+min+" Length:"+[Link]());
}
}
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 [Link];
class LinearSearch_String
{
public static void main()
{
Scanner in = new Scanner([Link]);
String arr[]=new String[5];
[Link]("\tProgram to search for a string in an array");
for(int i=0;i<5;i++)
{
[Link]("\nEnter the word at position "+(i+1)+":");
arr[i]=[Link]();
}
[Link]("\nEnter the word to be searched:");
String search=[Link]();
int flag=0;
for(int i=0;i<5;i++)
{
if(arr[i].equalsIgnoreCase(search))
{
[Link]("\nThe word is found at the position:"+(i+1));
flag=1;
break;
}
}
if(flag==0)
[Link]("\nWord is not found");
}
}

You might also like