Professional Documents
Culture Documents
Name: Jai Anand Class: 12 E Uid: 7098107 Subject: Computer School: CMS, Mahanagar
Name: Jai Anand Class: 12 E Uid: 7098107 Subject: Computer School: CMS, Mahanagar
CLASS: 12 E
UID: 7098107
SUBJECT: COMPUTER
SCHOOL:
CMS,MAHANAGAR
ACKNOWLEDGEMENT
I would like to express my special thanks of gratitude to mu
computer teacher, Mr Sumit Kapoor for their able guidance
and support in completing my project.
I would like to extend my gratitude to my parents as well as
to my principal for providing me with all the facilities
required.
THANK YOU.
JAI ANAND
12 E
INTERNAL EXAMINER:
EXTERNAL EXAMINER:
INDEX
1 PROGRAM1
2 PROGRAM 2
3 PROGRAM 3
4 PROGRAM 4
5 PROGRAM 5
6 PROGRAM 6
7 PROGRAM 7
8 PROGRAM 8
9 PROGRAM 9
10 PROGRAM 10
11 PROGRAM 11
12 PROGRAM 12
13 PROGRAM 13
14 PROGRAM 14
15 PROGRAM 15
PROGRAM 1
WRITE A PROGRAM TO ACCEPT TEN DIFFERENT IN SINGLE DIMENSIONAL
ARRAY AND ARRANGE IN ASCENDINGNORDER USING BUBBLE SORT
import java.util.*;
class Bubble
{
Scanner sc=new Scanner(System.in);
int arr[]=new int[10];
int i,j,temp=0;
void initialize()
{
System.out.println("Enter 10 numbers to be arranged in ascending
order");
for(int i=0;i<10;i++)
{
arr[i]=sc.nextInt();
}
}
void sort()
{
for(i=0;i<9;i++)
{
for(j=0;j<9-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void display()
{
System.out.println("Elements arranged in ascending order are");
for(i=0;i<10;i++)
{
System.out.println(arr[i]);
}
}
void main()
{
Bubble obj=new Bubble();
obj.initialize();
obj.sort();
obj.display();
}
}
OUTPUT
Enter 10 numbers to be arranged in ascending order
12
8
6
27
9
0
2
5
4
1
Elements arranged in ascending order are
0
1
2
4
5
6
8
9
12
27
ALGORITHM
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Bubble
STEP 4: Creating object of scanner class
STEP 5: Declaring array arr[][]
STEP 6: Declaring loop variables
STEP 7: Declaring temporary variable temp
STEP 8: Declaring function initialize()
STEP 9: Initialization of array
STEP 10: Accepting array elements
STEP 11: Declaring function sort()
STEP 12: Performing bubble sort
STEP 13: Declaring function display()
STEP 14: Printing the array in the sorted manner
STEP 15: Declaring main function
STEP 16: Creating an object of class Bubble
STEP 17:Calling the member function {initialize()}
STEP 18: Calling the member function {sort()}
STEP 19: Calling the member function {display()}
STEP 20: Stop
import java.util.*;
class Binary
{
Scanner sc=new Scanner(System.in);
int arr[]=new int[10];
int i,no,pos=0,mid,ub=9,lb=0;
void initialize()
{
System.out.println("Enter 10 numbers");
for(i=0;i<=ub)
{
mid=lb+ub/2;
if(arr[mid]no) ub=mid-1;
if(arr[mid]==no) { pos=1; break;
}
}
}
void display()
{
if(pos==1)
System.out.println("Number present");
else
System.out.println("Number not present");
}
void main()
{
Binary obj=new Binary();
obj.search();
obj.display();
}
}
OUTPUT
Enter 10 number
12
23
3
34
45
56
67
78
89
98
Enter number to be searched
22
Number not present
ALGORITHM
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Binary
STEP 4: Creating object of scanner class
STEP 5: Declaring instance variable
STEP 6: Declaring function initialize()
STEP 7: Initialization of array
STEP 8: Accepting the array elemnts
STEP 9: Declaring search()
STEP 10: Taking input of number to be searched
STEP 11: Loop for i from 0 to 10
STEP 12: Checking if number is equal to arr[i]
STEP 13: Declaring function display()
STEP 14: Printing message accordingly
STEP 15: Declaring main function 11 | P a g e
STEP 16: Creating an object of class Binary
STEP 17: Calling the member function {initialize()}
STEP 18: Calling the member function {search()}
STEP 19: Calling the member function {display()}
import java.util.Scanner;
class Automorphic
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Input a number : ");
int num = sc.nextInt();
int sq_num = num*num;
String str_num = Integer.toString(num);
String square = Integer.toString(sq_num);
if(square.endsWith(str_num))
System.out.println("Automorphic Number.");
Else
System.out.println("Not an Automorphic Number.");
}
}
OUTPUT
Input a number:76
Automorphic Number
ALGORITHMS
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Automorphic
STEP 4: Creating object of scanner class
STEP 5: Print statement to enter a number
STEP 6: Take input of the number
STEP 7: Take the square of the number
STEP 8: Store digits of the number using string
STEP 9: Store digits of the square of the number using string
STEP 10: Check if the square ends with the number
STEP 11: If true then display “It is a Automorphic number”
STEP 12: If false then display “It is a Not a Automorphic number”
STEP 13: STOP
OUTPUT
Input an integer number:2
It is an ugly number
ALGORITHM
STEP1: Start
STEP 2: input a number
STEP 3: check whether if the number is not equal to 0 or smaller than 0
STEP 4: if number is smaller than 0 or equal to 0 ask the user to input correct
number
STEP 5: we initialized the counter x=0
STEP 6: we will check if the number entered is divisible either by 2or3 or 5
STEP 7: if the number is not divisible by either 2 or 3 or 5 then counter is
increased by 1
STEP 8: stop
PROGRAM 5
Design a class Perfect to check if a given number is a perfect
number or not
import java.util.*;
class Perfect
{
int num;
Perfect(int nn)
{
num=nn;
}
int sum_of_factors(int i)
{
if (i == num) return 0;
else
if (num%i==0)
return i + sum_of_factors(i+1);
else
return sum_of_factors(i+1);
}
void check()
{
int s=sum_of_factors(1);
if (s==num) System.out.println(num+"is a perfect number");
else
System.out.println(num+"is not a perfect number");
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int no=sc.nextInt();
Perfect ob=new Perfect(no);
ob.check();
}
}
OUTPUT
Enter a number
6
6 is a perfect number
ALGORITHM
STEP 1: start
STEP 2: if sum of factors of the number is equal to the number then return 0
STEP 3: if the sum of factor is not equal to the number then increase the
number by 1
STEP 4: check the new number
STEP 5: if the sum of factors of the number is equal to the number print it is
perfect number else it is not a perfect number
STEP 6: stop
PROGRAM 6
Design a class ArmNum to check if a given number is an Armstrong
number or not.
import java.util.*;
class ArmNum
{
int n,l;
Scanner x=new Scanner(System.in);
ArmNum(int nn)
{
n=nn;
l=Integer.toString(n).length();
}
int sum_pow(int i)
{
if(i==0) return 0;
else
return (int)Math.pow(i%10,l) + sum_pow(i/10);
}
void isArmstrong()
{
if(sum_pow(n)==n)
System.out.println(n + "is an Armstrong number");
else
System.out.println(n + "is not an Armstrong number");
}
static void main()
{
ArmNum obj=new ArmNum(153);
obj.isArmstrong();
}
}
OUTPUT
153 is an Armstrong number
ALGORITHMS
STEP 1:start
STEP 2: enter the number
STEP 3: divide the number and add the cube of the digit
STEP 4: check if the sum is equal to the number
STEP 5: end
PROGRAM 7
Write a program to define a class col_sum as follows :
Data members : int a[][]
int r – rows
int c – columns
Member functions : colsum(int m, int n)
void fillarray()
void column() - to display the array along with the sum of each column.
main()
import java.util.*;
class col_sum
{
int a[][],r,c;
col_sum(int m,int n)
{
r=m; c=n;
a=new int[r][c];
}
void fillarray()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter array elements");
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
a[i][j]=sc.nextInt();
}
}
}
void column()
{
int i,j;
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
int s;
for(i=0; i<c; i++)
{ s=0;
for(j=0; j<r; j++)
{
s=s+a[j][i];
}
System.out.print("sum="+s+"\t");
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no of rows and columns");
int p=sc.nextInt();
int q=sc.nextInt();
col_sum obj=new col_sum(p,q);
obj.fillarray();
obj.column();
}
}
OUTPUT
Enter the no of rows and column
2
3
Enter array elements
1
2
3
4
5
6
1 2 3
4 5 6
Sum=5 sum=7 sum=9
ALGORITHM
Step 1 : START
Step 2 : Enter array elements
Step 3 : Print the array
Step 4 : Calculate the sum of each column and print it
Step 5 : Create the main function
Step 6 : Create an object
Step 7 : Call all the functions
Step 8 : END
PROGRAM 8
Define class 'GPseries' with following specifications :-
Date Members :-
a : to store first term
r : to store common ratio
n : to store limit
Member Functions :-
void readdata() : to input a,d,n
long nThterm(int) : to return nTh term of the series
long sum() : to return sum of the series
void showseries() : to display the series and sum
import java.io.*;
class GPseries
{
int a,r,n;
public static void main(String args[])throws Exception
{
GPseries a=new GPseries();
a.readdata();
a.showseries();
}
void readdata()throws Exception
{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter first term : ");
a = Integer.parseInt(in.readLine());
System.out.print("Enter common ratio : ");
r = Integer.parseInt(in.readLine());
System.out.print("Enter limit : ");
n = Integer.parseInt(in.readLine());
}
long nThterm(int b)
{
return a*(int)Math.pow(r,b-1);
}
long sum()
{
return a*(int)(Math.pow(r,n)-1)/(r-1);
}
void showseries()
{
for(int i=1;i<=n;i++)
System.out.print(nThterm(i)+" ");
System.out.println("\nSum : "+sum());
}
}
OUTPUT
Enter first term:2
Enter common ratio:3
Enter limit:4
2 6 18 54
Sum: 80
ALGORITHM
STEP 1: Start.
STEP 2: Declare integer type variables a, r, n as class variables.
STEP 3: Begin void readdata() function.
STEP 4: Ask for the First term, Common ratio and Limit of the
Geometric Progression in a, r, n respectively.
STEP 5: End the readdata() function.
STEP 6: Begin long nThterm(int b) function.
STEP 7: Perform a x r^(b-1) and return the value.
STEP 8: End the nThterm() function.
STEP 9: Begin long sum() function.
STEP 10: Perform a x ((r^n)-1)/(r-1) and return the value.
STEP 11: End the sum() function.
STEP 12: Begin void showseries() function.
STEP 13: Take a variable i=1.
STEP 14: Call the function nThterm() by sending i as parameter
and print the returned value.
STEP 15: Incement i by 1.
STEP 16: Repeat the Steps 14 and 15 until i becomes greater than n.
STEP 17: Call the sum() function and print the value returned.
STEP 18: End the showseries() function.
STEP 19: Begin the main() method.
STEP 20: Call the readdata() and showseries() functions.
STEP 21: Stop.
PROGRAM 9
Define class 'lucas' with following specifications :-
Date Members :-
n : to store no
Member Functions :-
void readdata() : to input n
int nThterm(int) : to return nTh term of the series
int sum() : to return sum of the series
void showseries(): to display the series
import java.io.*;
class lucas
{
int n;
public static void main(String args[])throws Exception
{
lucas a = new lucas();
a.readdata();
a.showseries();
}
void readdata()throws Exception
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter limit : ");
n = Integer.parseInt(in.readLine());
}
int nThterm(int m)
{
if(m>3)
return nThterm(m-1)+nThterm(m-2)+nThterm(m-3);
return m;
}
void showseries()
{
System.out.println("Series : ");
for(int i=1;i<=n;i++)
System.out.print(nThterm(i)+" ");
}
}
OUTPUT
Enter limit:7
Series:
1 2 3 6 11 20 37
ALGORITHM
STEP 1: Start.
STEP 2: Declare integer type variable n as class variable.
STEP 3: Begin void readdata() function.
STEP 4: Ask for the number of elements in the series.
STEP 5: End the readdata() function.
STEP 6: Begin int nThterm(int m) function.
STEP 7: If m > 3 , perform nThterm(m-1)+nThterm(m-2)+nThterm(m-3)
and return the result.
STEP 8: Else return the value of m.
STEP 9: End nThterm() function.
STEP 10: Begin void showseries() function.
STEP 11: Take a variable i=1.
STEP 12: Call the function nThterm() by sending i as parameter
and print the returned value.
STEP 13: Incement i by 1.
STEP 14: Repeat the Steps 12 and 13 until i becomes greater than n.
STEP 15: End the showseries() function.
STEP 16: Begin the main() method.
STEP 17: Call the readdata() and showseries() functions.
STEP 18: Stop.
PROGRAM 10
Write a program to display diamond pattern of string
import java.io.*;
class diamondstr
{
public static void main(String args[])throws Exception
{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
//inputting string
System.out.print("Enter a string : ");
String s = in.readLine();int i,j,k;
System.out.println("Pattern is : ");
//displaying diamond pattern
for(i=-s.length();i<s.length();i++)
{
k = 0;
for(j=-s.length();j<s.length();j++)
{
if(Math.abs(i)+Math.abs(j)<s.length())
{
System.out.print(s.charAt(k));
k = j<0?++k:--k;
}
else System.out.print(" ");
}
System.out.println();
}
}
}
OUTPUT
Enter a string: virat
Pattern is :
v
viv
viriv
virariv
viratariv
virariv
viriv
viv
v
ALGORITHM
STEP 1: Start.
STEP 2: Ask for a word from the user and store it in a variable s.
STEP 3: Declare 3 integer type variables i, j, k.
STEP 4: Initialise i by the negative value of the length of s.
STEP 5: Initialise k by 0.
STEP 6: Initialise j by the negative value of the length of s.
STEP 7: Check whether the sum of the absolute values of I
and j is less than the length of s.
If yes, print the character at the kth index of s. Do not
change the line on result window. Perform j<0?++k:--k.
If no, print blank space(' '). Do not change
the line on result window.
STEP 8: Increment the value of j by 1.
STEP 9: Repeat Steps 7 and 8 until j is equal to the length of s.
STEP 10: Change the line in the result window.
STEP 11: Increment the value of i by 1.
STEP 12: Repeat Steps 5 to 11 until i is equal to the length of s.
STEP 13: Stop.
PROGRAM 11
PRINT PIGLATIN WORDS
import java.util.*;
class Piglatin
{
public static void main()
{
Scanner sc =new Scanner (System. in);
System.out.println("Enter the word to be encoded");
String x= sc.nextLine();
x=x.toUpperCase();
int l=x.length ();
int pos=0;
for(int i=0;i<l;i++)
{
char ch= x.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
pos = i;
break;
}
}
String w= x.substring(pos)+x.substring(0,pos)+"AY";
System.out.println(w);
}
}
OUTPUT
Enter the word to be encoded
hello
ELLOHAY
ALGORITHM
STEP 1:start
STEP2: enter the number
STEP 3: check the vowel
STEP 4: change the case to upper case
STEP 5:print all the words present before vowel and add ay
STEP 6: end
PROGRAM 12
PRINT ANAGRAM WORDS
class anagram
{
public static void main(String x,String y)
{
x=x.ToUpperCase();
y=y.ToUpperCase();
String f="";
String e="";
int lx=x.length();
int ly=y.length();
for(int i=65;i<=90;i++)
{
for(int j=0;j<lx;j++)
{
char ch= x.charAt(j);
int m=(int)ch;
if(i==m)
e=e+(char)m;
}
for(int k=0;k<ly;k++)
{
char p=y.charAt(k);
int n=(int)p;
if(i==m)
f+=(char)m;
}
}
if(f.compareTo(e)==0)
System.out.println(x+"and"+y+"are Anagram words");
else
System.out.println(x+"and"+y+"are not Anagram words");
}
}
OUTPUT
X= Race
Y= Care
Race and Care are Anagram words
ALGORITHM
STEP 1: start
STEP 2: enter one word in x and second word in y
STEP 3: check the length of both the words entered
STEP 4: now compare both the string if they have same alphabets
STEP 5: if both the words have same same alphabets print it is anagram words
else print not an anagram word
STEP 6: End
PROGRAM 13
Write a program to define the following
class Interchange
Data Members : int a[] ;
int b[];
Member Functions : Interchange(int s)-
Parameterized constructor initialise size with s and
also instantiate both the arrays
void read()- to input array elements
void swap()- To interchange first and last element
second and second last element and so on.
void display()- To display elements (from first index
till last index)
main()- To input array size and call the functions
using object.
import java.util.*;
class Interchange
{
int a[],b[],size;
Interchange(int s)
{
size=s;
a=new int[size];
b=new int[size];
}
void read()
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter array elements”);
for(int i=0; i<size; i++)
{
a[i]=sc.nextInt();
}
}
void swap()
{
int j=0;
for(int i=size-1; i>=0; i--)
{
29
b[j]=a[i];
j++;
}
}
void display()
{
System.out.println(" Array A[] Array B[]");
for(int i=0; i<size; i++)
{
System.out.println(“\t”+a[i]+"\t"+b[i]);
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter array size");
int n=sc.nextInt();
Interchange obj=new
Interchange(n);
obj.read();
obj.swap();
obj.display();
}
}
OUTPUT
enter array size
4
Enter array elements
1
2
3
4
Array A[] Array B[]
1 4
2 3
3 2
4 1
ALGORITHM
STEP 1: start
STEP 2: enter the size of an array
STEP 3: enter the elements
STEP 4: interchange the elements and store it in B array
STEP 5: now print both the arrays
STEP 6: end
PROGRAM 14
WAP to define a class Magic with the following
specifications :
Data Members : int n
Member Functions :
Magic() - default constructor
void read() - to input value of n from the user
int sum (int x) - returns the sum of digits of x
boolean check ( int y ) - returns true if y is a magic
no. else it returns false
(A number is said to be a magic number if the
eventual sum of it's digits = 1)
void display() - to display whether n is a magic no.
or not
Create a main method to all the functions using
Object
import java.util.*;
class Magic {
int n ;
Magic() {
n=0; }
void read() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
n=sc.nextInt(); }
int sum(int x ) {
int s = 0 ;
while(x>0) {
int d = x % 10 ;
s=s+d ;
x=x/10 ; }
return s ;
5
4
}
boolean check (int y )
{
while(y>9)
{
y=sum(y) ;
}
if ( y==1)
return true ;
else
return false ;
}
void display()
{
if(check(n)==true)
System.out.println("Magic number");
else
System.out.println("Not a Magic number");
}
public static void main (String args[])
{
Magic obj = new Magic () ;
obj.read() ;
obj.display() ;
}
}
OUTPUT
Enter a number
19
Magic number
ALGORITHM
STEP 1: start
STEP 2: enter the number
STEP 3: check if the sum of the digits at eventual place is 1 or not
STEP 4: If the sum is 1 print the that the number entered is a magic number
else print entered number is not a magic number
STEP 5: end
PROGRAM 15
Define a class 'Encrypt' which following specifications :-
Data Members :-
str : to store string
Member Functions :-
void readdata() : to input string
void encrypt() : to encrypt string by word length
void decrypt() : to decrypt string by word length
void display() : to display string
import java.util.*;
class Encrypt
{
String str="";
void readdata()
{
Scanner sc=new Scanner(system.in);
System.out.print("Enter a string : ");
str = sc.nextLine()+" ";
System.out.print("Original String : ");
display(str);
System.out.print("Encrypt String : ");
encrypt();
System.out.print("Decrypt String : ");
decrypt();
}
void encrypt()
{
String s="",s1="";int i=0,j=0,r,k=0;
for(i=str.indexOf(" ",0);i!=-1;i=str.indexOf(" ",i+1))
{
s = str.substring(j,i);j=i+1;
for(k=0;k<s.length();k++)
{
r = s.charAt(k)+s.length();
if(r>90&&s.charAt(k)<='Z')
r = r-26;
if(r>122&&s.charAt(k)<='z')
r = r-26;
s1 = s1+(char)r;
}
s1 = s1+" ";
}
display(s1);
}
void decrypt()
{
String s="",s1="";int i=0,j=0,r,k=0;
for(i=str.indexOf(" ",0);i!=-1;i=str.indexOf(" ",i+1))
{
s = str.substring(j,i);j=i+1;
for(k=0;k<s.length();k++)
{
r=s.charAt(k)-s.length();
if(r<65&&s.charAt(k)>='A')
r = r+26;
if(r<97&&s.charAt(k)>='a')
r = r+26;
s1 = s1+(char)r;
}
s1 = s1+" ";
}
display(s1);
}
void display(String st)
{
System.out.println(st);
}
}
class code
{
public static void main()
{
Encrypt a=new Encrypt();
a.readdata();
}
}
OUTPUT
Enter a string : A a Z z
Original String : A a Z z
Encrypt String : B b A a
Decrypt String : Z z Y y
ALGORITHM
Step 1: define class Encrypt and declare String str
Step 2: define function readdata () to input a string and print original string alongwith
encrypted and decrypted string
Step 3: in void encrypt run loop for(i=str.indexOf(" ",0);i!=-1;i=str.indexOf(" ",i+1)) and
update s= str.substring( )and j=i+1
Step 4: start another loop with k=0; and run until k<str.length()
Step 5: put r = s.charAt(k)+s.length();
Step 6: check if(r>90&&s.charAt(k)<='Z')then do r = r-26; if(r>122&&s.charAt(k)<='z') r
= r-26; s1 = s1+(char)r;terminate all loops
Step 7: for decrypt() perform step 3,4,5 but for checking if(r<65&&s.charAt(k)>='A') r
= r+26;if(r<97&&s.charAt(k)>='a') r = r+26; s1 = s1+(char)r;
Step 8: end of all loops, end of function ,end of class.