Computer Project File
Computer Project File
FILE
Name: Debarghya
Acharya
Class: 10
Section: A (D.S)
Term: 2024-25
Scholar ID: 13-0045
Subject: Computer
Applications
Acknowledgement
pg. 1
I would like to express my special thanks of gratitude to my
teacher Smt. Debanjana Das as well as our principal Mrs.
Anasuya Paul who gave me the opportunity to do this
wonderful project. It helped me to understand and have a clear
idea of the various programs. I am really thankful to them.
Finally, I would also like to thank my parents who helped me a
lot in finalizing this project within the limited time frame.
Debarghya Acharya,
X-A (D.S)
5/11/2024
pg. 2
Index
1. String Handling 4 - 18
2. USD 19 – 27
4. Array 43 – 67
5. Class IX Programmes
b) Number Generation 77 – 84
Program
c) Slab-Program 85 – 88
6. Conclusion 89
7. Bibliography 90
String Handling: -
pg. 3
Q 1.) Write a program in Java to accept a string in lower case
and change the first letter of every word to upper case. Display
the new string.
Sample input: we are in cyber world
Sample output: We Are In Cyber World
Program code:
import java.util.Scanner;
public class words{
public static void main(String str){ //enter a string
String str=””;
int I,p;
char chr ;
str=’ ‘+str;
p=str.length();
for(i=0; i<p; i++){
chr=str.charAt(i);
str1=str1+’ ‘+Character.toUpperCase(chr1);
i=i+1;
}// end of for
else
str1=str1+chr;
}
System.out.println(“new string after changing the case pf the
first letter of the first words in the string:”);
pg. 4
System.out.println(str1);
} // end of main
} //end of class
VDT
Variable Description Purpose
I int To find the index number
chr int To extract the characters of
the string
str int To enter the string
str1 int To calculate the new string
p int To calculate length of str
Output:
pg. 5
To translate word into a Piglatin word, convert the word into
uppercase and then place the first vowel of the original word as
the start of the new word along with the remaining alphabets.
The alphabets present before the vowel being shifted towards
the end followed by “AY”.
Program code:
public class piglatin {
public static void main(String str)//enter a string
{
int x, y;
String str1, str2;
char ch;
x=str.length();
System.out.println(“Enter a word only:”);
for(y=0;y<x;y++){
ch=(str.charAt(y));
if(ch==’a’||ch==’e’||ch==’I’||ch==’o’||ch==’u’||ch==’A’||ch==’E’||
ch==’I’||ch==’O’||ch==’U’)
break;
pg. 6
}
str1=str.substring(y,x);
str2=str.substring(0,y);
System.out.println(str1+str2+”ay”);
}
}
VDT
Variable Description Purpose
str String To enter a word
str1 String To extract the last part of
the word
str2 String To extract the word upto
the first vowel
ch char To extract the characters
x int To calculate index
number
y int To calculate length of str
Output:
pg. 7
Sample Input : computer
Sample Output : cpmpvtfr
Program code:
import java.util.Scanner;
public class word
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a word: ");
String str = in.nextLine();
str = str.toLowerCase();
String newStr = "";
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (str.charAt(i) == 'a' || str.charAt(i) ==’e’|| str.charAt(i) == 'i' ||
str.charAt(i) == 'o' ||str.charAt(i) == 'u') {
char nextChar = (char)(ch + 1);
newStr = newStr + nextChar;
}
else {
newStr = newStr + nextChar;
pg. 8
}
}
System.out.println(“Output:”+ newStr);
} //end of main
} //end of class
VDT
Variable Description Purpose
str String To accept a word
newStr String To calculate the new word
len int To calculate the length of str
i int To find the index number
ch char To extract the characters of
str
nextChar char To extract the characters
next to a vowel to replace
them with the next alphabet
Output:
Q.4) WAP to accept a name (in first name & middle name and
last name format), then display that name in short format.
Input :: SURYA KIRAN KANJILAL
pg. 9
Output :: S. K. KANJILAL
Program code:
import java.util.Scanner;
public class initial {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println(“Enter your name(First Middle Last):”);
String st=in.nextLine(); //accepting full name
st=’ ‘+st; String sn, s2=””,s1=””;
int p=lastIndexOf(‘ ‘);
sn=st.substring(p);
for(int i=0;i<p;i++){
char chr=st.charAt(i);
if(chr==’ ‘)
s1=s1+st.charAt(i+1)+’.’;
}
s2=s1+sn;
System.out.println(“Output:”);
System.out.println(st2);
}//end of main
}//end of class
pg. 10
VDT
Variable Description Purpose
st String To enter the full name
s1 String To find the initials of first and
middle name
s2 String To find the name in initials
sn String To extract the surname
p Int To calculate the length of st
i Int To calculate the index number
chr char To extract the characters of st
Output:
pg. 11
1. To generate and print Letters from A to Z and their Unicode
Letters Unicode
Program code:
import java.util.Scanner;
public class initial
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println(“Enter your choice”);
int ch=sc.nextInt();
switch(ch){
case 1:
System.out.println(“UNICODE”+”\t”+”LETTER”);
for(int i=65;i<=90;i++){
System.out.print(i+”\t”+(char)i+”\n”);
}
pg. 12
break;
case 2:
String st=”BLUEJ”;
int p=st.length();
for(int i=l-1;i>=0;i--){
for(int j=0;j<=i;j++){
System.out.println(st.charAt(j));
}
System.out.println();
break;
default:
System.out.println(“invalid choice”);
} //end of switch
} //end of main
} //end of class
VDT
Variable Description Purpose
ch char As switch variable
i int To calculate index
number
st String To contain a word
p int To find length of st
j int As a counter variable in
the loop
pg. 13
Output:
pg. 14
Q.6) Write a program to accept a sentence. Display the
sentence in reversing order of its word.
Sample Input: Computer is Fun
Sample Output: Fun is Computer
Program code:
import java.util.Scanner;
public class reverse
{
System.out.println("Enter a sentence:");
char ch = str.charAt(i);
pg. 15
word = ch + word;
}
} //end of for
} //end of main
} //end of class
VDT
Variable Description Purpose
str String To enter a sentence
word String To interchange the words
of str
i int To calculate the index
number
len int To find the length of str
ch char To extract the characters
of str
Output:
pg. 16
Q.7) A non-palindrome word can be made a palindrome word
just by adding the reverse of the word to the original word.
Write a program to accept a non-palindrome word and display
the new word after making it a palindrome.
Sample Input : ICSE
Sample Output :The new word making it palindrome as:
ICSEESCI
Program code:
import java.util.Scanner;
public class palindrome{
System.out.println(“enter a string:”);
String s=sc.next(), s2="";
int p=s.length();
for(int i=p-1;i>=0;i--){
char b=s.charAt(i);
s2=s2+b;
} //end of for
System.out.println("palindrome string:"+s+s2);
} //end of main
} //end of class
pg. 17
VDT
Variable Description Purpose
s String To accept a word
s2 String To make s a palindrome
word
p int To find the length of s
i int To calculate the index
number
b char To extract the characters of
s
Output:
Program code:
public class Series {
double series(double n) {
double sum = 0.0;
for (int i = 1; i <= n; i++) {
sum = sum+(1.0/i);
}
return (sum);
}
double series(double a, double n) {
double sum = 0.0;
int x = 1;
for (int i = 1; i <= n; i++) {
int e = x + 1;
double term = x / Math.pow(a, e);
sum += term;
x += 3;
}
return (sum);
pg. 19
}
public static void main(String args[]) {
Series obj = new Series();
System.out.println("First series sum = " + obj.series(5));
System.out.println("Second series sum = " + obj.series(3,8));
}
}
VDT
Variable Description Purpose
a double To input the value of a
n double To input the last term of the
series
x int
e int
sum double To calculate the sum of the
series
term double To calculate the terms of
the series
i int As a counter variable in the
loop
Output:
pg. 20
Q.9) Design a class to overload a function compare() as
follows:
void compare(int, int) — to compare two integer values and
print the greater of the two integers.
void compare(char, char) — to compare the numeric value of
two characters and print the character with higher numeric
value.
void compare(String, String) — to compare the length of the
two strings and print the longer of the two.
Program code:
import java.util.Scanner;
public class compare{
void compare(int a, int b){
int c=Math.max(a,b);
System.out.println(c);
} //end of method
void compare (char x, char y){
int p = (int)x ;
int q = (int)y ;
int r=Math.max(p,q);
System.out.println((char)r);
} //end of method
void compare(String s, String t){
int l1= s.length();
pg. 21
int l2= t.length();
if (l1>l2)
System.out.println(s);
else if (l2>l1)
System.out.println(t);
}//end of method
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter your choice 1, 2, or 3:");
int ch= sc.nextInt();
Compare ob = new Compare(); //creation of object to call the
member methods
sc.nextLine();
switch(ch) {
case 1:
System.out.println("Enter 2 integer values: ");
int m=sc.nextInt();
int n= sc.nextInt();
ob.compare (m, n);
break;
case 2:
System.out.println("Enter 2 character literals:");
char g = sc.next().charAt(0);
char h = sc.next().charAt(0);
ob.compare(g, h);
pg. 22
break;
case 3:
System.out.println("Enter 2 Strings:");
String v = sc.nextLine();
String w = sc.nextLine();
ob.compare(v, w);
break;
default:
System.out.println(“invalid choice”);
} //end of switch
} //end of main
} //end of class
VDT
Variable Description Purpose
a int To input a number
b int To input a number
c int To find the larger number
between a and b
x char To input a character
y char To input a character
m int To store the value of a
n int To store the value of b
g char To store the value of x
h char To store the value of y
p int To store integer value of x
pg. 23
q int To store the integer value of y
r int To find the character of higher
ASCII value
s String To enter a String
t String To enter a String
v String string value of s
w String string value of t
ch int Switch variable
l1 int To find the length of s
l2 int To find the length of t
Output:
pg. 24
Q.10) Define a class to overload the function calculate() as
follows:
void calculate() - to print the following format
1111
2222
3333
4444
5555
void calculate (int n) - To check whether the number is an
automorphic number.
[A lead number is the one whose sum of even digits are equal
to sum of odd digits. e.g. 3669 odd digits sum = 3 + 9 = 12
even digits sum = 6 + 6 = 12 3669 is a lead number.]
Program code:
import java.util.*;
public class pattern{
void calculate(){
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=4;j++)
System.out.print(i);
System.out.println();
pg. 25
}// end of for
} //end of method
void calculate(int n){
int r,cpy=n,e=0,o=0;
while(n>0){
r=n%10;
n=n/10;
if(r%2==0)
e=e+r;
else
o=o+r;
}
if(e==o)
System.out.println(cpy+"is a lead number");
else
System.out.println(cpy+"is not a lead number");
} //end of method
public static void main(String[]args){
pattern ob=new pattern();
Scanner sc=new Scanner(System.in);
System.out.println("enter n");
ob.calculate();
ob.calculate(n);
} //end of main
} //end of class
pg. 26
VDT
Variable Description Purpose
i int As a counter variable in the
loop
j int As a counter variable in the
loop
n int To enter a number
r int To extract the digits of n
e int Sum of the even digits of n
o int Sum of the odd digits of n
cpy int To copy the value of n
Output:
pg. 27
Designer class programs :-
pg. 28
void accept(): to accept customer name, phone number,
number of units and rent.
void calculate(): to calculate the amount as rent + cost for the
units, where cost for the units is to be calculated according to
the following conditions:
First 50 calls : Free
Next 100 calls : Rs. 1.00/unit
Next 200 calls : Rs. 5.00/unit
Remaining calls : Rs. 10.00/unit
void display(): to display the values of all the data members
Write a main method to create an object and call other member
methods.
Program code:
import java.util.*;
public class PhoneBill{
String cust_name;
int rent, no_of_unit;
long phoneNo; double amount;
PhoneBill(){
cust_name="";
rent=0; no_of_unit=0;
phoneNo=0; amount=0.0;
}
pg. 29
void accept(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the details of customer");
cust_name=sc.nextLine();
rent=sc.nextInt();
no_of_unit=sc.nextInt();
phoneNo=sc.nextLong();
}
void calculate(){
if(no_of_unit<=50)
amount=rent+0;
else if(no_of_unit>50 && no_of_unit<=150)
amount=rent+(1.0*no_of_unit);
else if(no_of_unit>150 && no_of_unit<=350)
amount=rent+(1.0*100)+(5.0*(no_of_unit-150));
else
amount=rent+(1.0*100)+(5.0*200)+(10.0*(no_of_unit-
350));
}
void display(){
System.out.println("Name of customer:"+cust_name);
System.out.println("Phone number of
customer:"+phoneNo);
System.out.println("Rent of telephone:"+rent);
System.out.println("Number of units:"+no_of_unit);
pg. 30
System.out.println("Total bill:"+amount);
}
public static void main(String[]args){
PhoneBill ob=new PhoneBill();
ob.accept();
ob.calculate();
ob.display();
}// end of main
}// end of class
VDT
Variable Description Purpose
cust_name String To enter name of customer
rent int To enter the rent
no_of_unit int To enter the number of units
consumed
phoneNo long To enter the phone number
amount double To calculate the amount
Output:
pg. 31
Q.12) Define a class salary described as below:
Data Members/Instance variables :
name – String - to store the name of a teacher
address – String - to store the address
phone - long - to store the mobile no
subject - String - to store the subject specialization
monthly_salary - double - to store the monthly salary
income_Tax. - double - to store the annual income-tax
Member methods:
salary() – a non-parameterized constructor to set the default
initial values
void accept() - To accept the details of a teacher including the
monthly salary.
pg. 32
void display () - To display the details of the teacher.
void compute() - To compute the annual Income Tax as 5% of
the annual salary above Rs. 1,75,000/-.
Write a main method to create object of a class and call the
above member methods.
Program code:
import java.util.*;
class Salary{
String name,address,subject;
long phone;
double monthly_salary,income_Tax;
void accept(){
Scanner in = new Scanner(System.in);
System.out.println("Enter name, address, phone number,
subject specialization and monthly salary of a teacher");
name=in.nextLine();
address=in.nextLine();
phone=in.nextLong();
subject=in.next();
monthly_salary=in.nextDouble();
}
void calculate()
{
if(12*monthly_salary>175000)
pg. 33
income_Tax=((monthly_salary*12)-175000)*5/100;
else income_Tax=0;
}
void display()
{
System.out.println("Name :" +name);
System.out.println("Address :" +address);
System.out.println("Phone Number :" +phone);
System.out.println("Subject Specialization :" +subject);
System.out.println("Monthly Salary :Rs." +monthly_salary);
System.out.println("Income Tax :Rs." +income_Tax);
}// end of method
public static void main(String args[]) {
Salary ob=new Salary();
ob.accept();
ob.calculate();
ob.display();
}// end of main
}// end of class
VDT
Variable Description Purpose
name String To enter the name
address String To enter address
phone long To enter phone
number
pg. 34
subject String To enter subject
specialization
monthly_salary double To enter monthly
salary
income_tax double To calculate income
tax
Output:
pg. 35
double total-To calculate total marks of the student in the three
subjects
double average -To calculate average of the marks of the three
subjects
Member methods:
void accept() - To accept the details of a student.
void compute() - To compute the average and the maximum out
of three marks.
void display() - To display the name, marks in three subjects,
total and average.
Write a main method to create an object of a class and call the
above member methods.
Program code:
import java.util.*;
class Student {
String name;
int eng,hin,mts;double average,total;
Student(){
name="";
eng=0; hin=0; mts=0;
total=0.0; average=0.0;
}
void accept() {
Scanner sc=new Scanner(System.in);
pg. 36
System.out.println("Enter your name,marks of three
subjects of a student");
name=sc.nextLine();
eng=sc.nextInt();
hin=sc.nextInt();
mts=sc.nextInt();
}
void compute(){
total=(eng+hin+mts);
average=total/3;
}
void display (){
System.out.println("Name of Student :" +name);
System.out.println("Marks in English :" +eng);
System.out.println("Marks in Hindi :" +hin);
System.out.println("Marks in Maths :" +mts);
System.out.println("Total marks :" +total);
System.out.println("Average marks :" +average);
}
public static void main(String[]args){
Student ob= new Student();
ob.accept();
ob.compute();
ob.display();
pg. 37
}// end of main
}// end of class
VDT
Variable Description Purpose
name String To store name of a student
eng int To store marks scored in English
hin int To store marks stored in Hindi
mts int To store marks stored in Maths
total int To calculate the total marks
average double To calculate the average marks
Output:
Data
Purpose
Members
pg. 38
Data
Purpose
Members
Member
Purpose
Methods
pg. 39
Type of
Amount
Coaches
First_AC ₹700
Second_AC ₹500
Third_AC ₹250
Sleeper None
Program code:
import java.util.Scanner;
public class RailwayTicket{
private String name, coach;
private long mobno;
private int amt, totalamt;
private void accept() {
Scanner in = new Scanner(System.in);
pg. 40
System.out.println("Enter name, coach, mobile number
and amount: ");
name = in.nextLine();
coach = in.nextLine();
mobno = in.nextLong();
amt = in.nextInt();
}
private void update() {
if(coach.equals("First_AC"))
totalamt = amt + 700;
else if(coach.equals("Second_AC"))
totalamt = amt + 500;
else if(coach.equals ("Third_AC"))
totalamt = amt + 250;
else if(coach.equals ("Sleeper"))
totalamt = amt;
}
pg. 41
RailwayTicket obj = new RailwayTicket();
obj.accept();
obj.update();
obj.display();
}
}
VDT
Variable Description Purpose
name String To Input name of a
customer
coach String To input the type of
coach
mobno long To input the mobile
number of a
customer
amt int To input the basic
amount of ticket
totalamt int To calculate the
updated total
amount of the
ticket
Output:
pg. 42
ARRAYS :-
pg. 43
Q.15) Write a program to store 6 elements in an array P and 4
elements in an array Q. Now, produce a third array R,
containing all the elements of array P and Q. Display the
resultant array.
Program code:
import java.util.Scanner;
public class array2 {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int P[] = new int[6];
int Q[] = new int[4];
int R[] = new int[10];
int i = 0;
System.out.println("Enter 6 elements of array P:");
pg. 44
}
System.out.println("Elements of Resultant Array R: ");
for (i = 0; i < R.length; i++) {
System.out.print(R[i] + " ");
}
}// end of main
}// end of class
VDT
Variable Description Purpose
i int To calculate the
index values of the
arrays
Output:
pg. 45
Program code:
import java.util.*;
class array3 {
public static void main(String args[]) {
int a[]=new int[10];
Scanner sc=new Scanner(System.in);
System.out.println("Enter array elements : ");
for(int i=0;i<10;i++)
a[i]=sc.nextInt();
int max=a[0], min=a[0], p=0, q=0;
for(int i=0;i<10;i++) {
if(a[i]>max) {
max=a[i];
p=i;
}// end of if block
else
if(a[i]<min) {
min=a[i];
q=i;
}// end of else block
}// end of for
System.out.println("Maximum number : "+max+" INDEX IS :
"+p);
System.out.println("Minimum number : "+min+" Index is :"+q);
}// end of main
pg. 46
}// end of class
VDT
Variable Description Purpose
i int to calculate the
index number
max int to calculate the
maximum number
min int to calculate the
minimum number
p int for the index of the
maximum number
q int for the index of the
minimum number
Output:
pg. 47
Program code:
import java.util.*;
public class linearSearch {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
double arr[ ]=new double[20];
int i ;
System.out.println(“Enter the array elements”);
for(i=0;i<20;i++)
arr[i]=sc.nextDouble();
int len = arr.length;
System.out.print("Enter the number to search: ");
double n = sc.nextDouble();
//linear search
for (i = 0; i < len; i++)
{
if (arr[i] == n)
{
break;
}// end of if
} // end of for
if (i == len) {
System.out.println("Not found");
}
pg. 48
else {
System.out.println("Found");
System.out.println(n + " present at index " + i);
}//end of else
} // end of main
} // end of class
VDT
Variable Description Purpose
i int to calculate the index number
array
len int To calculate length of the
n int To enter the number to be
searched
Output:
pg. 49
I. The average of the total marks obtained by N number of
students.
[average = (sum of total marks of all the students)/N]
II. Deviation of each student’s total marks with the average.
[deviation = total marks of a student – average]
Program code:
import java.util.Scanner;
public class Student {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();
String name[] = new String[n];
int tm[] = new int[n];
int gt = 0;
for (int i = 0; i < n; i++) {
in.nextLine();
System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter total marks of student " + (i+1) +
": ");
tm[i] = in.nextInt();
gt += tm[i];
}
pg. 50
double avg = gt / (double)n;
System.out.println("Average = " + avg);
for (int i = 0; i < n; i++) {
System.out.println("Deviation for " + name[i] + " = "
+ (tm[i] - avg));
}// end of for
}// end of main
}// end of class
VDT
Variable Description Purpose
i int To calculate the index
number
gt int To calculate total
n int To enter the number of
students
avg double To calculate the average of
marks
Output:
pg. 51
Q.19) Write a program to input names of the capitals of 10
different states of India. Arrange these names in ascending
pg. 52
order of alphabets, using bubble sort technique. Print the sorted
array.
Program code:
import java.util.*;
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String arr[] = new String[10];
System.out.println("Enter names of ten states of India:");
for (int i = 0; i < 10; i++) {
arr[i] = in.next();
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j].compareTo(arr[j + 1])>0 ){
String t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}// end of for
pg. 53
}// end of main
}// end of class
VDT
Variable Description Purpose
i int To calculate the index
number
j int As a counter variable
t int As a third variable to
exchange the values of I
and j
Output:
pg. 54
Q.20) Write a program to input twenty elements in an integer
array. Arrange these elements in descending order, using the
selection sort technique.
Program code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int arr[]=new int[20]; //Array Declaration
System.out.println("Enter the elements of the array :");
for(int i=0;i<20;i++) {
arr[i]=sc.nextInt();
}
int temp = 0;
for (int i = 0; i < arr.length; i++){
for (int j = i+1; j < arr.length; j++) {
if(arr[i] < arr[j
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
pg. 55
}
//Displaying elements of array after sorting
System.out.println("Elements of array sorted in
descending order: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
} // end of main
}// end of class
VDT
Variable Description Purpose
i int For the index number
j int As a counter variable
temp int As a third variable to exchange
the values of i and j
Output:
pg. 56
Q.21) Write a program to perform binary search on a list of
integers given below, to search for an element input by the
user. If it is found display the element along with its position,
otherwise display the message "Search element not found".
5, 7, 9, 11, 15, 20, 30, 45, 89, 97.
Program code:
pg. 57
import java.util.*;
public class binary{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int a[]={ 5,7,9,11,15,20,30,45,89,97};
System.out.println("Enter number to be searched");
int n=sc.nextInt();
int ub=9,lb=0,p,k=0;
while(lb<=ub){
p=(lb+ub)/2;
if(a[p]<n)
lb=p+1;
if(a[p]>n)
ub=p-1;
if(a[p]==n){
k=1;
break;
}// end of if
}// end of while
if(k==1)
System.out.println("Search element found");
else
System.out.println("Search element not found");
}//end of main
}//end of class
pg. 58
VDT
Variabl Description Purpose
e
n int To enter the number to be searched
ub int To enter upper index
lb int To enter lower index
p int To calculate middle index of the array
k int As a flag variable
Output:
Program code:
import java.util.*;
public class Transpose {
public static void main(String args[]) {
int i, j;
pg. 59
System.out.println("Enter total rows and columns: ");
Scanner s = new Scanner(System.in);
int m = s.nextInt();
int array[ ][ ] = new int[m][m];
System.out.println("Enter matrix:");
for(i = 0; i < m; i++){
for(j = 0; j < m; j++) {
array[i][j] = s.nextInt();
System.out.print(" ");
}
}
System.out.println("The above matrix before Transpose is ");
for(i = 0; i < m; i++) {
for(j = 0; j < m; j++) {
System.out.print(array[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("The above matrix after Transpose is ");
for(i = 0; i < m; i++)
{
for(j = 0; j <m; j++)
{
System.out.print(array[j][i]+" ");
}
pg. 60
System.out.println(" ");
}//end of outer for
}//end of main
}//end of class
VDT
Variable Description Purpose
i int Counter variable
j int Counter variable
m int To store number of rows and
columns (row no.=column no.)
Output:
pg. 61
pg. 62
Q.23) WAP to input a 2D array of size M*N and display the sum
of its left and right diagonal elements separately.
Program code:
import java.util.*;
public class diagonal{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows and
columns");
int r=sc.nextInt();
int c=sc.nextInt();
int m[][]=new int[r][c];
int i,j,k,ld=0,rd=0;
System.out.println("Enter the numbers of the matrix");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
m[i][j]=sc.nextInt();
System.out.print("");
}
}
System.out.println("The numbers of the matrix are");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
pg. 63
System.out.print(m[i][j]+" ");
}
System.out.println();
}
for(i=0;i<r;i++){
ld=ld+m[i][i];}
System.out.println("Sum of the left diagonal
elements:"+ld);
k=r-1;
for(i=0;i<r;i++){
rd=rd+m[i][k];
k=k-1;
}
System.out.println("Sum of the right diagonal
elements:"+rd);
}
}
VDT
Variable Description Purpose
i int Counter variable in loop
j int Counter variable in the inner loop
c int To store the column number
ld int to calculate sum of elements in left
diagonal
rd int to calculate sum of elements in
right diagonal
k int As a counter
r int To store the row number
pg. 64
Output:
Q.24) WAP to input a 2D array of size M*N and print the sum
and average of all the elements.
Program code:
import java.util.*;
public class array4{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows and
columns");
int r=sc.nextInt();
pg. 65
int c=sc.nextInt();
int m[][]=new int[r][c];
int i,j;
int s=0; double avg=0.0;
System.out.println("Enter the numbers of the matrix");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
m[i][j]=sc.nextInt();
System.out.print("");
}
}
System.out.println("The numbers of the matrix are");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
System.out.print(m[i][j]+" ");
}
System.out.println();
}
for(i=0;i<r;i++){
for(j=0;j<c;j++){
s=s+m[i][j];
avg=s/(r*c);
}//end of inner for
}//end of outer for
System.out.println("Sum of all the elements:"+s);
pg. 66
System.out.println("Average of all the elements:"+avg);
}//end of main
}//end of class
VDT
Variable Description Purpose
r int To store the row number
c int To store the column number
i int Counter variable
j int Counter variable
s int Sum of all the elements
avg double Average of all the elements
Output:
pg. 67
Q.25) Write a program to create an array of 10 integer
elements and print only prime numbers among all elements.
Program code:
import java.util.*;
class array1{
public static void main(String args[]){
int a[ ]=new int[10];
Scanner sc=new Scanner(System.in);
System.out.println("enter array elements : ");
for(int i=0;i<10;i++)
a[i]=sc.nextInt();
System.out.println("Prime numbers : ");
for(int i=0;i<10;i++) //outer loop
{
int c=0;
int p=a[i];
for(int j=1;j<=p;j++) // inner loop
{
if(p%j==0)
c++;
}// end of inner for
if(c==2)
pg. 68
System.out.println(a[i]);
} // end of for
}//end of main
}//end of class
VDT
Variable Description Purpose
i int To Calculate index number
c int Used as counter
p int To store the numbers of
array
j int Counter variable in the loop
Output:
pg. 69
CLASS IX PROGRAMS
Menu-driven programs:-
Q.26) Using the switch statement, write a menu driven program
for the following.
1. To print the Floyd’s triangle
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
2. To print the following pattern:
*#*#*
*#*#
*#*
*#
*
Program Code:
import java.util.*;
class pattern {
public static void main(String args[]){
pg. 70
Scanner sc=new Scanner(System.in);
System.out.println("enter user's choice");
int i, j, k=1,1, m;
int ch-sc.nextInt();
switch(ch) {
case 1:
Scanner sc1=new Scanner(System.in);
System.out.println("enter a number");
int n=sc.nextInt();
System.out.println("Floyd's triangle");
for(i=1;i<=n;i++){
for(j=1;j<=1;j++){
System.out.print(k+", ");
}
System.out.println(); k++;
}
break;
case 2:
for (l=5;l>=1;l−−){
for (k=1;k>=1;k--){
if((1+k)%2==0)
System.out.print("*");
else
System.out.print("#");
}
pg. 71
}
System.out.println();
break;
default:
System.out.println("invalid choice");
}//end of switch
}//end of main
}//end of class
pg. 72
Output:
pg. 73
Program Code:
import java.util.*;
class pattern
{
public static void main (String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("enter user's choice");
System.out.println("enter 1 for pattern 1 & 2 for pattern 2");
int ch=sc.nextInt();int i,j;
switch(ch)
{
case 1:
for(i=9;i>=1;i=i-2 )
{
for(j=1;j<=i;j=j+2){
System.out.print(j);
}
System.out.println();
}
break;
case 2
for(i=1;i<=5;i++)
{
for(j=i;j>=1;j--){
System.out.print(j);
}
System.out.println();
}
break;
default:
System.out.println("invalid choice");
}// end of switch
}// end of main
}// end of class
pg. 74
Variable Description Table
Variable Description Purpose
ch int Used as the switch
constant
i int As a counter variable
in outer loop in both
cases
j int As a counter variable
in inner loop in both
cases
Output:
pg. 75
Q.28) WAP a menu driven program :
1. To enter a number and check whether it is a perfect number
or not.
2. To enter a number and check whether it is a palindrome
number or not.
Display an appropriate error message for invalid choice
Program code:
import java.util.*;
public class menu {
public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
System.out.println("1.perfect number \n 2.palindrome
number");
System.out.println("Enter user's choice");
int ch=sc.nextInt();
switch(ch){
case 1:
System.out.println("Enter a number");
int n=sc.nextInt();
int i,c=0; for(i=1;i<=n/2;i++){
if(n%i==0)
c=c+i;}
if(c==n)
System.out.println(n+"is a perfect number");
pg. 76
else
System.out.println(n+"is not a perfect number");
break;
case 2:
System.out.println("Enter a number");
int m=sc.nextInt();
int r,cpy=m,v=0;;
while(m>0){
r=m%10;
v=(v*10)+r;
m=m/10;
}
if(v==cpy)
System.out.println(cpy+"is a palindrome
number");
else
System.out.println(cpy+"is not a palindrome
number");
break;
default:
System.out.println("invalid choice");
}// end of switch
}//end of main
}//end of main
VDT
pg. 77
Variable Description Purpose
n int to input a number
i int as a counter variable in the loop
c int as a counter
m int to input a number
r int to extract the digits of m
cpy int to copy the value of m
v int to calculate the reverse of m
Output:
pg. 78
Number generation programs :-
Q.29) WAP to accept a number and check whether it is a spy
number or not. A spy number is a number where the sum of its
digits equals the product of its digits.
For example, 1124 is a spy number, the sum of its digits is
1+1+2+4=8 and the product of its digits is 1*1*2*4=8.
Program code:
import java.util.*;
class special
{
public static void main (String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int n=sc.nextInt();
int s=0,p=1,ld,cpy=n;
if(n>9 && n<=99){
while(n>0)
{
ld=n%10;
s=s+ld;
p=p*ld;
n=n/10;
}// end of while
if(s==p)
System.out.println(cpy+"is a special number");
else
System.out.println(cpy+"is not a special number");
}
else {
System.out.println(cpy+"is not a two digit number");
}
pg. 79
}// end of main
}//end of class
Output:
pg. 80
Q.30) WAP to input a number and check whether it is a niven /
Harshad number. A number is set to be Niven if it is divisible by
the sum of its digits.
Sample input: 126
Sample output: It is an niven number as the sum of digits
=1+2+6=9 and 126 is divisible by 9.
Program Code:
import java.util.*;
class niven
{
public static void main (String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int n=sc.nextInt();
int cpy=n;
int sum=0,rem;
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}//end of while loop
if(cpy%sum==0)
System.out.println(cpy+"is a niven number");
else
System.out.println(cpy+"is not a niven number");
} //end of main
} //end of class
pg. 81
Variable Description Table
Variable Description Purpose
n Int To input a number
cpy Int To copy the number n
rem Int To calculate remainer
when n is divided by 10
sum Int To calculate sum of digits
Output:
pg. 82
Q.31) WAP to accept a number and check whether it is an
Armstrong number or not. An Armstrong number is number
where the sum of cubes of all the digits is equal to the original
number.
Program code:
import java.util.*;
class armstrong{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int r,cpy=n;double s=0;
while(n>0){
r=n%10;
n=n/10;
s=s+Math.pow(r,3);
}
if(cpy==s)
System.out.println(cpy+" is an armstrong number");
else
System.out.println(cpy+" is not an armstrong number");
}
}
pg. 83
VDT
Variable Description Purpose
n int To input a number
r int To extract the digits
of n
cpy int To copy the value
of n
s double To find the sum of
the cubes of the
digits of n
Output:
pg. 84
Q.32) Write a program to input a number and check and print
whether it is a Pronic number or not. [Pronic number is the
number which is the product of two consecutive integers.]
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7
Program code:
import java.util.*;
public class pronic{
public static void main (String[]args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the number to check: ");
int num = in.nextInt();
int ans = 0;
for(int i=0; i<num; i++){
if(i*(i+1) == num) {
ans = 1;
break;
}//end of if
}//end of for
if(ans == 1)
System.out.println(num+" is a Pronic Number.");
else
pg. 85
System.out.println(num+" is Not a Pronic Number.");
}//end of main
}//end of class
VDT
Output:
Slab-Programs:-
pg. 86
Q.33) A shopkeeper offers 10% discount on the printed price of
a digital camera. However, a customer has to pay 6% GST on
the remaining amount. Write a program in java to calculate the
amount to be paid by the customer taking the printed price as
an input.
Program Code:
import java.util.*;
class abc {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("enter weight of the parcel");
int w-sc.nextInt(); double r=0;
if (w<=10)
r=w*30;
else if(w>10 && w<=30)
r=10*30+(w-10)*20;
else if (w>30 && w<=60)
r=10*30+20*20+ (w-30)*15;
System.out.println("Total cost of of the parcel” + r);
}//end of main
}//end of class
pg. 87
Variable Description Purpose
p double To store the printed price of a
digital camera
d double To calculate the discount
da double To calculate discount amount
gst double To calculate GST
pay double To calculate total payment
Output:
pg. 88
Weight Rupees
Upto 10 kg Rs 30 per kg
Next 20 kg Rs 20 per kg
Above 30 kg Rs 10 per kg
WAP to calculate the charge of parcel taking weight of parcel
as an input.
Program Code:
import java.util.*;
class abc {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("enter weight of the parcel");
int w-sc.nextInt(); double r=0;
if (w<=10)
r=w*30;
else if(w>10 && w<=30)
r=10*30+(w-10)*20;
else if (w>30 && w<=60)
r=10*30+20*20+ (w-30)*15;
System.out.println("Total cost of of the parcel” + r);
}//end of main
}//end of class
pg. 89
Variable Description Purpose
w int To input the weight of the
parcel
r double To calculate the cost of the
parcel according to the
weight
Output:
pg. 90
Conclusion
pg. 91
Bibliography
pg. 92