0% found this document useful (0 votes)
10 views

Grade X lab Programs

The document contains multiple Java programming examples demonstrating various concepts such as Pascal's Triangle, sorting algorithms, array manipulation, and class inheritance. Each section includes the aim, coding, output, result, and variable declarations for clarity. The programs are designed to showcase fundamental programming techniques and data structures.

Uploaded by

jaspalk74104
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Grade X lab Programs

The document contains multiple Java programming examples demonstrating various concepts such as Pascal's Triangle, sorting algorithms, array manipulation, and class inheritance. Each section includes the aim, coding, output, result, and variable declarations for clarity. The programs are designed to showcase fundamental programming techniques and data structures.

Uploaded by

jaspalk74104
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Pascal’s Triangle

AIM :
Write a program to display the pascal’s triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

CODING:
import java.util.*;
public class P_tri
{
public static void main(String agrs[])
{
int i,j,n,k,p=5;
int m[]=new int[20];
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no.of lines:");
n=sc.nextInt();
m[0]=1;
for(i=0;i<n;i++)
{
for(k=p;k>=1;k--)
System.out.print(" ");
for(j=0;j<=i;j++)
System.out.print(m[j]+" ");
System.out.println();
p--;
for(j=i+1;j>0;j--)
m[j]=m[j]+m[j-1];
}
}
}
OUTPUT:
Enter the no.of lines:5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

RESULT:
Thus the program was executed and compiled successfully.
Variable declaration

S.No Variable name Data type Purpose


1 i int loop variable
2 j int loop variable
3 n int for printing number of
rows.
4 k int loop variable
5 p int loop variable
6 m int for storing numbers in
arrays
Number Based Program
AIM:
Write a program in java to enter a number containing three
digits or more . Arrange the entered number in ascending
order and display the result.
Sample input:3827
Sample output:3278

