Programs
Programs
Programs
ManickamNadar– JanakiAmmal
SchoolMadurai-625009.
2023 –2024
Roll Number:
Name:…………………………………………………….
Class: …………………
…………………………………………
Teacher in charge
S.No Content Sign
1. Palindrome
2. Prime Palindrome
3. Disarium Number
4. Adam Number
5. Perfect Square
6. Pattern I
7. Pattern II
8. Pattern III
9. Series I
10. Series II
12. Encryption
import java.util.*;
class palindrome{
int a,b,c,d;
System.out.println("Enter a number");
a=sc.nextInt();
b=0;
d=a;
do
c=a%10;
b=b*10+c;
a=a/10;
while (a!=0);
if(b==d)
System.out.println(d+" is a palindrome");
else
}}
1.Palindrome
Output:
Enter a number
313
313 is a palindrome
2.Prime palindrome
import java.util.*;
class Prime_palindrome
int c=0,n,rev=0,s=0,r;
System.out.println("Enter a number");
n=sc.nextInt();
for(int i=1;i<=n;i++)
if(n%i==0)
c++;
if(c==2)
rev=n;
while(rev>0)
r=rev%10;
s=s*10+r;
rev=rev/10;
}
if(n==s)
else
}
2.Prime palindrome
Output:
Enter a number
212
Enter a number
313
import java.util.*;
class Disarium_number
String l=""+n;
int i,indx=0;
int sum=0;
for (i=length;i>0;i--,n=n/10)
sum=sum+(int)(Math.pow(n%10,i));
if(sum==p)
System.out.println("Disarium Number");
else
}
3.Disarium number
Output:
175
Disarium Number
123
import java.util.*;
class Adam_number
System.out.println("Enter a number");
int num=sc.nextInt();
int ogsq=num*num;
int numrev=0;
int temp=num;
while(temp!=0)
numrev=numrev*10+digit;
temp=temp/10;
temp=ogsq;
int sqrev=0;
while(temp!=0)
int digit=temp%10;
sqrev=sqrev*10+digit;
temp=temp/10;
if(numrev*numrev==sqrev)
System.out.println("Adam number");
else
}
4.Adam number
Enter a number
32
Enter a number
11
Adam number
5.Perfect square
import java.util.*;
class perfect_square
System.out.println("Enter a number");
int num=sc.nextInt();
if((Math.floor(root)-root)==0)
else
}
5.Perfect square
Enter a number
36
36 is a perfect number
Enter a number
71
1.Vowel count
import java.util.*;
class Count
String str=sc.nextLine();
int vowels=0,consonant=0,specialchar=0,digit=0;
for(int i=0;i<str.length();i++)
char ch=str.charAt(i);
if("AEIOUaeiou".indexOf(ch)!=-1)
vowels++;
System.out.println(“vowels =”+vowels);
}
String programs
1.Vowel count
amazing
vowels=3
marvelous
vowels=4
2.Encryption
import java.util.*;
class Encryption
String s=sc.nextLine();
s=s.toUpperCase();
int i;
int l=s.length();
char ch;
int k=0;
String p="";
for(i=1;i<l;i++)
ch=s.charAt(i);
if("AEIOUaeiou".indexOf(ch)!=-1)
k=(int)ch;
k+=2;
ch=(char)k;
p=p+ch;
}
{p=p+" ";}
else
k=(int)ch;
k++;
ch=(char)k;
p=p+ch;
System.out.println(p);
}
2.Encryption
import java.util.*;
class Reverse_words_in_sentence
String s=sc.nextLine();
s=s+" ";
String nword="";
String nsentence="";
for(i=0;i<s.length();i++)
ch= s.charAt(i);
if(ch!=' ')
nword=ch+nword;
else
nsentence=nsentence+nword+" ";
nword="";
}
System.out.println(nsentence);
}
3.Word reversing in sentence
import java.util.*;
class Longest_word
String s=sc.nextLine();
s=s+" ";
int max=0;
String nword="";
String nsentence="";
for(i=0;i<s.length()-1;i++)
ch= s.charAt(i);
if(ch!=' ')
nword=nword+ch;
else
if(max<nword.length())
{
max=nword.length();
maxword=nword;
nword="";
System.out.println(maxword+" "+max);
}}
4.Longest word
Football 8
madrid is a city
madrid 6
5.Merge string and arrange in alphabetic order
import java.util.*;
class MergeSort
String a = sc.next();
String b = sc.next();
String mergedString = a + b;
arr[i] = mergedString.charAt(i);
char t = arr[j];
arr[j + 1] = t;
System.out.print(arr[i]);
}
5.Merge string and arrange in alphabetic order
Apple
Amazing
Sorted string:
AAaegilmnppz
Array program
import java.util.*;
class menu
int p= sc.nextInt();
for(int i=0;i<p;i++)
arr[i]=sc.nextInt();
char ch=sc.next().charAt(0);
switch(ch)
case'l':
int t,i,j,min;
for(i=0;i<p-1;i++)
{
min=i;
for(j=i+1;j<p;j++)
if(arr[j]<arr[min])
min=j;
t=arr[i];
arr[i]=arr[min];
arr[min]=t;
case's':
int n = sc.nextInt();
while (l <= h) {
int m = (l + h) / 2;
if (arr[m] < n)
l = m + 1;
h = m - 1;
else {
index = m;
break;
if (index == -1) {
else {
case'S':
int i,lb=0,ub=p-1,y,k=0,ns=0;
ns=sc.nextInt();
while(lb<=ub)
y=(lb+ub)/2;
if(arr[y]<ns)
lb=p+1;
if(arr[y]>ns)
ub=y-1;
if(arr[y]==ns)
k=1;
break;
if(k==1)
System.out.println("search successful");
else
case 'B':
for(int i=0;i<p;i++)
for(int j=0;j<p-1;j++)
if(arr[j]>arr[j+1]);
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
Array programs
Enter l for selection sort, s for linear search, S for binary search ,B for bubble sort
search successful
2.Transpose array
import java.util.*;
int m=sc.nextInt();
int n=sc.nextInt();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
og[i][j]=sc.nextInt();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
tr[j][i]=og[i][j];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
System.out.print(tr[i][j]+" ");
System.out.println();
}
2.Transpose array
15
26
37
48
3.Find and print the sum of left and right diagonals
import java.util.*;
class Diagonal
int n= sc.nextInt();
int i,j;
int sumr=0;
int suml=0;
for (i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=sc.nextInt();
for(i=0;i<n;i++)
{
for (j=0;j<n;j++)
if(i==j)
for(i=n-1,j=0;i>=0;i--,j++)
sumr=sumr+a[i][j];
}
3.Sum of left diagonal and right diagonal
import java.util.Scanner;
int m = sc.nextInt();
int n = sc.nextInt();
int sum = 0;
arr[i][j] = sc.nextInt();
sum += arr[i][0];
}
4.Sum of boundary elements
import java.util.*;
class mirrorimage
int n = sc.nextInt();
int m = sc.nextInt();
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
OA[i][j]= sc.nextInt();
System.out.println("original array");
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
System.out.println();
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
MA[i][j]= OA[i][m-j-1];
System.out.println("mirror array");
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
System.out.println();
}}
5.Mirror Image
original array
987
654
321
mirror array
789
456
123
Package program
package Program_package;
int c=0;
for(int i=1;i<=n;i++)
if(n%i==0)
c++;
if(c==2)
return true;
else
return false;
double f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
int c=0;
while(n>0)
c++;
n=n/10;
return c;
}
1.Calculate NCR using factorial package
import java.util.*;
import Program_package.*;
class NCR
int n=sc.nextInt();
int r=sc.nextInt();
double ncr;
ncr=ob.factorial(n)/(ob.factorial(r)*ob.factorial(n-r));
System.out.println("NCR="+ncr);
}
Package programs
1.NCR
NCR=3.0
10
NCR=210.0
2.Circular Prime using package
import java.util.*;
import Program_package.*;
class Circular_Prime
int n=sc.nextInt();
int k=n;
int c=ob.count(n);
String t=Integer.toString(n);
int f=0;
for(int i=1;i<=c;i++)
t=t.substring(1)+t.charAt(0);
int p=Integer.parseInt(t);
System.out.println(p);
if(ob.isprime(p)==false)
f=1;
}
if(f==1)
else
System.out.println("Circular Prime");
}
2.Circular prime
13
31
13
Circular Prime
41
14
41
import java.util.*;
import Program_package.*;
class ARMSTRONG
int n=sc.nextInt();
int m=n;
int c=ob.count(n);
int sum=0;
while(n>0)
int d=n%10;
sum=sum+(int)(Math.pow(d,c));
n=n/10;
if(m==sum)
System.out.println("ARMSTRONG NUMBER...");
else
System.out.println("NOT AN ARMSTRONG NUMBER....");
}
3.Armstrong number
153
ARMSTRONG NUMBER
111
1.Read a file
import java.util.*;
import java.io.*;
FileInputStream fin=null;
int p;
while((p=fin.read())!=-1)
System.out.print((char)p);
fin.close();
}
2.Write data from file
import java.io.*;
int i,n;
System.out.println("Enter values");
for(i=0;i<10;i++)
n=Integer.parseInt(in.readLine());
ffout.writeInt(n);
ffout.close();
class PATTERN_number
int k=1;
for(int i=5;i>=1;i--)
for(int j=1;j<=i;j++)
System.out.print(k+" ");
System.out.println();k++;
k=4;
for(int i=2;i<=5;i++)
for(int j=1;j<=i;j++)
System.out.print(k+" ");
System.out.println();k--;
}
Pattern and series
1.Output:-
11111
2222
333
44
44
333
2222
11111
2.Pattern-2
class PATTERN_a
char ch='a';
int m=7;
for(int i=1;i<=5;i+=2)
for(int sp=1;sp<=m;sp++)
System.out.print(" ");
for(int j=1;j<=i;j++)
System.out.print(ch+" ");
System.out.println();
m-=2;
m+=4;
for(int i=3;i>=1;i-=2)
for(int sp=1;sp<=m;sp++)
System.out.print(" ");
for(int j=1;j<=i;j++)
System.out.print(ch+" ");
System.out.println();
m+=2;
}
2. Output:-
aaa
aaaaa
aaa
a
3.Patern-3
class PATTERN_pyramid
for(int i=7;i>=1;i-=2)
int k=1,p=2;
for(int sp=1;sp<=m;sp++)
System.out.print(" ");
for(int j=1;j<=i;j++)
if(row%2!=0)
{System.out.print(k+" ");k+=2;}
else
{System.out.print(p+" ");p+=2;}
m+=2;
row++;
System.out.println();
}}
3. Output:-
1 3 5 7 9 11 13
2 4 6 8 10
135
2
4.Sequence-1
import java.util.*;
class sequence
int n=sc.nextInt();
int a=sc.nextInt();
double sum=1;
int y=1;
for(int i =1;i<=n;i++)
y=y*i;
if(i%2!=0)
else
{
sum = sum - (Math.pow(a,i+1)/y);
System.out.println(sum);
}
4.
-40.84027777777777
5.sequence-2
import java.util.*;
class pattern_power
int n = sc.nextInt();
double s=0.0;
int k =0;
for(int i =1;i<=n;i++)
s = (Math.pow(i,i)-i);
k = (int)s;
System.out.print(k + ",");
}
5.
0,2,24,252,3120,