0% found this document useful (0 votes)
21 views36 pages

Computer Practical

This document contains 10 programming problems and their solutions in Java. It provides the name, class, section, and roll number of the student, along with the subject (Computer) and that it is for the final term computer practical exam. Each problem is numbered and includes a brief description. The programs provided solve problems related to finding Kaprekar numbers, removing repeating letters from a string, calculating the greatest common divisor of two numbers, generating Pascal's triangle, unique random numbers, matrix multiplication, summing the outer elements of a matrix, binary search, summing rows and columns of a 4x4 matrix, and room allocation in a 10 floor hotel.

Uploaded by

Priyanka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views36 pages

Computer Practical

This document contains 10 programming problems and their solutions in Java. It provides the name, class, section, and roll number of the student, along with the subject (Computer) and that it is for the final term computer practical exam. Each problem is numbered and includes a brief description. The programs provided solve problems related to finding Kaprekar numbers, removing repeating letters from a string, calculating the greatest common divisor of two numbers, generating Pascal's triangle, unique random numbers, matrix multiplication, summing the outer elements of a matrix, binary search, summing rows and columns of a 4x4 matrix, and room allocation in a 10 floor hotel.

Uploaded by

Priyanka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

NAME: PRIYANKA SEN

CLASS: 11
SEC :SCIENCE
ROLL NO. : 08
SUBJECT : COMPUTER

COMPUTER PRACTICAL
FINAL TERM
INDEX

1. WAP in java to find the kaprekar


numbers and count them.

2. WAP in java to remove repeating


letters from a string .

3. WAP in java to fin the GCD of two


numbers .

4. WAP to print the pascal’s triangle


5. WAP in java to generate unique
numbers between two numbers

6. WAP in java to perform matrix


multiplication .

7. WAP in java to enter elements in a


matrix and find the sum of the outer
elements .

8. WAP in java to search an element in a


SDA of size 10 using binary search
technique
9. WAP in java to store numbers in a 4 * 4
matrix . Find the sum of number of each
rows and columns .

10. A hotel has 10 floors numbered from


0-9 . Each row has 50 rooms. Using a
variable F and R as floor no. And room no.
Respectively, WAP in java for room
allocation work. The program should
automatically record the name and room
no.
PROGRAM 1
import java.util.*;
public class KaprekarNumber
{
public static boolean isKaprekar(int number)
{
Scanner sc=new Scanner (System.in);

// declare variables
int square = 0;
int temp = 0;
int countDigits = 0;
int firstPart = 0;
int secondPart = 0;
int sum = 0:
// calculate square value of the number
square = number * number;
// count number of digits in the square
temp =square;
while(temp!=0)

{
countDigits++;
temp /= 10;
}
// divide square into two parts and
// check it's sum is equal to the number?
for(int i=countDigits-1; i>0; i--) {

// find first part


firstPart = square / (int)Math.pow(10, i);
// find second part
secondPart = square % (int)Math.pow(10, i);

// check have any part only 0


if(firstPart == 0 || secondPart == 0)
continue;
// find sum value
sum = firstPart + secondPart;

// compare sum and number


if( sum == number )
return true;
}

return false;
}

public static void main(String[] args)


{
// declare variable
int number = 0;
// read the input
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer number:: ");
number = scan.nextInt();
// check the number is kaprekar number or not
if(isKaprekar(number))
System.out.println(number+" is a"+ " kaprekar number");
else
System.out.println(number+" is not a"+ " kaprekar number");
// close Scanner class object
scan.close();
}
}
PROGRAM 2
import java.util.*;
class Duplicate{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
String s1,s2="";
int i,p,j,t;
System.out.println("Enter a String");
s1=sc.next();
char ch1,ch2;
p=s1.length();
for(i=0;i<p;i++){
ch1=s1.charAt(i);t=0;
for(j=0;j<s2.length();j++){
ch2=s2.charAt(j);
if(ch1==ch2)
t=1;
}
if(t==0)
s2=s2+ch1;
}
System.out.println("The new String after removing duplicate
letters:"+s2);
}
}
PROGRAM 3

import java.util.*;
class GCD {
public static void main(String[] args) {
// find GCD of n1 and n2
int n1,n2;
Scanner sc=new Scanner(System.in);
System.out.println("enter the first number");
n1=sc.nextInt();
System.out.println("enter the second number");
n2=sc.nextInt();
// initially set to gcd
int gcd = 1;
for (int i = 1; i <= n1 && i <= n2; ++i) {
// check if i perfectly divides both n1 and n2
if (n1 % i == 0 && n2 % i == 0)
gcd = i;
}
System.out.println("GCD of " + n1 +" and " + n2 + " is " + gcd);
}
}
PROGRAM 4

// Print Pascal's Triangle in Java


import java.io.*;
class PascalsTriangle {
public int factorial(int i)
{
if (i == 0)
return 1;
return i * factorial(i - 1);
}
public static void main(String[] args)
{
int n = 4, i, j;
PascalsTriangle g = new PascalsTriangle();
for (i = 0; i <= n; i++) {
for (j = 0; j <= n - i; j++) {
// for left spacing
System.out.print(" ");
}
for (j = 0; j <= i; j++) {
// nCr formula
System.out.print(" "+ g.factorial(i)/ (g.factorial(i - j)*
g.factorial(j)));
}
// for newline
System.out.println();
}
}
}
PROGRAM 5

