0% found this document useful (0 votes)
14 views30 pages

COMPUTER PROJECT

The document contains a collection of Java programming tasks and their solutions, covering various concepts such as conditional statements, series calculations, relative velocity, co-prime checking, and data structures. Each task includes a brief description, sample input/output, and the corresponding Java code. The tasks are organized in a structured format with an index for easy navigation.

Uploaded by

tapaskumarmahato
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)
14 views30 pages

COMPUTER PROJECT

The document contains a collection of Java programming tasks and their solutions, covering various concepts such as conditional statements, series calculations, relative velocity, co-prime checking, and data structures. Each task includes a brief description, sample input/output, and the corresponding Java code. The tasks are organized in a structured format with an index for easy navigation.

Uploaded by

tapaskumarmahato
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/ 30

VIER’S SCH

XA OO
.

ST

L
COMPUTER PROJECT
INDEX
SL. NO PROGRAM NAME PAGE NO
1. Conditional statement table base program 1
2. Write the programs in java to find the sum of the following series: 2
S = 1 + 1 + 2 + 3 + 5 + ....... To n terms
3. The relative velocity of two trains travelling in opposite directions is 3
calculated by adding their velocities. In case, the trains are travelling in
the same direction, the relative velocity is the difference between their
velocities. Write a program to input the velocities and length of the
trains. Write a menu driven program to calculate the relative velocities
and the time taken to cross each other.
4. Write a program to enter two numbers and check whether they are 5
co-prime or not.
[Two numbers are said to be co-prime, if their HCF is 1 (one).]
Sample Input: 14, 15
Sample Output: They are co-prime.
5. Design a class named ShowRoom with the following description: 7
Instance variables/data members:
String name: to store the name of the customer.
long mobno: to store the mobile number of the customer.
double cost: to store the cost of the items purchased.
double dis: to store the discount amount.
double amount: to store the amount to be paid after discount.
Member methods:
ShowRoom(): default constructor to initialize data members.
void input(): to input customer name, mobile number, cost.
void calculate(): to calculate discount ,based on the following criteria:
COST DISCOUNT (IN PERCENTAGE)
Less than or equal to Rs. 10000 5%
More than Rs. 10000 and less than or equal to Rs. 20000 10%
More than Rs. 20000 and less than or equal to Rs. 35000 15%
More than Rs. 35000 20%
void display(): to display customer name, mobile number, amount to
be paid after discount.Write a main() method to create an object of
the class and call the above member methods
6. Write programs to find the sum of the following series: 9
(a) S = 1 + (3/2!) + (5/3!) + (7/4!) + ....... to n
7. Write the programs to display the following patterns: 11
(a)
1
31
531
7531
97531
8. Write a program in Java to accept an integer number N such that 13
0<N<27. Display the corresponding letter of the alphabet (i.e. the
letter at position N).
[Hint: If N =1 then display A]
9. Write a program to accept the year of graduation from school as an 15
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".
Sample Input:
n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
1982 1987 1993 1996 1999 2003 2006 2007 2009 2010
10. Write a program to input 10 integer elements in an array and sort 17
them in descending order using bubble sort technique.
11. Write a program to perform binary search on a list of integers given 19
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
12. Declare a single dimensional array of size 28 to store daily 21
temperatures for the month of February. Using this structure, write a
program to find:
1. The hottest day of the month
2. The coldest day of the month
3. The average temperature of the month
13. A double dimensional array is defined as N[4][4] to store numbers. 24
Write a program to find the sum of all even numbers and product of all
odd numbers of the elements stored in Double Dimensional Array
(DDA).
Sample Input:
12 10 15 17
30 11 32 71
17 14 29 31
41 33 40 51
14. Write a Program in Java to input elements in a 2-D square matrix and 26
check whether it is a Scalar Matrix or not.
Scalar Matrix: A scalar matrix is a diagonal matrix where the left
diagonal elements are same.
The Matrix is:
5000
0500
0050
0005
The Matrix is Scalar
15. Write a program to input and sort the weight of ten people. Sort and 28
display them in descending order using the selection sort technique.

16, Write a program to input a string and print each word of the string in 30
the reverse order.
Sample Input:
Enter a string: My name is Raman
Sample Output
yM eman si namaR
17. Write a program in Java to accept a string and display the number of 32
uppercase, number of lowercase, number of special characters and
number of digits present in the string.
18. Write a program in Java to enter a string/sentence and display the 34
longest word and the length of the longest word present in the string.
Sample Input:
"Tata football academy will play against Mohan Bagan"
Sample Output:
The longest word: Football
The length of the word: 8
19. Write a program to input a number and check and print whether it is a 36
'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
returns zero (0).
(Hint: Pronic number is the number which is the product of two
consecutive integers)
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7
20 Write a class with the name Area using method overloading that 37
computes the area of a parallelogram, a rhombus and a trapezium.
Formula:
Area of a parallelogram (pg) = base * ht
Area of a rhombus (rh) = (1/2) * d1 * d2
(where, d1 and d2 are the diagonals)
Area of a trapezium (tr) = (1/2) * ( a + b) * h
(where a and b are the parallel sides, h is the perpendicular distance
between the parallel sides)
Program 1: -
An air-conditioned bus charges fare from the passengers based on the distance travelled
as per the tariff given below:

Distance Travelled Fare

Up to 10 km Fixed charge ₹80

11 km to 20 km ₹6/km

21 km to 30 km ₹5/km

31 km and above ₹4/km

Design a program to input distance travelled by the passenger. Calculate and display the
fare to be paid.
import java.util.Scanner;

public class BusFare


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter distance travelled: ");


int dist = in.nextInt();

int fare = 0;

if (dist <= 0)
fare = 0;
else if (dist <= 10)
fare = 80;
else if (dist <= 20)
fare = 80 + (dist - 10) * 6;
else if (dist <= 30)
fare = 80 + 60 + (dist - 20) * 5;
else if (dist > 30)
fare = 80 + 60 + 50 + (dist - 30) * 4;

System.out.println("Fare = " + fare);


}
}
Program 2: -
Write the programs in Java to find the sum of the following series:

(a) S = 1 + 1 + 2 + 3 + 5 + ....... to n terms


import java.util.Scanner;

public class Series


{
public void computeSeriesSum() {

Scanner in = new Scanner(System.in);


System.out.print("Enter n: ");
int n = in.nextInt();

int a = 1, b = 1;
int sum = a + b;

for (int i = 3; i <= n; i++) {


int term = a + b;
sum += term;
a = b;
b = term;
}

System.out.println("Sum=" + sum);
}
}
Program 3: -
The relative velocity of two trains travelling in opposite directions is calculated by adding
their velocities. In case, the trains are travelling in the same direction, the relative
velocity is the difference between their velocities. Write a program to input the velocities
and length of the trains. Write a menu driven program to calculate the relative velocities
and the time taken to cross each other.
import java.util.Scanner;

public class Train


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.println("1. Trains travelling in same direction");


System.out.println("2. Trains travelling in opposite direction");
System.out.print("Enter your choice: ");
int choice = in.nextInt();

System.out.print("Enter first train velocity: ");


double speed1 = in.nextDouble();
System.out.print("Enter first train length: ");
double len1 = in.nextDouble();

System.out.print("Enter second train velocity: ");


double speed2 = in.nextDouble();
System.out.print("Enter second train length: ");
double len2 = in.nextDouble();

double rSpeed = 0.0;

switch(choice) {
case 1:
rSpeed = Math.abs(speed1 - speed2);
break;

case 2:
rSpeed = speed1 + speed2;
break;

default:
System.out.println("Wrong choice! Please select from 1 or 2.");
}

double time = (len1 + len2) / rSpeed;

System.out.println("Relative Velocity = " + rSpeed);


System.out.println("Time taken to cross = " + time);
}

Output

Program 4: -
Write a program to enter two numbers and check whether they are co-prime or not.
[Two numbers are said to be co-prime, if their HCF is 1 (one).]
Sample Input: 14, 15
Sample Output: They are co-prime.
import java.util.Scanner;

public class Coprime


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter b: ");
int b = in.nextInt();

int hcf = 1;

for (int i = 1; i <= a && i <= b; i++) {


if (a % i == 0 && b % i == 0)
hcf = i;
}

if (hcf == 1)
System.out.println(a + " and " + b + " are co-prime");
else
System.out.println(a + " and " + b + " are not co-prime");
}
}

