0% found this document useful (0 votes)
28 views60 pages

COMP PROGRAMS 10TH FINALS

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
28 views60 pages

COMP PROGRAMS 10TH FINALS

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 60

Q1) Write a program in Java to generate the following

pattern:
A
AB
ABC
ABCD
ABCDE
//program to generate a pattern

public class Pattern1

public static void main()

int i,j;

for(i=65;i<=69;i++)

for(j=65;j<=i;j++)

System.out.print((char)j);

System.out.println();

}
Output:

Variable name Data type Description


main() - -
i int To iterate through
range of rows
j int To iterate through
range of columns

Q2) Write a program in Java to generate the following


patterns:
A
BB
CCC
DDDD
EEEEE
//program to generate a given pattern

public class Pattern2


{

public static void main()

int i,j;

for(i=65;i<=69;i++)

for(j=65;j<=i;j++)

System.out.print((char)i);

System.out.println();

Output:
Variable name Data type Description
main() - -
i int To iterate through
range of rows
j int To iterate through
range of columns

Q3) Write a program in Java to input a character. Find and


display the next 10th character in the ASCII table.
//program to display characters

import java.util.*;

public class dis

public static void main(String args[])

Scanner in= new Scanner(System.in);

char cht;

int i,j;

System.out.println("Enter a character in Uppercase");

cht= in.next().charAt(0);

int n= (int)cht;

for(i=n+1;i<n+11;i++)

System.out.println((char)i);
}

Output:

Variable name Data type Description


main() - -
cht char To accept a character
i int Loop variable to
iterate through range
of 10
j int Loop variable
n int To convert character
in ASCII code

Q4) Write a program to accept 10 different numbers in a Single


Dimensional Array (SDA). Now, enter a number and by using
binary search technique, check whether or not the number is
present in the list of array elements. If the number is present
then display the message “Search successful” otherwise,
display “Search unsuccessful”.
//program to check a number

import java.util.*;

public class binary

public static void main(String args[])

Scanner in= new Scanner(System.in);

int i,k=0,p=0,lb=0,ub=9,ns;

int m[]=new int[10];

System.out.println("Enter the numbers in ascending order");

for(i=0;i<10;i++)

m[i]= in.nextInt();

System.out.println("Enter the number to be searched:");

ns= in.nextInt();

while(lb<=ub)

p=(ub+lb)/2;

if(m[p]<ns)

{
lb=p+1;

if(m[p]>ns)

ub=p-1;

if(m[p]==ns)

k=1;

break;

if(k==1)

System.out.println("The search successful and the number is


present");

else

System.out.println("The search unsuccessful and the number is not


present");

}
Output:

Variable Data type Description


main() - -
i int Loop variable to iterate
through a range of 10
k int Flag variable to check if
number is present or
not
p int Stores the middle
element of the array
lb int Stores the first
element of the array
ub int Stores the last element
of the array
ns int To store the number
which is to be searched
m int[] Variable of an array to
store 10 numbers
Q5) Write a program in Java to store the numbers in 4*4 matrix
in a Double Dimensional Array. Find the sum of the numbers of
the left diagonal and the sum of the numbers of the right
diagonal of the matrix by using an input statement.
//program to store numbers

import java.util.*;

public class Diag

public static void main(String args[])

Scanner in= new Scanner(System.in);

int i,j,ld,rd,k;ld=0;rd=0;

int m[][]=new int[4][4];

System.out.println("Enter the numbers of the matrix");

for(i=0;i<4;i++)

for(j=0;j<4;j++) m[i]

[j]= in.nextInt();

System.out.println("The numbers of the matrix are:");

for(i=0;i<4;i++)

{
for(j=0;j<4;j++)

System.out.print(m[i][j]+" ");

System.out.println();

for(i=0;i<4;i++)

ld= ld+m[i][i];

System.out.println("The sum of the left diagonal elements="+ld);

k=3;

for(i=0;i<4;i++)

rd= rd+m[i][k];

k=k-1;

System.out.println("The sum of the right diagonal elements="+rd);

}
Output:

Variable Data type Description


main() - -
i int Loop variable for rows
in the matrix
j int Loop variable for
columns in the matrix
ld int To store sum of left
diagonal elements
rd int To store sum of right
diagonal elements
k int Counter for accessing
elements in the right
diagonal
m int[][] 2D array to store the
matrix
Q6) Write a program in Java to create a 3*3 Square Matrix and
store numbers in it. The programmer wants to check whether
the Matrix is Symmetric or not. A Square Matrix is said to be
Symmetric if the element of the ith row and jth column is equal
to the element of the jth row and ith column.
//Program to check a matrix

