Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 71

VirudhunagarT.S.M.

ManickamNadar– JanakiAmmal
SchoolMadurai-625009.

COMPUTERSCIENCE RECORD WORK

2023 –2024

Roll Number:

Name:…………………………………………………….

Class: …………………

Certified to be the Bonafide record of work done by

…………………………………………

during the year 2023 – 2024

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

11. Vowel Count

12. Encryption

13. Word Reversing

14. Longest Word

15. Merge and Sort the String

16. Searching & Sorting

17. Transpose Matrix

18. Sum of diagonals

19. Sum of boundary elements


20. Mirror Image of Matrix

21. NCR using Package

22. Prime using Package

23. Armstrong using Package

24. Read a file

25. Write data to a file


1.Palindrome

import java.util.*;

class palindrome{

public static void main()

Scanner sc = new Scanner(System.in);

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

System.out.println(d+" is not a palindrome number");

}}
1.Palindrome

Output:

Enter a number

313

313 is a palindrome
2.Prime palindrome

import java.util.*;

class Prime_palindrome

public static void main()

Scanner sc = new Scanner(System.in);

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)

System.out.println(n+" is a prime palindrome number");

else

System.out.println(n+" is not a prime palindrome number");

}
2.Prime palindrome

Output:

Enter a number

212

212 is not a prime palindrome number

Enter a number

313

313 is a prime palindrome number


3.Disarium number

import java.util.*;

class Disarium_number

public static void main()

Scanner sc=new Scanner(System.in);

int n= sc.nextInt();int p=n;

String l=""+n;

int length = l.length();

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

System.out.println("Not Disarium Number");

}
3.Disarium number

Output:

175

Disarium Number

123

Not Disarium Number


4.Adam number

import java.util.*;

class Adam_number

public static void main()

Scanner sc= new Scanner(System.in);

System.out.println("Enter a number");

int num=sc.nextInt();

int ogsq=num*num;

int numrev=0;

int temp=num;

while(temp!=0)

int digit =temp%10;

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

System.out.println("Not Adam number");

}
4.Adam number

Enter a number

32

Not Adam number

Enter a number

11

Adam number
5.Perfect square

import java.util.*;

class perfect_square

public static void main()

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number");

int num=sc.nextInt();

double root = Math.sqrt(num);

if((Math.floor(root)-root)==0)

System.out.println(num+" is a perfect number");

else

System.out.println(num+" is not a perfect number");

}
5.Perfect square

Enter a number

36

36 is a perfect number

Enter a number

71

71 is not a perfect number


String programs

1.Vowel count

import java.util.*;

class Count

public static void main()

Scanner sc= new Scanner(System.in);

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

public static void main()

Scanner sc= new Scanner(System.in);

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;
}

else if (ch==' ')

{p=p+" ";}

else

k=(int)ch;

k++;

ch=(char)k;

p=p+ch;

System.out.println(p);

}
2.Encryption

I have the message

ICWG UIG NGTTCHG

Computer applications for standard eleven

QNQWUGS CQQMKDCUKQOT GQS TUCOECSE GMGWGO


3.Word reversing in sentence

import java.util.*;

class Reverse_words_in_sentence

public static void main()

Scanner sc= new Scanner(System.in);

String s=sc.nextLine();

int i;char ch;

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

Mars is smaller than earth

sraM si rellams naht htrae

The racecar is too fast

ehT racecar si oot tsaf


4.Longest word

import java.util.*;

class Longest_word

public static void main()

Scanner sc= new Scanner(System.in);

String s=sc.nextLine();

int i;char ch;String maxword="";

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 is the best

Football 8

madrid is a city

madrid 6
5.Merge string and arrange in alphabetic order

import java.util.*;

class MergeSort

public static void main()

Scanner sc = new Scanner(System.in);

System.out.println("Enter the first string:");

String a = sc.next();

System.out.println("Enter the second string:");