Program 5: -
Design a class named ShowRoom with the following description:
Instance variables/data members:
String name: to store the name of the customer.
long mobno: to store the mobile number of the customer.
double cost: to store the cost of the items purchased.
double dis: to store the discount amount.
double amount: to store the amount to be paid after discount.
Member methods:
ShowRoom(): default constructor to initialize data members.
void input(): to input customer name, mobile number, cost.
void calculate(): to calculate discount on the cost of purchased items, based on the
following criteria:
COST DISCOUNT (IN PERCENTAGE)
Less than or equal to Rs. 10000 5%
More than Rs. 10000 and less than or equal to Rs. 20000 10%
More than Rs. 20000 and less than or equal to Rs. 35000 15%
More than Rs. 35000 20%
void display(): to display customer name, mobile number, amount to be paid after
discount.
Write a main() method to create an object of the class and call the above member
methods..

import java.util.Scanner;
class ShowRoom{
//as mentioned in the question
String name;
long mobno;
double cost;
double dis;
double amount;
public ShowRoom(){
name = "";
mobno = 0l;
cost = 0.0;
dis = 0.0;
amount = 0.0;
}
public void input(){
Scanner sc = new Scanner(System.in);
System.out.print("Please enter Customer name: ");
name = sc.nextLine();
System.out.print("Please enter Mobile Number: ");
mobno = sc.nextLong();
System.out.print("Please enter Cost: ");
cost = sc.nextDouble();
}
public void calculate(){
if(cost <= 10000)
dis = 5.0/100.0 *cost;
else if(cost <= 20000)
dis = 10.0/100.0*cost;
else if(cost <= 35000)
dis = 15.0/100.0 *cost;
else
dis = 20.0 / 100.0 * cost;
amount = cost - dis;
}
public void display(){
System.out.println("Customer name is: " + name);
System.out.println("Mobile Number is: " + mobno);
System.out.println("Total Amount is: " + amount);
}
public static void main(String args[]){
ShowRoom obj = new ShowRoom();
obj.input();
obj.calculate();
obj.display();
}
}

Output

Program 6: -
Write programs to find the sum of the following series:

(a) S = 1 + (3/2!) + (5/3!) + (7/4!) + ....... to n


import java.util.Scanner;

public class Series


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
double sum = 0.0;
for (int i = 1, j = 1; i <= n; i++, j = j + 2) {
double f = 1;
for (int k = 1; k <= i; k++) {
f *= k;
}
sum += j / f;
}
System.out.println("Sum=" + sum);
}
}
Program 7: -
Write the programs to display the following patterns:

(a)

31

531

7531

97531
public class Pattern
{
public static void main(String args[]) {
for (int i = 1; i < 10; i = i + 2) {
for (int j = i; j > 0; j = j - 2) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Output

Program 8: -
Write a program in Java to accept an integer number N such that 0<N<27. Display the
corresponding letter of the alphabet (i.e. the letter at position N).

[Hint: If N =1 then display A]


import java.util.Scanner;

public class Integer2Letter


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter integer: ");
int n = in.nextInt();

if (n > 0 && n < 27) {


char ch = (char)(n + 64);
System.out.println("Corresponding letter = " + ch);
}
else {
System.out.println("Please enter a number in 1 to 26 range");
}
}
}
Output

Program 9: -
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".

Sample Input:

n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]

1982 1987 1993 1996 1999 2003 2006 2007 2009 2010
import java.util.Scanner;

public class GraduationYear


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};

System.out.print("Enter graduation year to search: ");


int year = in.nextInt();

int l = 0, h = n.length - 1, idx = -1;


while (l <= h) {
int m = (l + h) / 2;
if (n[m] == year) {
idx = m;
break;
}
else if (n[m] < year) {
l = m + 1;
}
else {
h = m - 1;
}
}

if (idx == -1)
System.out.println("Record does not exist");
else
System.out.println("Record exists");
}
}

Output

Program 10: -
Write a program to input 10 integer elements in an array and sort them in descending
order using bubble sort technique.
import java.util.Scanner;

public class BubbleSortDsc


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = 10;
int arr[] = new int[n];

System.out.println("Enter the elements of the array:");


for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}

//Bubble Sort
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int 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] + " ");
}
}

Output
Program 11: -
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

import java.util.Scanner;

public class BinarySearch


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


int arr[] = {5, 7, 9, 11, 15, 20, 30, 45, 89, 97};

System.out.print("Enter number to search: ");


int n = in.nextInt();

int l = 0, h = arr.length - 1, index = -1;


while (l <= h) {
int m = (l + h) / 2;
if (arr[m] < n)
l = m + 1;
else if (arr[m] > n)
h = m - 1;
else {
index = m;
break;
}

if (index == -1) {
System.out.println("Search element not found");
}
else {
System.out.println(n + " found at position " + index);
}
}
}
Program 12: -
Declare a single dimensional array of size 28 to store daily temperatures for the month of
February. Using this structure, write a program to find:

1. The hottest day of the month


2. The coldest day of the month
3. The average temperature of the month
import java.util.Scanner;

public class FebTemp


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double febTemp[] = new double[28];
int n = febTemp.length;

System.out.println("Enter Feb daily temperatures:");


for (int i = 0; i < n; i++) {
febTemp[i] = in.nextDouble();
}

double sum = 0.0;


int low = 0, high = 0;
for (int i = 0; i < n; i++) {
if (febTemp[i] < febTemp[low])
low = i;

if (febTemp[i] > febTemp[high])


high = i;

sum += febTemp[i];
}

double avg = sum / n;

System.out.println("Hottest day = " + (high + 1));


System.out.println("Coldest day = " + (low + 1));
System.out.println("Average Temperature = " + avg);
}

Output
Program 13: -
A double dimensional array is defined as N[4][4] to store numbers. Write a program to
find the sum of all even numbers and product of all odd numbers of the elements stored
in Double Dimensional Array (DDA).
Sample Input:

12 10 15 17

30 11 32 71

17 14 29 31

41 33 40 51
import java.util.Scanner;

public class KboatDDAEvenOdd


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N[][] = new int[4][4];
long evenSum = 0, oddProd = 1;
System.out.println("Enter the elements of 4x4 DDA: ");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
N[i][j] = in.nextInt();
if (N[i][j] % 2 == 0)
evenSum += N[i][j];
else
oddProd *= N[i][j];
}
}

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


System.out.println("Product of all odd numbers = " + oddProd);

}
Output

Program 14: -
Write a Program in Java to input elements in a 2-D square matrix and check whether it is
a Scalar Matrix or not.
Scalar Matrix: A scalar matrix is a diagonal matrix where the left diagonal elements are
same.
The Matrix is:
5000
0500
0050
0005

The Matrix is Scalar


import java.util.Scanner;

public class ScalarMatrix


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the size of the matrix: ");
int n = in.nextInt();

int arr[][] = new int[n][n];

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


for (int i = 0; i < n; i++) {
System.out.println("Enter Row "+ (i+1) + " :");
for (int j = 0; j < n; j++) {
arr[i][j] = in.nextInt();
}
}
System.out.println("The Matrix is:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}

boolean isScalar = true;


int x = arr[0][0];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i == j && arr[i][j] != x)
|| ( i != j && arr[i][j] != 0)) {
isScalar = false;
break;
}
}

if (!isScalar) {
break;
}
}

if (isScalar) {
System.out.println("The Matrix is Scalar");
}
else {
System.out.println("The Matrix is not Scalar");
}
}
}

Output
Program 15: -
Write a program to input and sort the weight of ten people. Sort and display them in
descending order using the selection sort technique.
import java.util.Scanner;

public class SelectionSort


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double weightArr[] = new double[10];
System.out.println("Enter weights of 10 people: ");
for (int i = 0; i < 10; i++) {
weightArr[i] = in.nextDouble();
}

for (int i = 0; i < 9; i++) {


int idx = i;
for (int j = i + 1; j < 10; j++) {
if (weightArr[j] > weightArr[idx])
idx = j;
}

double t = weightArr[i];
weightArr[i] = weightArr[idx];
weightArr[idx] = t;
}

System.out.println("Sorted Weights Array:");


for (int i = 0; i < 10; i++) {
System.out.print(weightArr[i] + " ");
}
}

Output

Program 16: -
Write a program to input a string and print each word of the string in the reverse order.
Sample Input:
Enter a string: My name is Raman
Sample Output
yM eman si namaR
import java.util.Scanner;