import java.util.*;

public class sym

public static void main(String args[])

Scanner in= new Scanner(System.in);

int i,j,k;k=0;

int m[][]= new int[3][3];

int n[][]= new int[3][3];

System.out.println("Enter the numbers of the matrix:");

for(i=0;i<3;i++)

for(j=0;j<3;j++)

m[i][j]=in.nextInt();
}

System.out.println("The numbers of the matrix are:");

for(i=0;i<3;i++)

for(j=0;j<3;j++)

System.out.print(m[i][j]+" ");

System.out.println();

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(m[i][j]!=m[j][i])

k=1;

if(k==0)
System.out.println("It is a Symmetric Matix");

else

System.out.println("It is not a Symmetric Matrix");

Output:

Variable Data type Description


main() - -
i int Loop variable for rows
in the matrix
j int Loop variable for
columns in the matrix
k int Flag variable to
indicate whether
matrix is symmetric or
not
m int[][] 2D array to store the
input matrix
n int[][] Unused 2D array

Q7) Write a program in Java to store 20 numbers (even and


odd) in a Single Dimensional Array (SDA). Calculate and display
the sum of all even numbers and all odd numbers separately.
//program to store and calculate sum

import java.util.*;

public class sumSDA

public static void main(String args[])

Scanner in= new Scanner(System.in);

int evenSum= 0;

int oddSum= 0;

int num[]= new int[20];

System.out.println("Enter 20 numbers (even and odd):");

for(int i=0;i<20;i++)

num[i]= in.nextInt();

if(num[i]%2==0)

evenSum+= num[i];
}

else

oddSum+= num[i];

System.out.println("Sum of even numbers:"+evenSum);

System.out.println("Sum of odd numbers:"+oddSum);

Output-
Variable Name Data type Description
main() - -
evenSum int To store sum of all even
numbers
oddSum int To store sum of all odd
numbers
num int[] Variable of an Array of
size 20 to store the
numbers
i int Loop variable to iterate
through a range of 20
Q8) Write a program in Java to store 20 numbers in a Single
Dimensional Array (SDA). Display the numbers which are prime.
//program to display prime numbers

import java.util.*;

public class primeno

public static void main(String args[])

Scanner in= new Scanner(System.in);

int i,j,c;

int m[]= new int[20];

System.out.println("Enter numbers in the cell");

for(i=0;i<20;i++)

m[i]= in.nextInt();

System.out.print("Prime numbers are:");

for(i=0;i<20;i++)

c=0;

for(j=1;j<=m[i];j++)

if(m[i]%j==0)
c=c+1;

if(c==2)

System.out.print(" "+m[i]+",");

Output:
Variable Data type Description
main() - -
i int Loop variable to store
and check a number
j int Inner loop variable to
check if a number is
prime or not
c int Counting variable
m int[] Variable of an array to
store 20 numbers

Q9) Write a program to accept a string and display:


1. The number of lowercase characters
2. The number of uppercase characters
3. The number of special characters
4. The number of digits present in the string
Sample input: S.T.D code of New Delhi – 011
Sample output:
The number of lowercase characters = 12
The number of uppercase characters = 5
The number of special characters = 9
The number of digits present in the string = 3
//program to count and display characters in a string

import java.util.*;

public class special

public static void main(String args[])

Scanner in= new Scanner(System.in);

int a,p;

int up=0,lc=0,d=0,spl=0;

String st;

char ch;

System.out.println("Enter string:");

st= in.nextLine();

p= st.length();

for(a=0;a<p;a++)
{

ch= st.charAt(a);

if(ch>='a'&&ch<='z')

lc= lc+1;

else if(ch>='A'&&ch<='Z')

up= up+1;

else if(ch>='0'&&ch<='9')

d= d+1;

else

spl= spl+1;

System.out.println("The Output:");

System.out.println("The number of lowercase characters:"+lc);

System.out.println("The number of uppercase characters:"+up);

System.out.println("The number of special characters:"+spl);

System.out.println("The number of digits present in the string:"+d);

Output:
Variable name Data type Description
main() - -
a int Loop variable for
characters
p int To store length of
string
up int To store uppercase
characters
lc int To store lowercase
characters
d int To store digits
spl int To store special
characters
st String To accept string
ch char To store characters of
string

Q10) Write a program to accept a name and display the


initials along with the surname.
Sample Input: Mohandas Karamchand Gandhi
Sample Output: M. K. Gandhi
//program to enter full name and print initials with surname

import java.util.*;

public class Surname

public static void main(String args[])

Scanner in= new Scanner(System.in);

String st,sn,st1="",st2="";

int i,p;

char ch;

System.out.println("Enter the full name:");

st= in.nextLine();

st=' '+st;

p= st.lastIndexOf(' ');

sn= st.substring(p);

for(i=0;i<p;i++)

ch= st.charAt(i);

if(ch==' ')

st1= st1+st.charAt(i+1)+'.';

}
st2= st1+sn;

System.out.println("Initials with surname:");

System.out.println(st2);

Output:

Variable name Data type Description


main() - -
st String To accept name
sn String To store surname
st1 String To store initials
st2 String To store the new
name with initials
and suranme
i int To iterate through
range of string
p int To store length of
string
ch char To extract and store
characters of string
Q11) A string is said to be Unique, if none of the letters
present in the string are repeated. Write a program to
accept a string and check whether the string is Unique or
not. The program displays a message accordingly.
Sample Input: COMPUTER
Sample Output: Unique String
//program to check unique strings

import java.util.*;

class Unique_String

public static void main(String args[])

Scanner in= new Scanner(System.in);

String str;

int i,j,p,k=1;

System.out.println("Enter a word:");

str= in.next();

str= str.toUpperCase();

p= str.length();

for(i=0;i<p;i++)

for(j=i+1;j<p;j++)

{
if(str.charAt(i)==str.charAt(j))

k=0;

if(k==1)

System.out.println(str+" is a Unique string");

else

System.out.println(str+" is not a Unique string");

Output:
Variable name Data type Description
main() - -
str String To accept a word
from user
i int To iterate through
range of length of
string
j int Loop variable to
compare two
successive characters
p int To store length of
string
k int Flag variable to
detect a unique string

Q12) Write a program to store names of 20 students and


their telephone numbers correspondingly in two single
dimensional arrays. Enter a name separately and search it in
the given list of names. If it is present then display the name
with its telephone number otherwise, display a message
“Name not found”. Use linear search technique.
//to search name and corresponding telephone

import java.util.*;

class Name_Telephone

public static void main(String[] args)

Scanner in = new Scanner(System.in);


int i, a = 0, k = 0;

String b = "";

String[] m = new String[20];

Long[] n = new Long[20];

for (i = 0; i < 20; i++)

System.out.println("Enter name one by one:");

m[i] = in.nextLine();

for (i = 0; i < 20; i++)

System.out.println("Enter Telephone number of " + m[i] + ":");

n[i] = in.nextLong();

in.nextLine();

System.out.println("Enter the name to be searched:");

String name = in.nextLine();

for (i = 0; i < 20; i++)

if (m[i].equalsIgnoreCase(name))

k = 1;
a = i;

b = m[i];

break;

if (k == 1)

System.out.println("The name is " + b + " and the telephone


number is: " + n[a]);

} else

System.out.println(name + " is not found at any location.");

}
Output:
Variable name Data type Description
main() - -
i int Loop variable to
accept 20 names and
telephone numbers
a int Copy variable for
printing the name
and number
k int Flag variable to
detect the name
entered
b String To interchange the
names in loop
m String[] Array to store 20
names
n Long[] Array to store 20
telephone numbers

Q13) Write a program to input a number and check and


print whether it is a ‘Pronic’ number or not. Use a method
int Pronic(int n) to accept a number. The method returns 1,
if the number is Pronic, otherwise zero (0).
//Program to check pronic number

import java.util.*;

public class Pronicc

int Pronic(int n)

int f=0;

for(int i=1;i<=(n-1);i++)

if(i*(i+1)==n)

f= 1;

break;

return (f);

}
public static void main(String args[])

Scanner in= new Scanner(System.in);

System.out.println("Enter your number");

int m= in.nextInt();

Pronicc ob= new Pronicc();

int a;

a= ob.Pronic(m);

if(a==1)

System.out.println("1");

else

System.out.println("0");

}
Output:

Variable Name Data type Description


main() - -
f int Flag variable
i int Loop variable to
iterate through 1 and
the entered number
n int Accepts the number
from main function
m int To accept number
from user
a int Variable to call
method Pronic
ob Pronicc Object of class Pronic
to call function

Q14) Write a class with the name area using method


overloading that computes the area of a parallelogram, a
rhombus and a trapezium.
Area of parallelogram (pg) = base*ht
Area of rhombus (rh) = ½*d1*d2 [where d1 and d2 are the
diagonals of rhombus]
Area of trapezium (tr) = ½*(a+b)*h [where a and b are
parallel sides and h is the perpendicular distance between
them]
//Program to calculate areas

import java.util.*;

public class Area

void calculate(int base,int ht)

double area= base*ht;

System.out.println("The area of parallelogram="+area);

void calculate(double d1,double d2)

double area= 0.5*d1*d2;

System.out.println("The area of rhombus="+area);

void calculate(int a,int b,int h)

double area= 0.5*(a+b)*h;

System.out.println("The area of trapezium="+area);

public static void main(String args[])


{

Scanner in= new Scanner(System.in);

int base,ht;

double d1,d2;

int a,b,h;

System.out.println("Enter value of base and height of


parallelogram");

base= in.nextInt();

ht= in.nextInt();

System.out.println("Enter value of diagonals of rhombus");

d1= in.nextDouble();

d2= in.nextDouble();

System.out.println("Enter value of parallel sides of trapezium


and perpendicular distance between them");

a= in.nextInt();

b= in.nextInt();

h= in.nextInt();

Area ob= new Area();

ob.calculate(base,ht);

ob.calculate(d1,d2);

ob.calculate(a,b,h);

}
Output:

Variable name Data type Description


main() - -
area double To calculate area
base int To accept base of
parallelogram
ht int To accept height of
parallelogram
d1 double To accept diagonal of
rhombus
d2 double To accept diagonal of
rhombus
a int To accept value of
parallel side of
trapezium
b int To accept value of
parallel side of
trapezium
h int To accept value of
height between the
parallel sides of
trapezium
ob Area Object of class area to
call the functions
Q15) Define a class Salary with the following specifications:
Class name: Salary
Data members: Name, Address, Phone, Subject,
Specialization, Monthly Salary, Income tax.
Member methods:
1) to accept the details of a teacher including the monthly
salary
2) to compute the annual income tax at 5% of the annual
salary exceeding Rs. 1,75,000.
Write a main method to create object of the class and call
the above member methods.
//program to display details of teacher

import java.util.*;

class Salary

String name,add,sub;

long ph;

double sal,tax;

void accept()

Scanner in = new Scanner(System.in);

System.out.println("Enter name ");

name = in.nextLine();
System.out.println("Enter Address ");

add = in.next();

System.out.println("Enter the Phone Number ");

ph = in.nextLong();

System.out.println("Enter Subject Specialization ");

sub = in.next();

System.out.println("Enter the monthly Salary ");

sal = in.nextDouble();

void calculate()

if(12*sal>175000)

tax=((sal*12)-175000)*5/100;

else

tax=0;

void display()

System.out.println("Name : "+name);

System.out.println("Address : "+add);

System.out.println("Phone Number : "+ph);

System.out.println("Subject Specialization ;"+sub);


System.out.println("Monthly Salary :Rs." +sal);

System.out.println("Income Tax :Rs." +tax);

public static void main(String args [])

Salary ob=new Salary();

ob.accept();

ob.calculate();

ob.display();

Output:
Variable name Data type Description
name String To accept name
add String To accept address
sub String To accept specialized
subject
ph long To accept phone
number
sal double To accept the monthly
salary
tax double To calculate and store
income tax
main() - -
Salary ob Object to call functions

Q16) Define class ElectricityBill with the following


specifications:
Class name: ElectricityBill
Data members/Instance variables:
String n: strores the name of the customer
int units: stores the number of units consumed
double bill: stores the amount to be paid
Member methods:
void accept( ) : to input the name of the customer and the
number of units consumed
void calculate( ) : to calculate the bill as per the following
tariff :
Number of units Rate per unit
First 100 units Rs.2.00
Next 100 units Rs.3.00
Above 300 units Rs.5.00

A surcharge of 2.5% is charged if the number of units


consumed is above 300 units.
void print( ) : to display the details, as follows:
Name of the customer:
Number of units consumed:
Bill amount:
Write a main method to create an object of the class and
call the above member methods.
//program to calculate electricity bill

import java.util.*;

public class ElectricityBill

String name;

int ut;

double amt=0.0,scg=0;

Scanner in = new Scanner(System.in);

void accept()

{
System.out.println("Enter the Name of Consumer: ");

name = in.nextLine();

System.out.println("Enter the units consumed: ");

ut = in.nextInt();

void calculate()

if(ut<=100)

amt= ut*2.0;

if(ut>100&& ut<=300)

amt= 200+(ut-100)*3.0;

if(ut>300)

scg= ut*2.5/100.0;

amt= scg+(200+600+(ut-300)*5.0);

void print()

System.out.println("Name of the Consumer: "+name);

System.out.println("Numer of units consumed: "+ut);

System.out.println("Bill Amount :Rs."+amt);


}

public static void main(String args[])

ElectricityBill ob = new ElectricityBill();

ob.accept();

ob.calculate();

ob.print();

Output:

Variable name Data type Description


name String To accept name
ut int To accept units
consumed
amt double To calculate and store
the amount
scg double To store the
surcharge value
main() - -
ElectricityBill ob Object to call the
functions

Q17) An electronic shop has announced a special discount


on the purchase of laptops as given below:
Category Discount on Laptop
Up to Rs.25,000 5.0%
Rs.25,001 - Rs.50,000 7.5%
Rs.50,000 - Rs.1,00,000 10.0%
More than Rs.1,00,000 15.0%

Define a class Laptop described as follows:


Data members/Instance variable: name, price, amt, dis;

Member methods:
1) A parameterized conductor to initialize the data
members
2) To accept the details (name of customer and price)
3) To compute the discount
4) To display the name, discount and amount to be paid
after discount.
Write a main method to create an object of the class and
call the member methods.
//Program to compute discounts on laptop

import java.util.*;
public class Laptop

String name;

double price;

double dis= 0.0;

double amt= 0.0;

Laptop(String n,double pr)

name= n;

price= pr;

void accept()

System.out.println("Customer name: "+name);

System.out.println("Price: "+price);

void compute()

if(price<=25000)

dis= price*5.0/100.0;

if(price>=25001&&price<=50000)

dis= price*7.5/100.0;
if(price>=50001&&price<=100000)

dis= price*10.0/100.0;

if(price>100000)

dis= price*15.0/100.0;

void display()

amt= price-dis;

System.out.println(" Bill ");

System.out.println("Customer name:"+name);

System.out.println("Discount on laptop:"+dis);

System.out.println("Amount:"+amt);

public static void main(String args[])

Scanner in= new Scanner(System.in);

String nm;

double prc;

double disc,amo;

System.out.println("Enter customer name and price of laptop");

nm= in.nextLine();

prc= in.nextDouble();
Laptop ob= new Laptop(nm,prc);

ob.accept();

ob.compute();

ob.display();

Output :

Variable name Data type Description


main() - -
name String Stores name of
customer
price double Stores price of laptop
dis double To calculate and store
discount on the price
amt double To calculate and store
amount of the laptop
after discount
nm String Accepts name of
customer from user
prc double Accepts price of
laptop from user
ob Laptop Object of class Laptop
to call its functions

