Grade X lab Programs
Grade X lab Programs
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
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);
}
}
RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration
}
}
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:
}
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:
RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration:
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:
OUTPUT:
Sorted array elements
apple
bat
cat
dog
frog
RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration:
OUTPUT:
Sorted array elements
April
August
December
July
March
RESULT:
Thus the program was compiled and executed
successfully.
Variable declaration:
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:
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:
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:
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: