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

Ishana Computer Project XG

Uploaded by

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

Ishana Computer Project XG

Uploaded by

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

⁸Question.

1 (Program1- If else if program ) – Write a


program to check the marks entered and allot grades
accordingly .

Marks Grade

<50 fail

>=50&& <60 D

>=60&& <70 C

>=70&&<80 B

>=80 &&<90 A

>=90&&<100 A+
//Java Program to demonstrate the use of If else-if ladder.
//It is a program of grading system for fail, D grade, C
grade, B gr ade, A grade and A+.
public class IfElseIfExample {
public static void main(String[] args) {
int marks=65;
if(marks<50){
System.out.println("fail");
}
else if(marks>=50 && marks<60)
{ System.out.println("D grade");
}
else if(marks>=60 && marks<70){
System.out.println("C grade");
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
else if(marks>=80 && marks<90){
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("A+ grade");
}else{
System.out.println("Invalid!");
}
}
}

Output:

C grade
VD table
Name Type Purpose
marks int To store the marks

Question. 2- (Program 2- If -else-if bill program) Write a program to


calculate the electricity bill accordingly :-

Numbers of calls Charge


Up to 50 calls No charge (free)
For next 100
Rs. 2 per call
calls exceeding
50 call
Rs. 1.50 per call + Previous 100
For next 200
calls
exceeding 150 call calls bill amount
More than
Rs. 1 per call + Previous
350 calls
200 calls bill amount +
previous 100 calls bill
amount

import java.util.Scanner;
// program to calculate phone call bills
public class Q2
{
public static void main(String args[])
{
int call,mr=180;
float bill;
Scanner sc=new Scanner(System.in);
System.out.print("Enter total number of calls ");
call=sc.nextInt();// to ask the user for the no. Of
calls.

// calculation as per
rates if(call<=50)
{
bill=0;
}
else if(call>50 && call<=150)
{
bill=(call-50)*2;
} else if(call>150 && call<=350)
{
bill=(call-150)*1.5f+(100*2);
}
else
{
bill=(call-350)*1+(200*1.5f)+(100*2);
}
// final printing after calculation

System.out.println("Total Calls = " + call);


System.out.println("Total Calls Charges = " + bill);
System.out.println("Fixed Monthly Rental = " + mr);
System.out.print("Total Bill Amount = " +
(bill+mr));
}
}
Output:-

Enter total number of calls 243


Total Calls = 243
Total Calls Charges =
339.5 Fixed Monthly
Rental = 180 Total Bill
Amount = 519.5

VD table

Name Type Purpose

Call Int To input no. Of


calls
mr Int Fixed monthly rental

bill Float To calculate the


bill

Question 3.(Program-2) Write a program in java using a no. And calculate


the month accordingly.

public class Main D

public static void main(Stíing[]

aígs) D int month = 4;

switch (month) D

case 1:

System.out.píintln("Januaíy");
bíeak

; case

2:

System.out.píintln("Febíuaí

y"); bíeak;

case 3:

System.out.píintln("Maích")

; bíeak;

case 4:

System.out.píintln("Apíil");

bíeak;

case 5:

System.out.píintln("May");

bíeak;

case 6:

System.out.píintln("June");

bíeak;

default: System.out.píintln("In next half");


}

Ouīpuī:

April
VD table
Name Type Purpose
month Int To calculate the month
name according to the
no.

Question4.(Program 4). Write a program in java demonstrating fall through


in switch case.

1 public class Test {


2
3 public static void main(String[] args) {
4 // The java switch statement is fall-through.
// It means it executes all statement after first match if
break s
5 // not used with switch cases.
6 int num = 100;

7 switch (num) {
8
9 case 100:// Even though this case matches the switch
expression
10 System.out.println("Value of Case 1 is " + num);
case 200:// This case will be executed since the break
statement i
11
System.out.println("Value of Case 2 is " + num);
12 case 300:// This case will be executed since the break
statement i
13 System.out.println("Value of Case 3 is " + num);

14 default:// This case will be executed since the break


statement is
15 System.out.println("Value of default is " + num);
16
}
17
18
}
19
}
20
21
22

Output-

1
Value of Case 1 is 100
2 Value of Case 2 is 100
3 Value of Case 3 is 100

4 Value of default is 100


VD table
Name Type Purpose
num Int To use the case lable
according to the num
stored in it
Question .5 (Program 5) A Dudeney number is a positive integer
that is a perfect cube such that the sum of its digits is
equal to the cube root of the number. Write a program to
input a number and check and print whether it is a
Dudeney number or not.

Example:

Consider the number

512. Sum of digits = 5 +

1+2=8
Cube root of 512 = 8

As Sum of digits = Cube root of Number hence 512 is a


Dudeney number.

ANSWER
import java.util.Scanner;

public class KboatDudeneyNumber


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

//Check if n is a perfect cube


int cubeRoot =
(int)Math.round(Math.cbrt(n)); if
(cubeRoot * cubeRoot * cubeRoot == n) {
//If n is perfect cube then find
//sum of its digits
int s = 0;
int t = n;
while (t != 0) {
int d = t %
10;
s += d;
t /= 10;
}

if (s == cubeRoot) {
System.out.println(n + " is a Dudeney number");
}
else {
}
else {
System.out.println(n + " is not a Dudeney number");
}
}
}

OUTPUT
Enter the number: 512
512 is a dudeney number.
Name Type Purpose
n Int To input a no.
Cuberoot Int To calculate the
cuberoot of the no.
S Int To store the sum
T Int Temporary variable
D Int To store the remainder

Question 6.(Program 6). A tech number has even number of digits.


If the number is split in two equal halves, then the
square of sum of these halves is equal to the number
itself. Write a program to generate and print all four
digits tech numbers.

Example:

Consider the number 3025

Square of sum of the halves of 3025 = (30 + 25)2


= (55)2
= 3025 is a tech number.

ANSWER
public class TechNumbers
{
public static void main(String
args[]) { for (int i = 1000; i <=
9999; i++) {
int secondHalf = i % 100;
int firstHalf = i / 100;
int sum = firstHalf +
secondHalf; if (i == sum * sum)
System.out.println(i);
}
}
}

OUTPUT
2025
3025
9801

Name Type Purpose


I Int Çounter variable
SecondHalf Int To store the second
half of the number

firstHalf Int To store the first half of


the number
sum Int To store the sum
Question 7.Write a program to generate a fibonacci series.
class Main {
public static void main(String[] args) {

int n = 10, firstTerm = 0, secondTerm = 1;


System.out.println("Fibonacci Series till " + n + " terms:");

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


{ System.out.print(firstTerm + ", ");

// compute the next term


int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}}

Output . : Fibonacci Series till 10 terms:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Vd table
Name Type Purpose
n Int To store a no.
firstTerm int To store the first term

secondTerm Int To store the second


term
I Int Counter variable
nextTerm Int To store the sum

Question 8. Write a program to input a three digit number. Use a


method int Armstrong(int n) to accept the number. The
method returns 1, if the number is Armstrong, otherwise
zero(0).

Sample Output: 153 ⇒ 13 + 53 + 33


Sample Input: 153

= 153 It is an Armstrong Number.


import java.util.Scanner;

public class KboatArmstrongNumber


{
public int armstrong(int n) {

int num = n, cubeSum =

0; while (num > 0) {


int digit = num % 10;
cubeSum = cubeSum + (digit * digit *
digit); num /= 10;
}

if (cubeSum == n)
return 1;
else
return 0;
}

public static void main(String args[]) {

Scanner in = new
Scanner(System.in);
System.out.print("Enter Number:
"); int num = in.nextInt();

KboatArmstrongNumber obj = new


KboatArmstrongNumber(); int r =
obj.armstrong(num);

if (r == 1)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
Output

Enter a no.:153
153 is an Armstrong number.
Vd table
Name Type Purpode
num Int To store a no.
cubeSum Int To store the cube
digit Int To store the remainder
r Int To store the returned
value

Question9 Write a method fact(int n) to find the factorial of a


number n. Include a main class to find the value of S where:

S=n!m!(n−m)!S=m!(n−m)!n!
where, n! = 1 x 2 x 3 x....x n
import java.util.Scanner;

public class KboatFactorial


{
public long fact(int n) {

long f = 1;

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


{ f *= i;
}

return f;

public static void main(String args[]) {

KboatFactorial obj = new KboatFactorial();

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

double s = (double)(obj.fact(n)) / (obj.fact(m) *


obj.fact(n -
m));
System.out.println("S=" + s);
}
Output:
Enter m:4
Enter n:6
S=15.0
Vd table

Name Type Purpose


n Int To input a no.
f Long To store the factorial
i Int Counter variable.
m Int To store a number.
s Double To store the sum.

Questions 10. Write a program jn java to create a add function

public class Example {

public int sum(int a, int b)


{ int c = a + b;
return c;
}

public static void main(String args[])


{ Example obj = new Example();
int x = 2, y = 3;
int z = obj.sum(x, y);
System.out.println(z);
}
}
Output:
5
Vd table
Name Type Purpose
a int To pass the value in the
function
b Int To pass the value to
function.
c Int To return the sum
z Int To store the returned
value.

Question 11. Write a class with the name Perimeter using method
overloading that computes the perimeter of a square, a
rectangle and a circle.

Formula:

Perimeter of a square = 4 * s

Perimeter of a rectangle = 2 * (l +

b)
import java.util.Scanner;

public class Perimeter


{
public double perimeter(double s) {
double p = 4 * s;
return p;
}

public double perimeter(double l, double b) {


double p = 2 * (l + b);
return p;
}
public double perimeter(int c, double pi, double r) {
double p = c * pi * r;
return p;
}

public static void main(String


args[]) { Scanner in = new
Scanner(System.in); Perimeter obj
= new Perimeter();

System.out.print("Enter side of square: ");


double side = in.nextDouble();
System.out.println("Perimeter of square = "
+
obj.perimeter(side));

System.out.print("Enter length of rectangle:


"); double l = in.nextDouble();
System.out.print("Enter breadth of rectangle:
"); double b = in.nextDouble();
System.out.println("Perimeter of rectangle =
" +
obj.perimeter(l, b));

System.out.print("Enter radius of circle:


"); double r = in.nextDouble();
System.out.println("Perimeter of circle = " +
obj.perimeter(2, 3.14159, r));
Output:-

VD TABLE
Name Type Purpose
P Double To store the perimeter
Side Double To input the side of the
square
L Double To input the lengttg of
the rectangle
B Double To input the breadth of
the rectangle
r Double To input the radius of
the circle.

Question 12. Write a program in Java using a method Discount( ), to


calculate a single discount or a successive discount. Use
overload methods Discount(int), Discount(int,int) and
Discount(int,int,int) to calculate single discount and successive
discount respectively. Calculate and display the amount to be
paid by the customer after getting discounts on the printed
price of an article.
Sample Input:
Printed price: ₹12000
Successive discounts = 10%,
8%
= ₹(12000 - 1200)
import java.util.Scanner;

public class KboatSuccessiveDiscount


{
public void discount(int price) {
System.out.println("Amount after single discount =
" +
discount(price, 10));
System.out.println("Amount after successive discount = " +
discount(price, 10, 8));
}

public double discount(int price, int d) {


double priceAfterDisc = price - price * d / 100.0;
return priceAfterDisc;
}

public double discount(int price, int d1, int d2) {


double priceAfterDisc1 = price - price * d1 /
100.0;
double priceAfterDisc2 = priceAfterDisc1 - priceAfterDisc1
* d2
/ 100.0;
return priceAfterDisc2;
}

public static void main(String args[])

{ Scanner in = new

Scanner(System.in);

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

KboatSuccessiveDiscount obj = new KboatSuccessiveDiscount();

obj.discount(price);
}

Vd table:-
Name Type Purpose
Price Int To store the price
d Double To store the discount
rate

Question 13.
Create a class named Pizza that stores details about a pizza. It
should contain the following:

Instance Variables:
String pizzaSize — to store the size of the pizza (small, medium,
or large) int cheese — the number of cheese toppings
int pepperoni — the number of pepperoni
toppings int mushroom — the number of
mushroom toppings

Member Methods:
Constructor — to initialise all the instance variables
CalculateCost() — A public method that returns a double value,
that is, the cost of the pizza.
Pizza cost is calculated as follows:
 Small: Rs.500 + Rs.25 per topping

 Medium: Rs.650 + Rs.25 per topping


 Large: Rs.800 + Rs.25 per topping

PizzaDescription() — A public method that returns a String


containing the pizza size, quantity of each topping, and the pizza
cost as calculated by
public class Pizza
{
String pizzaSize;
int cheese;
int
pepperoni;
int mushroom;

public Pizza(String size,


int tc,
int tp,
int tm)
{
pizzaSize = size;
cheese = tc;
pepperoni = tp;
mushroom = tm;
}

public double CalculateCost()


double topCost = 25 * (cheese + pepperoni + mushroom);

if (pizzaSize == "small")
totalCost = 500 + topCost;
else if(pizzaSize == "medium")
totalCost = 650 + topCost;
else
totalCost = 800 + topCost;

return totalCost;
}

public String PizzaDescription() {


double cost = CalculateCost();
String desc = "Size: " +
pizzaSize
+ " Cheese: " + cheese
+ " Pepperoni: " + pepperoni
+ " Mushroom: " + mushroom
+ " Cost: Rs. " + cost;

return desc;
}

public static void main(String args[]) {


Pizza p = new Pizza("medium", 15, 5,
10); String desc =

System.out.println(desc);
}

Output
Vd table
Name Type Purpose
Pizza size String To store the pizza
size
Cheese Int To store a no.
Pepperoni Int To store a number
Mushroom Int To store a number

Question 14. WRITE a programs to demonstrate constructor overloading.


public class Rectangle
{
int length;
int width;

//Constructor without any


parameters public Rectangle()
{
System.out.println("Invoking constructor with no
parameters"); length = 20;
width = 15;
}

// Constructor with one


parameter public Rectangle(int
len) {
System.out.println("Invoking constructor with one
parameter"); length = len;
width = 20;
}

// Constructor with two parameters


public Rectangle(int len, int wd)

{
System.out.println("Invoking constructor with two
public void {
Area()
int area = length * width;
System.out.println("Length = " +
length); System.out.println("Width = "
+ width);
System.out.println("Area = " + area);
}

public static void main(String args[])


{ Rectangle rect1 = new
Rectangle(); rect1.Area();

Rectangle rect2 = new


Rectangle(30); rect2.Area();

Rectangle rect3 = new Rectangle(30, 10);


rect3.Area();
}
}
Vd table:-

Name Type Purpose


length Int To store the length
of
the rectangle
Breadth Int To store the breadth
of the triangle

Output:

Question 15.
Write a program to search for an integer value input by
the user in the list given below using linear search
technique. If found display "Search Successful" and print
the index of the element in the array, otherwise display
"Search Unsuccessful".

{75, 86, 90, 45, 31, 50, 36, 60, 12, 47}

import java.util.Scanner;

public class KboatLinearSearch


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

int arr[] = {75, 86, 90, 45, 31, 50, 36, 60, 12, 47};
int l =
arr.length; int i
= 0;

System.out.print("Enter the number to


search: "); int n = in.nextInt();

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


{ if (arr[i] == n)
{
break;
}
}

if (i == l) {
System.out.println("Search Unsuccessful");
}
else {
System.out.println("Search Successful");
System.out.println(n + " present at index " +
i);

Output:-
VD TABLE
Name Type Purpose

arr Int To store the array


L Int To count the length
of
the array
I Int Counter variable

Question 16.Write a program using an array and search for a no.


Using binary search technique
import java.util.Scanner;

public class KboatBinarySearchDemo


{
public static void main(String
args[]) { Scanner in = new
Scanner(System.in);
int arr[] = {1, 4, 5, 7, 8}; //Sorted Array

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


"); int n = in.nextInt();

boolean found = false;


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

//Binary Search
while (l <= h) {
int m = (l + h) / 2;
/*
* These println statements are
* added for explanation only
* so that you can easily track
* the values of start index,
* end index and middle index,
* at each step of the search
*/
System.out.println("\nStep " + step++);
System.out.println("l=" + l);
System.out.println("h=" + h);
System.out.println("m=" + m);
if (arr[m] <
n) l = m +
1;
else if (arr[m] >
n) h = m - 1;
else {
System.out.println(n + " found at index "
+ m + " in the array");
found = true;
break;
}
}

if (!found)
System.out.println(n + " not found in the array");
}
}
Output:-

Vd table
Name Type Purpose
arr Int To store the array
n Int To input a no.
Found Boolean To give the end
messag
l Int Beginning index of
the
array
h Int End index of the
array
m Int Mid index of the
array

Question 17.Write a program to input integer elements into an


array of size 20 and perform the following operations:

1. Display largest number from the array


2. Display smallest number from the array
3. Display sum of all the elements of the array

Answer
import java.util.Scanner;

public class KboatSDAMinMaxSum


{
public static void main(String
args[]) { Scanner in = new
Scanner(System.in); int arr[] =
new int[20];
System.out.println("Enter 20
numbers:"); for (int i = 0; i < 20;
i++) {
arr[i] = in.nextInt();
}
int min = arr[0], max = arr[0], sum
= 0; for (int i = 0; i < arr.length;
i++) {
if (arr[i] < min)
min = arr[i];

if (arr[i] > max)


max = arr[i];

sum += arr[i];
}

System.out.println("Largest Number = " + max);


System.out.println("Smallest Number = " + min);
System.out.println("Sum = " + sum);
OUTPUT-

VD table
Name Type Purpose
arr Int To store the
no.s
i Int Counter
variable
max Int To store the
maximum no.
min Int To store the
minimum no.
sum Int To store the
sum
of the no.s

Quwstion 18. Write a program to display a double dimensional array


in matrix format.
public class KboatDDA
{
public static void main(String args[])
{ int arr[][] = {{1, 3}, {5, 7}, {9,
11}};

System.out.println("2D Array Printed in Matrix Form:");


/*
* Outer loop is for the number of rows
*/
for (int i = 0; i < 3; i++) {
/*
* Inner loop is for the number of columns
*/
for (int j = 0; j < 2; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}

OUTPUT

VD table
Name Type Purpose
arr Int To store the array
i Int Counter variable
Question 19.Write a program to input a sentence. Find and
display the following:
(i)Number of words present in the sentence
(ii)Number of letters present in the sentence
Assume that the sentence has neither include any digit nor a
special character.
import java.util.Scanner;

public class KboatWordsNLetters


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

int wCount = 0, lCount =


0; int len =
str.length();
for (int i = 0; i < len; i++)
{ char ch = str.charAt(i);
if (ch == ' ')
wCount++;
else
lCount++;
}

/*
* Number of words in a sentence are one more than
* the number of spaces so incrementing wCount by 1
*/
wCount++;

System.out.println("No. of words = " + wCount);


System.out.println("No. of letters = " + lCount);
}
Output:-

VD TABLE-
Name Type Purpose
str String To input the
sentence
wCount Int To count the
number of words
Lcount Int To count the no.
Of letter

Question 20. Write a program in Java to accept a word/a


String and display the new string after removing all the
vowels present in it.
Sample Input: COMPUTER APPLICATIONS
Sample Output: CMPTR PPLCTNS
import java.util.Scanner;

public class KboatVowelRemoval


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

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

char ch =

Character.toUpperCase(str.charAt(i)); if

(ch == 'A' ||
ch == 'E' ||
ch == 'I'
|| ch ==
'O' || ch
== 'U') {
continue;
}

newStr = newStr + ch;


}

System.out.println("String with vowels removed");


System.out.println(newStr);
Ouput:-

VD TABLE
Name Type Purpose
str String To input the sentence
len Int To store the length
of
the string
ch Char To store the
character
chopped.

You might also like