import java.util.*;
class UniqueRange
{
public static boolean check(int num)
{
String n=Integer.toString(num);// converting number into String
for(int i=0;i<n.length();i++)
{
for(int j=i+1;j<n.length();j++)
{
if(n.charAt(i)==n.charAt(j))//digit ectraction
return false;
}
}
return true;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter lower and upper limit ");//range
int l=sc.nextInt();
int u=sc.nextInt();
if(l<=u)//range is checked
{
for(int i=l;i<=u;i++)
{
if(check(i)==true)//condition for unique range
System.out.println(i);
}
}
else
System.out.println("Invalid Range ");

}
}
PROGRAM 6
import java.util.*;
public class MatrixMul
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter no. of rows and columns for first matrix
");
int r1=sc.nextInt();
int c1=sc.nextInt();
System.out.println("Enter no. of rows and columns for Second2
matrix ");
int r2=sc.nextInt();
int c2=sc.nextInt();
if(c1!=r2)
System.out.println("Multiplication not possible ");
else
{
int a[][]=new int[r1][c1];//first matrix
int b[][]=new int[r2][c2];//second matrix
System.out.println("Enter "+(r1*c1)+" Numbers ");
for(int i=0;i<r1;i++)//numbers enters in 1st matrix
{
for(int j=0;j<c1;j++)
a[i][j]=sc.nextInt();
}

System.out.println("Enter "+(r2*c2)+" Numbers ");


for(int i=0;i<r2;i++)//numbers entered in 2nd matrix
{
for(int j=0;j<c2;j++)
b[i][j]=sc.nextInt();
}
int c[][]=new int[r1][c2];//third matrix
for(int i=0;i<r1;i++) // first row
{
for(int j=0;j<c2;j++)// second column
{
int s=0;
for(int k=0;k<c1;k++)//first column/second row
s=s+a[i][k]*b[k][j];//summation of the product

c[i][j]=s;//sum is implemented in the third matrix


}
}
System.out.println("First Matrix ");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
System.out.print(a[i][j]+"\t");//1st matrix is printed

System.out.println();
}
System.out.println("Second Matrix ");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
System.out.print(b[i][j]+"\t");//2nd matrix is printed
System.out.println();
}
System.out.println("final Matrix ");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
System.out.print(c[i][j]+"\t");//final matrix is printed

System.out.println();
}
}
}

}
PROGRAM 7

import java.util.*;
class boundarysum
{
int x[][];
int r,c;
void get()
{
Scanner sc=new Scanner(System.in);
do{ //entering the no of rows and cols
System.out.println("Enter row and column");
r=sc.nextInt();
c=sc.nextInt();
}
while((r<=0) || (c<=0));
x=new int[r][c];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
x[i][j]=sc.nextInt();
}
}
display(x); //nested function
boundary(x);
}
void display(int a[][])
{
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
{
System.out.print(a[i][j]+"\t"); //display the matrix
}
System.out.println();
}
}
void boundary(int x[][])
{
int s1=0;
for(int i=0;i<x.length;i++)
{
for(int j=0;j<x[0].length;j++)
{
if(i==0 || i==x.length-1 || j==0 || j==x[0].length-1) //conditions
for the elements
s1+=x[i][j];
}
}
System.out.println("Boundary elements sum ="+s1); //printing
the sum
}
public static void main(String args[]){
new boundarysum().get(); //method call
}
}
PROGRAM 8

import java.util.*;
class binary
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int i,k=0,p=0,lb=0,ub=0;
int m[]=new int[10];//array
System.out.print("enter numbers ");
for(i=0;i<10;i++)
{
m[i]=sc.nextInt();
}

System.out.print("enter number to be searched ");


int ns=sc.nextInt();
while(lb<=ub)//checking done to see if lowerboud is lesser than
upperbound
{
p=(lb+ub)/2;//position determined
if(m[p]<ns)
lb=p+1;//lowerboud increased
if(m[p]>ns)
ub=p-1;//upperboud increased
if(m[p]==ns)
{
k=1;//element found
break;
}
}
if(k==1)
System.out.print("The element is found");
else
System.out.print("The element is not found");

}
}
PROGRAM 9

import java.util.*;
class rcsum
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int i,j,r,c;
int a[][]=new int [4][4];
System.out.println("Enter the numbers in the matrix");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
a[i][j]=sc.nextInt();//numbers are input in the matrix
}
}
System.out.println("The numbers in the matrix are");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(a[i][j]+" ");//matrix is displayed
}
System.out.println();
}
System.out.println("The sum of elements of each rows");
for(i=0;i<4;i++)
{ r=0;//row is initialised
for(j=0;j<4;j++)
{
r=r+a[i][j];//sum of elements in row
}
System.out.println("The sum of elements in row " + (i+1)+ "is " +r);
}
System.out.println("The sum of elements of each column");
for(i=0;i<4;i++)
{ c=0;//column is initialised
for(j=0;j<4;j++)
{
c=c+a[i][j];//sum of elements in column
}
System.out.println("The sum of elements in column " +(i+1)+" is "
+c);
}
}
}
PROGRAM 10

import java.util.Scanner;
class HotelRoom
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int[][] rs = new int[10][50];
while (true)
{
System.out.print("Enter your name: ");
String name = scanner.nextLine();
int f,r=1;
boolean roomFound = false;
for (f=0; f<=10; f++)

{
for (r=0; r<50; r++)
{
if (rs[f][r] == 0)
{
roomFound = true;
break;
}
}
if (roomFound)
{
break;
}
}
if (roomFound)
{
rs[f][r] = 1;
System.out.println("Room allocated on floor" + f + ", room " + r +
".");
System.out.println("Name: " + name);
System.out.println("Room number: " +(f* 100+ r));

}
else
{
System.out.println("Sorry, no rooms available.");
}
System.out.println("Press 1 to allocate another room, or any other key
to exit: ");
String choice = scanner.nextLine();
if (!choice.equals("1"))
{
break;
}
}
scanner.close();
}
}
CONCLUSION
ACKNOWLEDGEMENT
BIBLIOGRAPHY

You might also like