public class WordsReverse


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();

str += " ";


int len = str.length();

String word = "";


int wLen = 0;
char ch;

for (int i = 0; i < len; i++) {


ch = str.charAt(i);
if (ch != ' ') {
word = word + ch;
}
else {
wLen = word.length();
for(int j = wLen - 1; j >= 0; j--) {
System.out.print(word.charAt(j));
}
System.out.print(' ');
word = "";
}
}
}
}

Output

Program 17: -
Write a program in Java to accept a string and display the number of uppercase, number
of lowercase, number of special characters and number of digits present in the string.
import java.util.Scanner;

public class Count


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();

int len = str.length();

int uc = 0;
int lc = 0;
int sc = 0;
int dc = 0;
char ch;
for (int i = 0; i < len; i++) {
ch = str.charAt(i);
if (Character.isUpperCase(ch))
uc++;
else if (Character.isLowerCase(ch))
lc++;
else if (Character.isDigit(ch))
dc++;
else if (!Character.isWhitespace(ch))
sc++;
}

System.out.println("UpperCase Count = " + uc);


System.out.println("LowerCase Count = " + lc);
System.out.println("Digit count = " + dc);
System.out.println("Special Character Count = " + sc);

}
}

Program 18: -
Write a program in Java to enter a string/sentence and display the longest word and the
length of the longest word present in the string.

Sample Input:
"Tata football academy will play against Mohan Bagan"

Sample Output:
The longest word: Football
The length of the word: 8
import java.util.Scanner;

public class LongestWord


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a word or sentence:");
String str = in.nextLine();
str += " "; //Add space at end of string
String word = "", lWord = "";
int len = str.length();

for (int i = 0; i < len; i++) {


char ch = str.charAt(i);
if (ch == ' ') {

if (word.length() > lWord.length())


lWord = word;

word = "";
}
else {
word += ch;
}
}

System.out.println("The longest word: " + lWord +


": The length of the word: " + lWord.length());
}

Output

Program 19: -
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 returns zero (0).
(Hint: Pronic number is the number which is the product of two consecutive integers)
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7
import java.util.Scanner;

public class PronicNumber


{
public int pronic(int n) {

int isPronic = 0;

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


if (i * (i + 1) == n) {
isPronic = 1;
break;
}
}

return isPronic;
}

public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter the number to check: ");
int num = in.nextInt();

KboatPronicNumber obj = new KboatPronicNumber();


int r = obj.pronic(num);

if (r == 1)
System.out.println(num + " is a pronic number");
else
System.out.println(num + " is not a pronic number");

}
}

Output
Program 20 : -
Write a class with the name Area using method overloading that computes the area of a
parallelogram, a rhombus and a trapezium.
Formula:
Area of a parallelogram (pg) = base * ht
Area of a rhombus (rh) = (1/2) * d1 * d2
(where, d1 and d2 are the diagonals)
Area of a trapezium (tr) = (1/2) * ( a + b) * h
(where a and b are the parallel sides, h is the perpendicular distance between the
parallel sides)
import java.util.Scanner;

public class Area


{
public double area(double base, double height) {
double a = base * height;
return a;
}

public double area(double c, double d1, double d2) {


double a = c * d1 * d2;
return a;
}

public double area(double c, double a, double b, double h) {


double x = c * (a + b) * h;
return x;
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
Area obj = new Area();

System.out.print("Enter base of parallelogram: ");


double base = in.nextDouble();
System.out.print("Enter height of parallelogram: ");
double ht = in.nextDouble();
System.out.println("Area of parallelogram = " + obj.area(base, ht));

System.out.print("Enter first diagonal of rhombus: ");


double d1 = in.nextDouble();
System.out.print("Enter second diagonal of rhombus: ");
double d2 = in.nextDouble();
System.out.println("Area of rhombus = " + obj.area(0.5, d1, d2));

System.out.print("Enter first parallel side of trapezium: ");


double a = in.nextDouble();
System.out.print("Enter second parallel side of trapezium: ");
double b = in.nextDouble();
System.out.print("Enter height of trapezium: ");
double h = in.nextDouble();
System.out.println("Area of trapezium = " + obj.area(0.5, a, b, h));
}
}
Output

You might also like