Computer X Class Board Project
Computer X Class Board Project
1. Write a program to input a number. Calculate its square root and cube root. Finally,
display the result by rounding it off.
import java.util.Scanner;
Write a main method to create an object of the class and call the above methods.
Data Members Purpose
int vno To store the vehicle number
int hou rs To store the num ber of hours the vehicle is parked
in the parking lot
double bill To store the bill amount
import java.util.Scanner;
vno = in.nextInt();
hours = in.nextInt();
if (hours <= 1)
bill = 3;
else
obj.input();
obj.calculate();
obj.display();
3. An ICSE school displays a notice on the school notice board regarding admission in Class XI for
choosing stream according to marks obtained in English, Maths and Science in Class 10 Council
Examination.
if (eng >= 80 && sci >= 80 && maths >= 80) //condition check
System.out.println("Pure Science");
else if (eng >= 80 && sci >= 80 && maths >= 60)
System.out.println("Bio. Science");
else if (eng >= 60 && sci >= 60 && maths >= 60)
System.out.println("Commerce");
else
System.out.println("Cannot allot any stream");
} //main close
}
}
Variable Descriptive Table:
Variable Name Data Type Description
eng int To store English marks
maths int To store maths marks
sci int To store science marks
4. Write a program to input a number. Check and display whether it is a Niven number or not. (A
number is said to be Niven which is divisible by the sum of its digits).
int digitSum = 0;
while (num != 0) {
int digit = num % 10; num
= num/ 10;
digitSum =digitSum + digit; //program logic
}
/*
* digitSum != 0 check prevents
* division by zero error for the
* case when users gives the number
* 0 as input
*/
if (digitSum != 0 && orgNum % digitSum == 0)
System.out.println(orgNum + " is a Niven number");//Output display
else
System.out.println(orgNum + " is not a Niven number");
}
} //main close
Output
Variable Name Data Type Description num int To insert the number
orgNum int To store num value as original
value
digitSum int To store sum of digits
digit int To store the modulated value of
num
5. Using the switch statement, write a menu driven program for the following:
(a) To print the Floyd's triangle:
12 34 5 6
7 8 9 10
11 12 13 14 15
case 2:
String s = “ICSE”;
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(s.charAt(j) + “ “);
}
System.out.println();
}
break;
default:
System .out .println( “Incorrect Choice ”);
} //closing of switch
} //closing of main
}
Output
6. Write a program to input a set of 20 letters. Convert each letter into upper case. Find and display the
number of vowels and number of consonants present in the set of given letters.
import java.util.Scanner;
ch == 'U')
vc++;
else if (ch >= 'A' && ch <= 'Z')
cc++; }
System.out.println("Number of Vowels = " + vc);
System.out.println("Number of Consonants = " + cc);
}
} //cosing of class
Output
7. Write a program to accept the year of graduation from school as an integer value from the user.
Using the binary search technique on the sorted array of integers given below, output the message
"Record exists" if the value input is located in the array. If not, output the message "Record does not
exist".
Variable Name Data Type Description
n[] int Single dimensional array
year int To store year
l int To store first index position
h int To store last index position
int idx;
for (idx = 0; idx < SIZE; idx++) {
if (city.compareToIgnoreCase(cities[idx]) == 0) { //citites comparision
break;
}
}
1. double volume(double r) — with radius (r) as an argument, returns the volume of sphere using
the formula:
V = (4/3) * (22/7) * r * r * r
2. double volume(double h, double r) — with height(h) and radius(r) as the arguments, returns the
volume of a cylinder using the formula: V = (22/7) * r * r * h
3. double volume(double 1, double b, double h) — with length(l), breadth(b) and height(h) as the
arguments, returns the volume of a cuboid using the formula: V = l*b*h (length * breadth *
height)
public class Volume
{
double volume(double r) {
return (4 / 3.0) * (22 / 7.0) * r * r * r;
} //volume calculation
double volume (double h, double r) {
return (22 / 7.0) * r * r * h;
}
Outpu t
Price Discount
Less than or equal to Rs 1000 2% of price
More than Rs1000 and less than or equal to 10% of price
Rs3000
More than Rs3000 15% of price
Write a main method to create an object of the class and call the above member methods.
import java.util.Scanner;
public void input() { // To input and store the name and price of the book
Scanner in = new Scanner(System.in);
System.out.print("Enter name of the book: ");
bname = in.nextLine();
System.out.print("Enter price of the book: ");
price = in.nextDouble();
}
price -= disc;
}
1 2 3 4 5 6 7
2 3 4 5 6 7
3 4 5 6 7
4 5 6 7
5 6 7
6 7
7
6 7
5 6 7
4 5 6 7
3 4 5 6 7
2 3 4 5 6 71
2 3 4 5 6 7
import java.util.Scanner;
System.out.println();
}
//Printing lower half of the pattern
System.out.println();
}
}//closing main
}
Output:
Same as pattern
Variable Name Data Type Description rows int To enter no of rows i int To print no of
rows
j int To print no of columns
12. Write a program in Java to accept a name(Containing three words) and Display only the initials
(i.e., first letter of each word).
Sample Input: Nalanda Public School
Sample Output: N P S
import java.util.Scanner;
Variable Name Data Type Description str String To enter name i int Condition check
13. Write a program to accept a list of 20 integers. Sort the first 10 numbers in ascending order and
next the 10 numbers in descending order by using 'Bubble Sort' technique. Finally, print the complete
list of integers.
import java.util.Scanner;
//Sort first half in ascending order for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = 0; j < arr.length / 2 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}
//Sort second half in descending order for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = arr.length / 2; j < arr.length - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}
Output:
Write a menu driven program to generate the upper case letters from Z to A and lower case letters from
'a' to 'z' as per the user's choice.
Enter '1' to display upper case letters from Z to A and Enter '2' to display lower case letters from a to z.
import java.util.Scanner;
switch (ch) {
case 1:
for (int i = 90; i > 64; i--) { //program logic
char c = (char)i;
System.out.print(c);
System.out.print(" ");
count++;
} }
break;
case 2:
for (int i = 97; i < 123; i++) {
char c = (char)i;
System.out.print(c);
System.out.print(" ");
count++;
} }
break;
default:
System.out.println("Incorrect Choice");
}
}
} //close main
Output:
S = a + a 2 + a3 + ....... + a n
import java.util.Scanner;
Output: