Ishana Computer Project XG
Ishana Computer Project XG
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
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
VD table
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;
Ouīpuī:
April
VD table
Name Type Purpose
month Int To calculate the month
name according to the
no.
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);
Output-
1
Value of Case 1 is 100
2 Value of Case 2 is 100
3 Value of Case 3 is 100
Example:
1+2=8
Cube root of 512 = 8
ANSWER
import java.util.Scanner;
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
Example:
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
Vd table
Name Type Purpose
n Int To store a no.
firstTerm int To store the first term
if (cubeSum == n)
return 1;
else
return 0;
}
Scanner in = new
Scanner(System.in);
System.out.print("Enter Number:
"); int num = in.nextInt();
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
S=n!m!(n−m)!S=m!(n−m)!n!
where, n! = 1 x 2 x 3 x....x n
import java.util.Scanner;
long f = 1;
return f;
Scanner in = new
Scanner(System.in);
System.out.print("Enter m: ");
int m = in.nextInt();
System.out.print("Enter n:
"); int n = in.nextInt();
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;
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.
{ Scanner in = new
Scanner(System.in);
System.out.print("Enter price:
"); int price = in.nextInt();
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
if (pizzaSize == "small")
totalCost = 500 + topCost;
else if(pizzaSize == "medium")
totalCost = 650 + topCost;
else
totalCost = 800 + topCost;
return totalCost;
}
return 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
{
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);
}
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;
int arr[] = {75, 86, 90, 45, 31, 50, 36, 60, 12, 47};
int l =
arr.length; int i
= 0;
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
//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
Answer
import java.util.Scanner;
sum += arr[i];
}
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
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;
/*
* Number of words in a sentence are one more than
* the number of spaces so incrementing wCount by 1
*/
wCount++;
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
char ch =
Character.toUpperCase(str.charAt(i)); if
(ch == 'A' ||
ch == 'E' ||
ch == 'I'
|| ch ==
'O' || ch
== 'U') {
continue;
}
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.