Q18) Define a class Arrange described as below:


Data members/Instance variables:
String str (a word), I, p (to store the length of the word),
char ch;

Member methods:
1) A parameterized constructor to initialize the data
member
2) To accept the word
3) To arrange all the alphabets of word in ascending order
of their ASCII values without using the sorting
technique
4) To display the arranged alphabets.
Write a main method to create an object of the class and
call the above member methods.
//Program to arrange alphabets

import java.util.*;

public class Arrange

String str;
int p;

Arrange(String word)

str = word;

p = str.length();

void acceptWord(String word)

str = word;

p = str.length();

void arrangeAndDisplayAlphabets()

char[] arr = str.toCharArray();

for (int i = 0; i < arr.length; i++)

for (int j = i + 1; j < arr.length; j++)

if (arr[i] > arr[j])

char temp = arr[i];

arr[i] = arr[j];
arr[j] = temp;

System.out.println("Arranged word: " + new String(arr));

public static void main(String[] args)

Scanner in = new Scanner(System.in);

System.out.println("Enter a word: ");

String word = in.nextLine();

Arrange ob = new Arrange(word);

ob.arrangeAndDisplayAlphabets();

Output :
Variable name Data type Description
main() - -
str String Stores word entered
by user
p int Stores length of word
arr char[] Array of characters
i int Loop variable for
iterating through
character array
j int Loop variable for
iterating through
character array
temp char Temporary variable
used for swapping
characters
word String Accepts the word
input by user
ob Arrange Object of class
Arrange to call
functions

Q19) Write a program by using a class with the following


specifications:

Class name: Factorial


Data members : private int n
Member methods :
void input() : to input a number
void fact() : to find and print the factorial of the number.
Use a main function to create an object and call member
methods of the class.
//program to find factorial

import java.util.*;

public class Factorial

private int n;

void input()

Scanner in= new Scanner(System.in);

System.out.println("Enter the number");

n= in.nextInt();

void fact()

int f= 1;

for(int i=1;i<=n;i++)

f=f*i;

System.out.println(" The Factorial of "+ n +" is "+ f);

public static void main(String args[])


{

Factorial ob= new Factorial();

ob.input();

ob.fact();

Output:

Variable Name Data type Description


main() - -
n int Stores the number for
which the factorial is to
be calculated
f int Stores the calculated
factorial
i int Loop variable to iterate
from 1 to n while
calculating the factorial
ob Factorial An object of the
factorial class used to
call the member
methods input and fact

Q20) Write a class program in java with the following


specifications:
Class name: Matrix
Data Members : int array m[][] with 3 rows and 3 columns
Member Methods:
void getdata(): to accept the numbers in the array
void rowsum(): to find and print the sum of the numbers of
each row
void colsum(): to find and print the sum of numbers of each
column
Use a main function to create an object and call member
methods of the class.

//program to create a matrix

import java.util.*;

public class Matrix

int m[][]= new int[3][3];

void getdata()

Scanner in= new Scanner(System.in);


System.out.println("Enter numbers in the array");

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

m[i][j]= in.nextInt();

void rowsum()

System.out.println("The sum of numbers of each row:");

for(int i=0;i<3;i++)

int r=0;

for(int j=0;j<3;j++)

r= r+m[i][j];

System.out.println("The sum of numbers of "+(i+1)+" row= " +r);

}
void colsum()

System.out.println("The sum of the numbers of each column:");

for(int i=0;i<3;i++)

int c= 0;

for(int j=0;j<3;j++)

c= c+m[j][i];

System.out.println("The sum of the numbers of "+(i+1)+" column=


" +c);

public static void main(String args[])

Matrix ob= new Matrix();

ob.getdata();

ob.rowsum();

ob.colsum();

}
Output:

Variable Name Data type Description


main() - -
m int[][] Array of size 3 rows
and 3 columns to
accept and store
numbers
i int Loop variable to
iterate through the
rows of the matrix
j int Loop variable to
iterate through the
columns of the matrix
r int To store the sum of
numbers of each row
c int To store the sum of
numbers of each
column
ob Matrix Object of class Matrix

You might also like