Record Final X San
Record Final X San
NAME: SANCHIT
CLASS: X
SEC: B
1
S.NO TITLE PAGE NUMBER
1 Electricity bill 4
3 Automorphic number 10
4 Special number 12
5 Disarium number 14
6 Armstrong number 16
7 Pattern generation 18
8 Series generation 23
9 Selection Sort 27
11 Function 31
12 Function overloading 34
13 Constructor 36
14 Parametrized constructor 38
15 Constructor overloading 40
2
16 Strings 43
17 Strings 45
18 Two-Dimensional Array 47
19 Two-Dimensional Array 49
20 Binary Search 51
3
PROGRAM:1 ELECTRICITY BILL
AIM:
PROGRAM CODING:
import java.util.*;
class ElectricityBill
int units,cno;
String cnm;
cno= sc.nextInt();
cnm= sc.next();
units=sc.nextInt();
double billpay=0;
if(units<=100)
billpay=units*1.80;
billpay=100*1.80+200 *2.30+(units-300)*2.80;
else
billpay=100*1.80+200 *2.30+200*2.80+(units-500)*3.50;
5
INPUT/OUTPUT:
101
ANU
250
consumer no:101
VARIABLE DESCRIPTION:
6
PROGRAM:2 PRIME PALINDROME NUMBER
AIM:
PROGRAM CODING:
import java.util.*;
class PrimePalin
int n,p,rev,s=0,i,c=0;
System.out.println("Enter No.");
n= in.nextInt();
p=n;
for(i=1;i<=p;i++)
if(p%i==0)
c++;
while(n>0)
rev=n%10;
s=s*10+rev;
n=n/10; }
7
if(p==s && c==2)
else
8
INPUT/OUTPUT:
VARIABLE DESCRIPTION:
9
PROGRAM:3 AUTOMORPHIC NUMBER
AIM:
PROGRAM CODING:
import java.util.*;
class automorphic
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter a number");
n=sc.nextInt( );
int m,b,p,q;
m=n;
p=m*m;
int c=0;
while(m>0)
{
c++;
m=m/10;
}
q=p%(int)Math.pow(10,c);
if(n==q)
{
System.out.println("It is an automorphic number");
}
else
{
System.out.println("It is not an automorphic number");
}
}
}
10
INPUT/OUTPUT:
Enter a number
25[25*25=625]
It is an automorphic number
VARIABLE DESCRIPTION:
11
PROGRAM:4 SPECIAL NUMBER
AIM:
Write a program to check whether the entered number is a special number or not.
A number is said to be special number when the sum of the factorial of its digits are equal to the
number itself.
PROGRAM CODING:
import java.util.*;
class special
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,num;
System.out.println("Enter a number");
num=sc.nextInt();
int a,s=0;
int fact;
n=num;
while(n>0)
{
a=n%10;
fact=1;
int i=1;
while(i<=a)
{
fact*=i;
i++;
}
s=s+fact;
n=n/10;
}
if(s==num)
{System.out.println("It is a special number");
}
else
{
System.out.println("It is not a special number");
}
}
}
12
INPUT/OUTPUT:
Enter a number
145[1!+4!+5!=1+24+120=145]
It is a special number
VARIABLE DESCRIPTION:
13
PROGRAM:5 DISARIUM NUMBER
AIM:
Write a program to accept a number and check whether it is a disarium number or
not. A number is called Disarium if sum of its digits powered with their respective positions is
equal to the number itself.
PROGRAM CODING:
import java.util.*;
class disarium
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter a number");
n=sc.nextInt();
int a,d,sum=0;
a=n;
String s=Integer.toString(n);
int len=s.length();
while(n>0)
{
d=n%10;
sum=sum+(int)Math.pow(d,len);
len--;
n=n/10;
}
if(sum==a)
{
System.out.println("It is a disarium number");
}
else
{
System.out.println("It is not a disarium number");
}
}
}
14
INPUT/OUTPUT:
Enter a number
135[11+32+53=1+9+125=135]
It is a disarium number
VARIABLE DESCRIPTION:
15
PROGRAM: 6 ARMSTRONG NUMBER
AIM:
Write a program to accept a number and check whether the number is Armstrong
number or not by using the function name check(int n). The function returns 1, if the number is
Armstrong , otherwise 0. [Armstrong number is a number which equals to the sum of the cubes of
its individual digits.]
PROGRAM CODING:
import java.util.Scanner;
public class armstrong
{
public int check(int n)
{
int d, s=0;
int num=n;
while(n>0)
{
d=n%10;
s=s+(d*d*d);
n=n/10;
}
if(s==num)
return 1;
else
return 0;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
armstrong obj=new armstrong();
int n;
System.out.println("Enter a number");
n=sc.nextInt();
int k=obj.check(n);
if(k==1)
System.out.println("Armstrong number");
else
System.out.println("Not a armstrong number");
}
}
16
INPUT/OUTPUT:
Enter a number
153[13+53+33=1+125+27=153]
Armstrong Number
VARIABLE DESCRIPTION:
17
PROGRAM: 7 PATTERN GENERATION
AIM:
Write a program to print the following patterns:
i.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
class pattern
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(i+"\t");
}
System.out.println();
}
}
}
ii.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
class pattern
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+"\t");
}
System.out.println();
}
}
}
18
iii.
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
class pattern
{
public static void main(String args[])
{
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+"\t");
}
System.out.println();
}
}
}
iv.
1 1 1
2 2
3
class pattern
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=3;j>=i;j--)
{
System.out.print(i+"\t");
}
System.out.println();
}
}
}
v.
3
2 2
1 1 1
class pattern
{
public static void main(String args[])
{
for(int i=3;i>=1;i--)
{
for(int j=3;j>=i;j--)
{
19
System.out.print(i+"\t");
}
System.out.println();
}
}
}
vi.
1
2 1
3 2 1
class pattern
{
public static void main(String args[])
{
for(int i=1;i<=3;i++)
{
for(int j=i;j>=1;j--)
{
System.out.print(j+"\t");
}
System.out.println();
}
}
}
vii.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
class pattern
{
public static void main(String args[])
{
int k=1;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(k+"\t");
k++;
}
System.out.println();
}
}
}
20
viii.
*
* #
* # *
* # * #
* # * # *
class pattern
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
if(j%2==0)
{
System.out.print("#"+"\t");
}
else
{
System.out.print("*"+"\t");
}
}
System.out.println();
}
}
ix.
1
1 3
1 3 5
1 3 5 7
1 3 5 7 9
class pattern
{
public static void main(String args[])
{
for(int i=1;i<=9;i+=2)
{
for(int j=1;j<=i;j+=2)
{
System.out.print(j+"\t");
}
System.out.println();
21
}
}
}
x.
5 5 5 5 5
4 4 4 4 4
3 3 3 3 3
2 2 2 2 2
1 1 1 1 1
class pattern
{
public static void main(String args[])
{
for(int i=5;i>=1;i--)
{
for(int j=5;j>=1;j--)
{
System.out.print(i+"\t");
}
System.out.println();
}
}
}
VARIABLE DESCRIPTION:
22
PROGRAM:8 SERIES GENERATION
AIM:
Write a program to generate the following series:
i.Sum=1/2 +3/4+5/6+7/8+….+19/20
class series
{
public static void main(String args[])
{
double term=0,sum=0;
for(int i=1;i<=19;i+=2)
{
term=(double)i /i+1;
sum+=term;
}
System.out.println(“Sum of the series=”+sum);
}
}
Output:
sum=20
ii.sum=2-4+6-8+…..-20
class series
{
public static void main(String args[])
{
int sum=0;
for(int i=2;i<=20;i+=2)
{
if(i%4==0)
sum=sum-i;
else
sum=sum+i;
}
System.out.println(“Sum=”+sum);
}
}
Output:
sum=-10
iii.1,-3,5,-7,9
class series
{
public static void main(String args[])
{
23
int i,j=-3;
for(i=1;i<=20;i+=4)
{
System.out.print(i+”,”);
System.out.print(j+”,”);
j=j-4;
}
}
Output:
1,-3,5,-7……-19
iv.Sum=1!+2!+3!+….n
import java.util.*;
class series
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println(“Enter a number”);
n=sc.nextInt( );
int f=1;
int sum=0;
for(i=n;i>=1;i--)
{
for(int j=i;j>=1;j--)
{
f=f*j;
}
sum=sum+f;
f=1;
}
System.out.println(“Sum=”+sum);
}
}
Output:
Enter a number
5
sum=153
v.1,12,123,1234,….n
class series
{
public static void main(String args[])
{
24
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.print(“,”);
}
}
}
Output:
1,12,123,1234,….12345678910
vi. s=(1*2)+(2*3)+….+(19*20)
class series
{
public static void main(String args[])
{
int sum=0;
for(int i=1;i<=19;i++)
{
sum=sum+(i*(i+1));
}
System.out.println(“Sum=”+sum);}}
Output:
sum=2660
vii. s=1+(1+2)+(1+2+3)+….+(1+2+3+…+n);
import java.util.*;
class series
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int s=0,sum=0,n;
System.out.println(“Enter a number”);
n=sc.nextInt();
for(int i=1;i<=n;i++)
{
s=s+i;
sum=sum+s;
}
System.out.println(“Sum=”+sum);
}
}
25
Input/Output:
Enter a number
5
sum =35
VARIABLE DESCRIPTION:
26
PROGRAM:9 SELECTION SORT
AIM:
Write a program to accept 10 numbers from the user. Sort the elements of the array in
ascending order using selection sort technique.
PROGRAM CODING:
import java.util.*;
class selectionSort
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int m[]=new int[10];
System.out.println("ENTER 10 NOS.");
for(int i=0;i<10;i++)
{
m[i]=sc.nextInt();
}
int temp,pos;
for(int i=0;i<10;i++)
{
pos=i;
for(int j=i+1;j<10;j++)
{
if(m[j]>pos)
{
pos=j;
}
}
temp=m[i];
m[i]=m[pos];
m[pos]=temp;
}
System.out.println("FINAL SORTED ARRAY");
for(int i=0;i<10;i++)
{
System.out.println(m[i]);
}
}
}
27
INPUT/OUTPUT:
Enter 10 numbers
20
14
16
18
10
2
4
6
8
12
Sorted Array:
4
6
8
10
12
14
16
18
20
VARIABLE DESCRIPTION:
28
PROGRAM:10 BUBBLE SORT
AIM:
Write a program to arrange Strings in an Alphabetical Order using bubble sort technique.
PROGRAM CODING:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str[] = new String[5];
String temp;
System.out.println("Enter the Strings one by one:");
for(int i = 0; i < 5; i++)
{
str[i] = sc.nextLine();
}
for (int i = 0; i < 5; i++)
{
for (int j = i + 1; j < 5; j++) {
if (str[i].compareTo(str[j])>0)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
} } }
System.out.print("Strings in Sorted Order:");
for (int i = 0; i <5; i++)
{
System.out.print(str[i] + ", ");
} } }
29
INPUT/OUTPUT:
Enter String one by one:
Anu
Jason
Rithik
Benny
Kevin
VARIABLE DESCRIPTION:
30
PROGRAM:11 FUNCTION
AIM:
Define a class called ParkingLot with the following description:
Instance variables:
int vno- to store the vehicle number
int hours- to store the number of hours the vehicle is parked in the parking lot
double bill- to store the bill amount
Member methods:
void input( )-to input the vno and hours
void calculate()- to compute the parking charge at the rate of Rs.3 for the first hour or part
thereof,and Rs.1.50 for each additional hour or part thereof.
void display( )-to display the detail.
Write a main method to create an object of the class and call the above methods.
PROGRAM CODING:
import java.util.*;
class parkingLot
{
private int vno;
private int hours;
private double bill;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the vehicle number");
vno=sc.nextInt();
System.out.println("Enter the number of hours");
hours=sc.nextInt();
}
void calculate()
{
if(hours>0&& hours<=1)
{
bill=hours*3.0;
System.out.println("Parking charges="+bill);
}
else
{
bill=3.0+(hours-1)*1.50;
System.out.println("Parking charges="+bill);
}
}
void display()
{
System.out.println("Vehicle number"+vno);
System.out.println("Total hours"+hours);
System.out.println("Total bill"+bill);
}
31
public static void main(String args[])
{
parking obj=new parking();
obj.input();
obj.calculate();
obj.display();
}
}
32
INPUT/Output:
Enter the vehicle number
7345
Enter the number of hours
5
Parking charges=9.0
Vehicle number7345
Total hours5
Total bill9.0
VARIABLE DESCRIPTION:
33
PROGRAM:12 FUNCTION OVERLOADING
AIM:
Design a class to overload a function compare( ) as follows:
a. void compare(int,int)-to compare two integer values and print the greater of the two
integers.
b. void compare(char,char)-to compare the numeric value of two integers and print the
character with higher numeric value.
c. void compare(String,String)-to compare the two strings and print the longer of the two.
PROGRAM CODING:
class overload
{
public void compare(int a,int b)
{
int c=0;
if(a>b)
c=a;
else
c=b;
System.out.println("Greater number is" +c);
}
public void compare(char a,char b)
{
char c=' ';
int m=(int)a;
int n=(int)b;
if(m>n)
c=a;
else
c=b;
System.out.println("Character with higher value is" +c);
}
public void compare(String a,String b)
{
String c="";
int m=a.length( );
int n=b.length( );
if(m>n)
c=a;
else
c=b;
System.out.println("Longest string is" +c);
}
public static void main(String args[])
{
overload obj=new overload();
obj.compare(5,3);
obj.compare('D','N');
obj.compare("Computer","Science");
} }
34
Input/Output:
VARIABLE DESCRIPTION:
35
PROGRAM:13 CONSTRUCTOR
AIM:
Write a complete java program with the following members of its class:
deposited
PROGRAM CODING:
class Account
{
private long ac_num;
private String ac_name;
private double bal;
Account( )
{
ac_num=0;
ac_name=" ";
bal=0.0;
}
public void getValues(long acNumber,String acName)
{
ac_num= acNumber;
ac_name= acName;
}
public void deposit(double amt)
{
bal+=amt;
}
public void withdrawl(double wamt)
{
bal-=wamt;
}
36
public void display( )
{
System.out.println("Account Number:"+ac_num);
System.out.println("Account Holder’s Name:"+ac_name);
System.out.println("Account Holder’s Balance:"+bal);
}
public static void main(String args[])
{
Account obj=new Account();
obj.getValues(20041961,"Sumith Arora");
System.out.println("After Depositing Money");
obj.deposit(5000);
obj.display();
System.out.println("After withdrawing Money");
obj.withdrawl(1000);
obj.display();
}
}
Input/Output:
After Depositing Money
Account Number:20041961
Account Holder’s Name:Sumith Arora
Account Holder’s Balance:5000.0
After withdrawing Money
Account Number:20041961
Account Holder’s Name:Sumith Arora
Account Holder’s Balance:4000.0
VARIABLE DESCRIPTION:
37
PROGRAM:14 PARAMETRIZED CONSTRUCTOR
AIM:
Data members:
Member methods:
Write a main method to create an object of a class and call the above methods.
PROGRAM CODING:
import java.util.*;
Input/Output:
Name: Aravind
Employee number=emp0876
Gross salary=36250.0
Net salary=33250.0
VARIABLE DESCRIPTION:
39
PROGRAM:15 CONSTRUCTOR OVERLOADING
AIM:
Data members:
Member methods:
Upto 1 km Rs.25
Write a main method to create an object of a class and call the above methods.
PROGRAM CODING:
import java.util.*;
public class Cabservice {
String taxino, name;
int dis, amt;
Cabservice( )
{
taxino=" ";
name="";
dis=0;
40
}
Cabservice(String tno, String n, int d)
{
taxino=tno;
name=n;
dis=d;
}
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter taxi number");
taxino=sc.nextLine();
System.out.println("Enter name");
name=sc.next();
System.out.println("Enter distance travelled");
dis=sc.nextInt();
}
public void compute( )
{
if(dis<=1)
amt=25;
if(dis>1 && dis<=5)
amt=dis*30;
if(dis>5 && dis<=10)
amt=dis*35;
if(dis>10 && dis<=20)
amt=dis*45;
if(dis>20)
amt=dis*50;
}
public void display( )
{
System.out.println("Taxi no:"+taxino);
System.out.println("Name:"+name);
System.out.println("Distance travlled="+dis);
System.out.println("Bill amount="+amt);
}
public static void main(String args[ ])
{
Cabservice obj =new Cabservice();
//Default constructor
obj.input();
obj.compute( );
obj.display();
Cabservice obj1 =new Cabservice("Tn07 AH54","Reni",12);
// Parametrized constructor
obj1.compute( );
obj1.display();
}
}
41
Input/Output:
VARIABLE DESCRIPTION:
42
PROGRAM 16: STRINGS
AIM:
Write a program to accept a word and display the new word by replacing only the vowels with
the character following it.
PROGRAM CODING:
import java.util.*;
class replace
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter a string");
s=sc.next();
int i,l;
int c;
char ch;
l=s.length();
char d;
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
c=(int)(ch + 1);
d=(char)c;
System.out.print(d);
}
else
System.out.print(s.charAt(i));
}
}
}
43
Input/Output:
Enter a string
computer
cpmpvtfr
VARIABLE DESCRIPTION:
44
PROGRAM 17: STRINGS
AIM:
PROGRAM CODING:
import java.util.*;
class alphabetical
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter a string");
s=sc.next();
int l=s.length();
char ch;
int a=0;
for(int i=65;i<=90;i++)
{
for(int j=0;j<l;j++)
{
ch=s.charAt(j);
a=ch;
if((a==i)||(a==i+32))
System.out.print(ch);
}
}
}
}
45
Input/ Output:
Enter a string
computer
cemoprtu
VARIABLE DESCRIPTION:
46
PROGRAM 18: TWO DIMENSIONAL ARRAY
AIM:
Write a program to input values in a two dimensional array with 3 rows and 3 columns. Compute and
print the sum of elements at the left diagonal.
PROGRAM CODING:
import java.util.*;
class sumLeftDiagonal
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int a[ ][ ]=new int[3][3];
System.out.println(“Enter the values”);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt( );
}
}
System.out.println(“Matrix format”);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+”\t”);
}
System.out.println( );
}
int s=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
s=s+a[i][j];
}
}
System.out.println(“Sum of elements at the left diagonal is” +s);
}
}
47
OUTPUT:
VARIABLE DESCRIPTION:
48
PROGRAM 19: TWO DIMENSIONAL ARRAY
AIM:
Write a program to input values in a two dimensional array with 3 rows and 3 columns.
Compute and print the transpose of the given matrix.
PROGRAM CODING:
import java.util.*;
class transpose
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int a[ ][ ]=new int[3][3];
System.out.println(“Enter the values ”);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt( );
} }
System.out.println(“Matrix format”);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+”\t”);
}
System.out.println( );
}
int b[ ][ ]=new int[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
b[i][j]=a[j][i];
} }
System.out.println(“Transpose matrix”);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(b[i][j]+”\t”);
}
System.out.println( );
}
} }
49
INPUT/OUTPUT:
Matrix format:
1 2 3
4 5 6
7 8 9
Transpose matrix:
1 4 7
2 5 8
3 6 9
VARIABLE DESCRIPTION:
50
PROGRAM 20: BINARY SEARCH
AIM:
Write a java program to search an element in an array using binary search technique.
PROGRAM CODING:
import java.util.*;
class search
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m[]=new int[10];
System.out.println("Enter 10 numbers in ascending or descending order");
for(int i=0;i<10;i++)
{
m[i]=sc.nextInt();
}
int ns;
int k=0;
int beg=0,last=9,mid=0;
System.out.println("Enter the number to be searched");
ns=sc.nextInt();
while(beg<=last)
{
mid=(beg+last)/2;
if(m[mid]<ns)
beg=mid+1;
if(m[mid]>ns)
last=mid-1;
if(m[mid]==ns)
{
k=1;
break;
}
}
if(k==1)
System.out.println("Search successful");
else
System.out.println("Search unsuccessful");
}
}
51
INPUT/OUTPUT:
Enter 10 numbers in ascending or descending order
1
4
6
8
9
13
16
18
20
31
Enter the number to be searched
4
Search successful
VARIABLE DESCRIPTION:
beg int the starting position of the array after the array
splits where the search begins
last int the ending position of the array after the array
splits where the search ends
52