CODING:
public class Digits
{
public static void main(int n)
{
int i,p,r;
System.out.println("The digits of the number in ascending
order");
for(i=0;i<=9;i++)
{
p=n;
while(p!=0)
{
r=p%10;
if(r==i)
{
System.out.print(r);
}
p=p/10;
}
}
}
}

OUTPUT:
The digits in ascending order
3,4,5,6,7

RESULT:
Thus the program was executed and compiled successfully.

Variable declaration
S.No Variable name Data type Purpose
1 I int loop variable
2 j int loop variable
3 p int to store the value of a
number after dividing by
10
4 r int to take out the last digit
of a number.
5 n int Input a number
FUNCTION MODEL

AIM:
Write a program by using a class with the following
specifictions:
Class name:Product
Data members/Instance variables
intpno:To store product number
String name:To store name of the product
intqnty:To store a quantity
Member methods:
voidgetdata():To input the product name,number and
quantity.
void show():To display product name,number and quantity.
Define another class that inherits from Product with the
following specifications:
Class name:Sale
Data members/Instance variable:
double rate
double amount
Member method:
void input():To accept rate
void calculate():To find amount by multiplying quantity
and rate
void display():To display amount to be paid

CODING:
importjava.util.*;
class Product
{
int pno;
String name;
int qnty;
Scanner in=new Scanner(System.in);
void getdata()
{
System.out.println("Enter the values");
pno=in.nextInt();
name=in.next();
qnty=in.nextInt();
}
void show()
{
System.out.println("Product number"+pno);
System.out.println("Product name"+name);
System.out.println("Quantity"+qnty);
}
}

class sale extends Product


{
doublerate,amount;
Scanner in=new Scanner(System.in);
void input()
{
System.out.println("Enter rate of the item purchased");
rate=in.nextDouble();
}
void calculate()
{
amount=rate*qnty;
}
void display()
{
System.out.println("Amount to be paid"+amount);
}
}
OUTPUT:
Enter the values
123
ANITHA
10
Product number:123
Product name:ANITHA
Quantity:10
Enter rate of item purchased
100
Amount to be paid
1000

RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration

S.No Variable name Data type Purpose


1 pno int to store the product
number

2 name string to store the name of the


product.
to store quantity
3 qnty int
to store the rate of the
4 rate double product
to store the amount of
5 amount double the product
Single dimensional Array
AIM:
Write a program to accept and categorize 20 employees.
The age group of 22-30,31-40,41-50,51-60.
CODING:
import java.util.*;
class Arrr
{
public static void main()
{
Scanner s=new Scanner(System.in);
int c1=0,c2=0,c3=0,c4=0,i;
int age[]=new int[20];
System.out.println("Enter the age of 20 employees");
for(i=0;i<20;i++)
{
age[i]=s.nextInt();
}
for(i=0;i<20;i++)
{
if(age[i]>=22&&age[i]<=30)
{
c1++;
}
else if(age[i]>31&&age[i]<=40)
{
c2++;
}
else if(age[i]>41&&age[i]<=50)
{
c3++;
}
else if(age[i]>51&&age[i]<=60)
{
c4++;
}
}
System.out.println("No.of employees between 22 & 30 is
:"+c1);
System.out.println("No.of employees between 31& 40 is
:"+c2);
System.out.println("No.of employees between 41& 50 is
:"+c3);
System.out.println("No.of employees between 51& 60 is
:"+c4);

}
}

OUTPUT:
Enter the age of 20 employees
34
44
54
22
33
55
23
43
54
24
60
78
58
90
78
67
45
49
59
29
No.of employees between 22 & 30 is :4
No.of employees between 30 & 40 is :2
No.of employees between 40 & 50 is :4
No.of employees between 50 & 60 is :6

RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration:

S.No Variable name Data type Purpose


1 Age int to store the age of the
employee

2 c1,c2,c3,c4 int to categorize the


employee according to
the age group.
loop variable
3 i int
Deleting an item in array
Aim:
Write a program to delete a number from an array from
given position.
CODING:
class Delete
{
public static void main(int a[],int p)
{
int num,i;
int n=a.length;
if(p<n)
{
num=a[p];
n=n-1;
System.out.println("The resulted output is:\n");
for(i=0;i<=n;i++)
{
System.out.println(a[i]);
}
System.out.println("The deleted value is:"+num);

}
else
{
System.out.println("Your position is not in the array a to
delete");
}
}
}

OUTPUT:
int a[]={10,20,30,40,50}
int p=3
The resulted output is:
10
20
30
40
50
The deleted value is:40
RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration:

S.No Variable name Data type Purpose


1 a int to store the array

2 p int to store the position of


the number

3 n int to store the length of the


array
loop variable
4 i int
to store and print the
5 num int value.
Inserting an item in an array
Aim:
Write a program with a value inserted in an array at a place.
Coding:
importjava.util.*;
class Insert
{
public static void main(String args[])
{
intsize,loc,item,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size");
size=sc.nextInt();
int a[]=new int[size+1];
System.out.println("Enter the array elements");
for(i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Enter array
location"); loc=sc.nextInt();
System.out.println("Enter new item:");
item=sc.nextInt();
for(i=size;i>loc;i--)
{
a[i]=a[i-1];
}
a[loc]=item;
size++;
System.out.println(“After inserting the array is”);
for(i=0;i<a.length;i++)
{
System.out.println(a[i]+" ");
}
}
}
Output:
Enter the array size
4
Enter the array elements
12
34
56
67
89
Enter array location
2
Enter new item:
66
After inserting the array is
12
34
66
56
67

RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration:

S.No Variable name Data type Purpose


1 size int to store the size of
array

2 loc int
to store the location of
the number
3 item int
to store the value of
number
4 i int loop variable
TWO DIMENSIONAL ARRAY
AIM:
Write a program to accept data from user in 2-D array.
CODING:
import java.util.Scanner;
public class Twodimen
{
public static void main(String args[])
{ int i,j;
Scanner s=new Scanner(System.in);
System.out.println("Enter the number of row");
int row=s.nextInt();
System.out.println("Enter the number of column");
int col=s.nextInt();
int mat[][]=new int [row][col];
for(i=0;i<row;i++)
{
for( j=0;j<col;j++)
{
System.out.println("Enter the value"); mat[i]
[j]=s.nextInt();
}
}
for(i=0;i<row;i++)
{
for( j=0;j<col;j++)
{
System.out.print(mat[i][j]+"\t");
}
System.out.println();
}
}
}
OUTPUT:
Enter the number of row
2
Enter the number of column
3
Enter the value
12
Enter the value
34
Enter the value
45
Enter the value
67
Enter the value
78
Enter the value
89
12 34 45
67 78 89
RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration:

S.No Variable name Data type Purpose


1 Row int to get number of rows
2 col int to get number of
columns

3 mat int to store the array

4 i int loop variable

5 j int loop variable


SINGLE DIMENSIONAL ARRAY
AIM:
Write a program to sort a String array by using Bubble Sort
technique.
CODING:
public class SortExample
{
public static void main(String args[])
{
String str_arr[]={"apple","dog","frog","cat","bat"};
SortExample(str_arr);
for (inti=0;i<str_arr.length;i++)
{
System.out.println(str_arr[i]);
}
}
private static void SortExample(String[] array)
{
String str;
System.out.println("Sorted array elements");
for(inti=0;i<array.length;i++)
{
for(int j=0;j<array.length-1-i;j++)
{
if(array[j].compareTo(array[j+1])>0)
{
str =array[j];
array[j]=array[j+1];
array[j+1]=str;
}
}
}
}
}

OUTPUT:
Sorted array elements
apple
bat
cat
dog
frog
RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration:

S.No Variable name Data type Purpose


1 Str_arr String to store the array loop
variables

2 str String to shift the elements of


the array
loop variable
3 i int
loop variable
4 j int
SELECTION SORT
AIM:
Write a program to sort the string array by using Selection
Sort technique.
CODING:
public class Selection
{
public static void main(String args[])
{
String
str_arr[]={"March","July","December","August","April"};
Selection(str_arr);
for (inti=0;i<str_arr.length;i++)
{
System.out.println(str_arr[i]);
}
}
private static void Selection(String[] array)
{
String str;
System.out.println("Sorted array elements");
for(inti=0;i<array.length-1;i++)
{
for(int j=i+1;j<array.length;j++)
{
if(array[i].compareTo(array[j])>0)
{
str =array[j];
array[j]=array[i];
array[i]=str;
}
}
}
}
}

OUTPUT:
Sorted array elements
April
August
December
July
March
RESULT:
Thus the program was compiled and executed
successfully.

Variable declaration:

S.No Variable name Data type Purpose


1 Str_arr String to store the array loop
variables

2 str String to shift the elements of


the array
loop variable
3 i int
loop variable
4 j int
LINEAR SEARCH
AIM:
Write a program to search a string array by using
Linear Search technique.

CODING:
public class Search
{
public static void main(String args[])
{
String
arr_str={"Sunday","Monday","Tuesday","Wednesday",
"Thursday"};
int i;
String str;
str="Thursday";
boolean b=false;
for(i=0;i<arr_str.length;i++)
{
if(arr_str[i]==str)
{
b=true;
}
}
if(b)
{
System.out.println(str+ "is in the list.");
}
else
{
System.out.println(str+ "is not in the list.");
}
}
}

OUTPUT:
Thursday is in the list.
RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration:

S.No Variable name Data type Purpose


1 str_arr String to store the array loop
variables

2 str String to shift the elements of


the array
loop variable
3 i int
to check whether it is
4 b boolean true or false
BUBBLE SORT
AIM:
Write a program to input the name and sex code(B/G)
for ‘n’ number of students and store them in two
different arrays. Print the separate list for boys and
girls.

CODING:
import java.util.*;
class Bubble{
public static void main(String agrs[])
{
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the students name");
for(i=0;i<6;i++)
{
String a[]=new String[6];
System.out.println("Enter the gender of the student");
}for(i=0;i<6;i++)
{
char g[]=new char[6];
}
for(i=1;i<6;i++)
{
System.out.println("list of girls");
if(g[i]=='G')
{
System.out.println(a[i]);
}
else
{
System.out.println("List of boys");

}
if(g[i]=='B')
{
System.out.println(a[i]);
}
}
}
}

OUTPUT:
Enter the student name
Suman
Keerthi
Manoj
Moni
Basha
Enter the gender of student
B
G
B
G
B
List of Boys
Suman
Manoj
Basha
List of Girls
Keerthi
Moni

RESULT:
Thus the program was compiled and executed
successfully.

Variable declaration:

S.No Variable name Data type Purpose


1 g[] character array
2 a[] String to enter the name of the
str student

3 i int loop variable

4 j int loop variable


STRING BASED PROGRAM
AIM:
Write a program in java to accept a word (string) and
display the new string after removing all the vowels
present in it.

CODING:
import java.util.*;
classStringEx
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String str=sc.nextLine();
String nstr="";
loop:
for(inti=0;i<str.lenght();i++)
{
char c=str.charAt(i);
Switch(Character.toUpperCase(c));
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
continue;
}
nstr+=c;
}
System.out.println("After removing vowels:" +nstr);

}
}

OUTPUT:
Enter the string
HELLO JAVA
After removing the vowels
HLL JV
RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration:

S.No Variable name Data type Purpose


1 Str String to store the input string
2 nstr String to store the value of
string after removing the
variables
loop variable
3 i int
to convert the string into
4 c char character.
COMPARING THE STRING
AIM:
Write a java program to compare two strings.

CODING:
import java.util.*;
classStringExample
{
public static void main(String args[])
{
String str1="Hello java";
String str2="Hello world";
System.out.println("String 1:"+str1);
System.out.println("String 2:"+str2);
int result=str1.compareTo(str2);
if(result<0)
{
System.out.println("\"" +str1+"\""+"is less
than"+"\""+str2+ "\"");
}
else if(result==0)
{
System.out.println("\"" +str1+"\""+"is equal
to"+"\""+str2+ "\"");
}
else if(result>0)
{
System.out.println("\"" +str1+"\""+"is greater than
"+"\""+str2+ "\"");
}
}
}

OUTPUT:
String 1:Hello java
String 2:Hello world
"Hello java"is less than"Hello world"

RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration:

Sl.no Variable name Data type Purpose


1 str1 String to store the first string
value

2 str2 String to store the second string


value
to compare the input
3 result int strings.

You might also like