String b = sc.next();

String mergedString = a + b;

char arr[] = new char[mergedString.length()];

for (int i = 0; i < mergedString.length(); i++)

arr[i] = mergedString.charAt(i);

for (int i = 0; i < mergedString.length(); i++)

for (int j = 0; j < mergedString.length() - 1; j++)

if (arr[j] > arr[j + 1])


{

char t = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = t;

System.out.println("Sorted string: ");

for (int i = 0; i < mergedString.length(); i++)

System.out.print(arr[i]);

}
5.Merge string and arrange in alphabetic order

Enter the first string:

Apple

Enter the second string:

Amazing

Sorted string:

AAaegilmnppz
Array program

1.Menu driven program on searching and sorting

import java.util.*;

class menu

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the length of array");

int p= sc.nextInt();

System.out.println("Enter the array");

int arr[]= new int[p];

for(int i=0;i<p;i++)

arr[i]=sc.nextInt();

System.out.println("Enter l for selection sort, s for linear search, S for binary


search ,B for bubble sort");

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':

System.out.print("Enter number to search: ");

int n = sc.nextInt();

int l = 0, h = arr.length - 1, index = -1;

while (l <= h) {

int m = (l + h) / 2;

if (arr[m] < n)

l = m + 1;

else if (arr[m] > n)

h = m - 1;
else {

index = m;

break;

if (index == -1) {

System.out.println("Search element not found");

else {

System.out.println(n + " found at position " + index);

case'S':

int i,lb=0,ub=p-1,y,k=0,ns=0;

System.out.println("Enter the numbers to be searched");

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

System.out.println("search not successful");

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

1.Menu driven program in searching and sorting

Enter the length of array

Enter the array

Enter l for selection sort, s for linear search, S for binary search ,B for bubble sort

Enter the numbers to be searched

search successful
2.Transpose array

import java.util.*;

public class TransposeArray

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("enter the length of rows and columns");

int m=sc.nextInt();

int n=sc.nextInt();

int og[][]=new int[m][n];

System.out.println("enter the array elements");

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

og[i][j]=sc.nextInt();

int tr[][]=new int[n][m];

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

enter the length of rows and columns

enter the array elements

15

26

37

48
3.Find and print the sum of left and right diagonals

import java.util.*;

class Diagonal

public static void main()

Scanner sc = new Scanner(System.in);

System.out.println("enter the length of side of matrix");

int n= sc.nextInt();

int i,j;

int a[][] = new int[n][n];

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)

suml=suml+ a[i][j];//2,0 1,1 0,2

for(i=n-1,j=0;i>=0;i--,j++)

sumr=sumr+a[i][j];

System.out.println("Sum of left diagonal"+suml);

System.out.println("Sum of right diagonal"+sumr);

}
3.Sum of left diagonal and right diagonal

enter the length of side of matrix

Sum of left diagonal 15

Sum of right diagonal 15


4.Find the sum of boundary matrix

import java.util.Scanner;

public class Boundary

public static void main()

Scanner sc = new Scanner(System.in);

System.out.println("Enter the length of rows and columns:");

int m = sc.nextInt();

int n = sc.nextInt();

int arr[][] = new int[m][n];

int sum = 0;

System.out.println("Enter the matrix elements:");

for (int i = 0; i < m; i++)

for (int j = 0; j < n; j++)

arr[i][j] = sc.nextInt();

for (int i = 0; i < m; i++)

sum += arr[i][0];

for (int j = 1; j < n; j++)


sum += arr[0][j];

for (int i = 1; i < m; i++)

sum += arr[i][n - 1];

for (int j = 1; j < n - 1; j++)

sum += arr[m - 1][j];

System.out.println("Sum of boundary elements: " + sum);

}
4.Sum of boundary elements

Enter the length of rows and columns:

Enter the matrix elements:

Sum of boundary elements: 21


5.Mirror Image

import java.util.*;

class mirrorimage

public static void main()

Scanner sc = new Scanner(System.in);

System.out.println("Enter rows and columns");

int n = sc.nextInt();

int m = sc.nextInt();

int[][] OA = new int[n][m];

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.print(OA[i][j] + " ");

System.out.println();

int[][] MA = new int[n][m];

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.print(MA[i][j]+ " ");

System.out.println();

}}
5.Mirror Image

Enter rows and columns

original array

987

654

321

mirror array

789

456

123
Package program

package Program_package;

public class FUNCTIONS

public boolean isprime(int n)

int c=0;

for(int i=1;i<=n;i++)

if(n%i==0)

c++;

if(c==2)

return true;

else

return false;

public double factorial(int n)

double f=1;

for(int i=1;i<=n;i++)

f=f*i;
return f;

public int count(int n)

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

public static void main()

Scanner sc=new Scanner(System.in);

FUNCTIONS ob=new FUNCTIONS();

System.out.println("Enter the value of n and r");

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

Enter the value of n and r....

NCR=3.0

Enter the value of n and r....

10

NCR=210.0
2.Circular Prime using package

import java.util.*;

import Program_package.*;

class Circular_Prime

public static void main()

Scanner sc=new Scanner(System.in);

FUNCTIONS ob=new FUNCTIONS();

System.out.println("Enter the value of n");

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)

System.out.println("Not Circular Prime");

else

System.out.println("Circular Prime");

}
2.Circular prime

Enter the value of n

13

31

13

Circular Prime

Enter the value of n

41

14

41

Not Circular Prime


3.Find Armstrong number using package to find the number of digits

import java.util.*;

import Program_package.*;

class ARMSTRONG

public static void main()

Scanner sc=new Scanner(System.in);

FUNCTIONS ob=new FUNCTIONS();

System.out.println("Enter the value of n....");

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

Enter the value of n

153

ARMSTRONG NUMBER

Enter the value of n

111

NOT AN ARMSTRONG NUMBER


File Program

1.Read a file

import java.util.*;

import java.io.*;

public class Read

public static void main(String args[])throws IOException

FileInputStream fin=null;

int p;

System.out.println("Bytes of file Capital.Txt are: ");

fin = new FileInputStream("Capital.txt");

while((p=fin.read())!=-1)

System.out.print((char)p);

fin.close();

}
2.Write data from file

import java.io.*;

public class write

public static void main(String args[])throws IOException

File intFile= new File("Number.dat");

FileOutputStream fout = new FileOutputStream(intFile);

DataOutputStream ffout=new DataOutputStream(fout);

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in=new BufferedReader(read);

int i,n;

System.out.println("Enter values");

for(i=0;i<10;i++)

n=Integer.parseInt(in.readLine());

ffout.writeInt(n);

ffout.close();

Series and Pattern


1.Pattern-1

class PATTERN_number

public static void main()

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

public static void main()

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

public static void main()

int m=7;int row=1;

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

public static void main()

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of terms");

int n=sc.nextInt();

System.out.println("Enter the value of a ");

int a=sc.nextInt();

double sum=1;

int y=1;

System.out.println("The sequence is:-");

for(int i =1;i<=n;i++)

y=y*i;

if(i%2!=0)

sum = sum + (Math.pow(a,i+1)/y);

else

{
sum = sum - (Math.pow(a,i+1)/y);

System.out.println(sum);

}
4.

Enter the number of terms

Enter the value of a

The sequence is:-

-40.84027777777777
5.sequence-2

import java.util.*;

class pattern_power

public static void main()

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of terms");

int n = sc.nextInt();

double s=0.0;

int k =0;

System.out.println("The sequence is:-");

for(int i =1;i<=n;i++)

s = (Math.pow(i,i)-i);

k = (int)s;

System.out.print(k + ",");

}
5.

Enter the number of terms

The sequence is:-

0,2,24,252,3120,

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy