ICSE9th

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

COMPUTER APPLICATIONS (STD-9th )

Chapter 1 - Unit 1
Principles of Object Oriented Programming
Write short notes:
Question 1: Object Oriented Programming
Answer: Object Oriented Programming is an approach in which stress is laid on data rather than
functions. The data values remain associated with the functions of a particular block of the program so as
to encourage data security.
Question 2: Data Abstraction
Answer : Data Abstraction is the act of representing the essential features without knowing the
background details
Question 3: Encapsulation
Answer : Wrapping of data and functions that operate on that data into a single unit is called
Encapsulation.
Question 4: Polymorphism
Answer : In object-oriented programming, Polymorphism provides the means to perform a single
action in multiple different ways. Example of Polymorphism is given here.
Question 5: Procedure Oriented Programming
Answer : Procedure Oriented Programming basically consists of a list of instructions for the
computer to follow and these are organized into groups known as functions. In Procedure Oriented
Programming, most of the functions share global data and this data moves more openly around the
system from one function to the other.
Question 6: Inheritance
Answer : Inheritance enables new classes to receive or inherit the properties and methods of
existing classes.

Distinguish between
Question 1: Object Oriented Programming and Procedure Oriented Programming
Object Oriented Programming Procedure Oriented Programming

The stress is put on data rather than functions. The stress is put on function rather than data.

The data is restricted, to be used in a specific program area. It allows data to flow freely throughout the program.

It follows bottom-up programming approach. It follows top-down programming approach.

Question 2: High Level language and Low Level language


High Level language Low Level language

High Level language is machine independent. Low Level language is machine dependent.

High Level language is human friendly so it is easy to Low Level language is machine friendly so it is difficult
understand for programmers. to understand for programmers.

High Level language needs a compiler or interpreter for Low Level language might need a assembler for
translation to machine code. translation to machine code.

Programs written in High Level language are easier to Programs written in Low Level language are hard to
modify and debug. modify and debug.
Answer the following questions
Question 1: Enlist the features of Object Oriented Programming.
Answer : Some of the features of Object Oriented Programming are:
It gives stress on data items rather than functions.
It makes the complete program/problem simpler by dividing it into number of objects.
The objects can be used as a bridge to have data flow from one function to another.
The concept of data hiding enhances security in programs.
It is highly beneficial to solve complex programs.

Chapter 1 - Unit 2
Introduction to Java
Question 1: Mention at least four features of Java.
Answer : Four features of Java are:
 It is an Object Oriented Programming Language.
 It is platform independent. It provides us Write Once, Run Anywhere (WORA)
feature.
 It uses a compiler as well as an interpreter.
 It is case sensitive.

Question 2: Define the following:


(a) A compiler
A compiler is a program that translates a source program written in some high-level
programming language into a target program in another low-level programming
language without changing the meaning of the program. The compiler processes the
complete source program at once and if there are compilation errors, they are all
reported together at once.
(b) An interpreter
An interpreter is a program that reads a source program line by line, converts each line
into its equivalent machine code and executes it. As it reads the program line by line so
the errors are reported one by one.
(c) Byte code
Java compiler converts Java source code into an intermediate binary code called
Bytecode. Bytecode can't be executed directly on the processor. It needs to be
converted into Machine Code first.

Question 2: What is Java Virtual Machine (JVM)?


Java Virtual Machine (JVM) is a software that takes Bytecode as input, converts it into
Machine code of the specific platform it is running on and executes it. JVM is platform
specific, each platform has its own JVM.
Question 3: Name three packages of Java Class Library.
Three packages of Java Class Library are:
java.lang
java.io
java.util
Question 4: What are Java reserved words(Keywords)? Name any five.
In Java, a reserved word is a word that has a predefined meaning in the language. Due
to this, reserved words can’t be used as names for variables, methods, classes or any
other identifier. Reserved words are also known as keywords. Five commonly used
Java reserved words are:
Public,class,int,double,char

Question 5: Distinguish between:


(a) Source code and Object code
Source code Object code

It is a set of statements in a High-Level programming It is a set of statements in Machine


language. Language.

It is understood by human/programmer. It is understood by the processor.


(b) Compiler and Interpreter
Compiler Interpreter

It translates the whole source program into target It translates the source program into target program
program at once. one line at a time.

All the errors found during compilation are Errors are displayed line by line as each line is
displayed together at once. translated and executed.

Question:You want to display your bio-data on the output screen. Write a program in Java to perform
the task in the given format:
Name:
Father's Name:
Date of birth:
Blood Group:
Aadhar Card No.:
State:
Answer
class BioData {
public static void main(String args[]) {
System.out.println("Name: Rahul Khanna");
System.out.println("Father's Name: Arvind Khanna");
System.out.println("Date of birth: 12/12/2005");
System.out.println("Blood Group: O+");
System.out.println("Aadhar Card No.: 4321 8756 9978" );
System.out.println("State: Maharashtra");
}
}
Question 2
Mention five states (characteristics) and two methods for the following Classes:
(a) Class Employee
Answer
Characteristics Methods

Name computeSalary()

Employee Number computeTax()


Characteristics Methods

Pan Number

Salary

Income Tax
(b) Class Bank
Answer

Characteristics Methods

Bank Name openAccount()

Bank Address depositMoney()

IFSC Code

MICR Code

Accounts
(c) Class School
Answer
Characteristics Methods

School Name admitStudent()

School Address conductExams()

Classes

Students

Teachers
(d) Class Book
Answer
Characteristics Methods

Book Name buyBook()

ISBN Number readBook()

Price

Author

Publisher
Chapter 2
Elementary Concept of Object and Classes
Question 1:How will you define a software object?
Answer: A software object replaces the characteristics and behaviours of a real world object
with data members and member methods, respectively.
Question 2: Class and Objects are inter-related. Explain.
Answer: A Class is used to create various Objects that have different characteristics and
common behaviours. Each object follows all the features defined within a class. That is why
class is also referred to as a blue print or prototype of an object. This way we can say that they
are inter-related.
Question 3: Why is an Object called an 'Instance' of a class? Explain.
Answer: A class can create objects of itself with different characteristics and common
behaviour. So, we can say that an Object represents a specific state of the class. For these
reasons, an Object is called an Instance of a Class.
Question 4: Why is a class known as composite data type?
Answer: A class can contain data members of various primitive and reference data types.
Hence, class is known as composite data type.

Chapter 3
Values and Data Types
Write short answers
Question 1: What do you mean by data type?
Data types are used to identify the type of data a memory location can hold and the associated operations of
handling it.
Question 2: Define variable with an example.
A variable represents a memory location through a symbolic name which holds a known or unknown value of a
particular data type. This name of the variable is used in the program to refer to the stored value.
Example:
int mathScore = 95;
Question 3: What do you mean by constant? Explain with an example.
The keyword final before a variable declaration makes it a constant. Its value can't be changed in the program.
Example:
final int DAYS_IN_A_WEEK = 7;

Question 4: State two kinds of data types.


Two kinds of data types are:
Primitive Datatypes.
Non-Primitive Datatypes.
Question 5:What do you understand by Token? Name different types of tokens.
A token is the smallest element of a program that is meaningful to the compiler. The different types of tokens in
Java are:
Identifiers
Literals
Operators
Separators
Keywords
Question 6:What are the rules to assign a variable in a Java programming?
Name of the variable should be a sequence of alphabets, digits, underscore and dollar sign characters only.
It should not start with a digit.
It should not be a keyword or a boolean or null literal.
Question 7: Explain the term 'type casting'?
The process of converting one predefined type into another is called type casting.
Question 8
Perform the following:
(a) Assign the value of pie (3.142) to a variable with the requisite data type.
double pi = 3.142;
(b) Assign the value of (1.732) to a variable with the requisite data type.
double x = 1.732;
Question 9
Distinguish between:
(a) Integer and floating constant
Integer Constant Floating Constant

Integer Constants represent whole number values like Floating Constants represent fractional numbers like
2, -16, 18246, 24041973, etc. 3.14159, -14.08, 42.0, 675.238, etc.

Integer Constants are assigned to variables of data type Floating Constants are assigned to variables of data type
— byte, short, int, long, char — float, double
(b) Token and Identifier
Token Identifier

A token is the smallest element of a program that is Identifiers are used to name things like classes,
meaningful to the compiler. objects, variables, arrays, functions an so on.

Tokens in Java are categorised into 5 types — Keywords,


Identifier is a type of token in Java.
Identifiers, Literals, Punctuators, Operators.
(c) Character and String constant
Character Constant String Constant

Character Constants are written by enclosing a String Constants are written by enclosing a set of
character within a pair of single quotes. characters within a pair of double quotes.

Character Constants are assigned to variables of type


String Constants are assigned to variables of type String.
char.
(d) Character and Boolean literal
Character Literal Boolean Literal

A boolean literal can take only one of the two


Character literals are written by enclosing a character
boolean values represented by the words true or
within a pair of single quotes.
false.

Character literals can be assigned to variables of any Boolean literals can only be assigned to variables
numeric data type — byte, short, int, long, float, double, declared as boolean
Character Literal Boolean Literal

char

Only true and false values are allowed for boolean


Escape Sequences can be used to write character literals
literals
Question 10: Write down the data type of the following:
(a) Integer
int
(b) Long Integer
long
(c) A fractional number
double
(d) A special character
char
Question 11: What do you understand by Boolean type data? Explain with an example.
A boolean data type is used to store one of the two boolean values — true or false. The size of boolean data
type is 8 bits or 1 byte.
Example:
boolean bTest = false;
Question 12: What do you understand by primitive data type? Give two examples.
Primitive data types are the basic or fundamental data types used to declare a variable. Examples of primitive
data types in Java are byte, short, int, long, float, double, char, boolean.
Question 13: Why is it necessary to define data type in Java programming?
Data types tells Java how much memory it should reserve for storing the value. Data types also help in
preventing errors as the compiler can check and flag illegal operations at compile time itself.
Question 14: Define the following with an example each:
(a) Implicit type conversion
In implicit type conversion, the result of a mixed mode expression is obtained in the higher most data type of
the variables without any intervention by the user. Example:
int a = 10;
float b = 25.5f, c;
c = a + b;
(b) Explicit type conversion
In explicit type conversion, the data gets converted to a type as specified by the programmer. For example:
int a = 10;
double b = 25.5;
float c = (float)(a + b);
Question 15: Predict the return data type of the following:
(i)int p; double q;
r = p+q;
System.out.println(r);
Answer
Return data type is double.
(ii)float m;
p = m/3*(Math.pow(4,3));
System.out.println(p);
Answer
Return data type is double.
Question 20: What are the resultant data types if the following implicit conversions are performed?
Show the result with flow lines.
int i; float f; double d; char c; byte b;
(a) i + c/b;
Answer
i + c/b;
⇒ int + char / byte
⇒ int + char
⇒ int
(b) f/d + c*f;
Answer
f/d + c*f;
⇒ float / double + char * float
⇒ double + float
⇒ double
Chapter 4
Operators in Java
Answer the following questions
Question 1: What is an operator?
An operator is a symbol or sign used to specify an operation to be performed in Java
programming.
Question 2: Name the different types of operators.
The different types of operators are Arithmetical, Logical and Relational.
Question 3: Differentiate between the following:
(a) Arithmetical operator and Logical operator
Arithmetical Operator Logical Operator

Logical operators operate on boolean expressions to combine


Arithmetic operators are used to
the results of these boolean expression into a single boolean
perform mathematical operations.
value.

+, -, *, /, etc. are a few examples of


&&, ||, ! are a few examples of Logical Operators
Arithmetic operators.
(b) Binary operator and Ternary operator
Binary operator Ternary operator

Binary operators work on two operands. Ternary operator work on three operands.

+, -, *, /, etc. are a few examples of Binary The conditional operator ? : is a Ternary


operators. operator.
(c) Logical AND (&&) and Logical OR(||)
Logical AND (&&) Logical OR(||)

It evaluates to true only if both of its operands It evaluates to true if one or both of its operands are
are true. true.

Example: Example:
int a = 8, b = 13, c = 0; int a = 8, b = 13, c = 0;
Logical AND (&&) Logical OR(||)

if (a > 10 && b > 10) if (a > 10 || b > 10)


c = 10; c = 10;
else else
c = 5; c = 5;
Here, value of c will be 5 as one of the Here, value of c will be 10 as at least one of the
operands is false. operands is true.
(d) Prefix operator and Postfix operator
Prefix Operator Postfix Operator

It works on the principle of CHANGE-THEN-


It works on the principle of USE-THEN-CHANGE.
USE.

It is written before the operand. It is written after the operand.

Example: Example:
int a = 99; int a = 99;
int b = ++a; int b = a++;
After the execution of these two statements, After the execution of these two statements, a will have
both a and b will have the value of 100. the value of 100 and b will have the value of 99.

Question 6:Differentiate between an operator and an expression.


An operator is a symbol or sign used to specify an operation to be performed whereas an expression is a set of
variables, constants and operators i.e. an expression is a combination of operators and operands.
Question 7:
If m=5 and n=2 then what will be the output of m and n after execution that will store in (a) & (b)?
qa
Question 8
State the difference between = and ==.
= ==

It is the assignment operator used for assigning a It is the equality operator used to check if a variable is equal to
value to a variable. another variable or literal.

E.g. int a = 10; assigns 10 to variable a. E.g. if (a == 10) checks if variable a is equal to 10 or not.

Question 9 a = --b + c++ + b


1.What will be the output for the following ⇒ a = 9 + 40 + 9
program segment? ⇒ a = 58
int a=0,b=10,c=40; 2.What will be the output of the following?
a = --b + c++ +b; if x =5
System.out.println(" a = " + a); (a) 5* ++x;
Output Answer
a = 58 5 * ++x
Explanation ⇒5*6
⇒ 30
(b) 5* x++; 5 * x++
Answer ⇒5*5
⇒ 25

Question 11
Evaluate the following expressions, if the values of the variables are:
a = 2, b = 3, and c = 9
(a) a - (b++) * (--c); (b) a * (++b) % c;
Answer Answer
a - (b++) * (--c) a * (++b) % c
⇒2-3*8 ⇒ a * (++b) % c
⇒2-3*8 ⇒ 2 * (4) % 9
⇒ 2 - 24 ⇒8%9
⇒ -22 ⇒8
Question 12:
If a = 5, b = 9, calculate the value of:
a += a++ - ++b + a;
Answer
a += a++ - ++b + a
⇒ a = a + (a++ - ++b + a)
⇒ a = 5 + (5 - 10 + 6)
⇒a=5+1
⇒a=6
Question 13:
Give the output of the program snippet.
int a = 10, b =12;
if(a>=10)
a++;
else
++b;
System.out.println(" a = " + a + " and b = " +b);
Output
a = 11 and b = 12
Explanation
The condition if(a>=10) is true so a++ increments a to 11. b remains the same.
Question 14
Rewrite the following using ternary operator.
if(income<=100000)
tax = 0;
else
tax = (0.1*income);
Answer
tax = income <= 100000 ? 0 : (0.1*income);
Question 15: Rewrite the following using ternary operator.
if(p>5000)
d = p*5/100;
else
d = 2*p/100;
Answer
d = p > 5000 ? p * 5 / 100 : 2 * p / 100;

Chapter 5
Input In Java
Name the following
Question 1: A package needed to import scanner class
java.util
Question 2: A method that accepts a character through scanner object
charAt()
Question 3: A method to accept an exponential value through scanner object
nextDouble()
Question 5: A method that accepts an integer token through scanner object
nextInt()

Differentiate between the following


Question 1
nextInt( ) and nextFloat( ) methods
nextInt( ) nextFloat( )

Scans the next token of input as an int Scans the next token of input as a float
Question 2
Syntax and logical errors
Syntax Errors Logical Errors

Syntax Errors occur when we violate the rules of writing the Logical Errors occur due to our mistakes in
statements of the programming language. programming logic.

Program compiles and executes but doesn't give the


Program fails to compile and execute.
desired output.

Logic errors need to be found and corrected by the


Syntax Errors are caught by the compiler.
people working on the program.
Answer the following
Question 1:What do you mean by scanner class?
Scanner class is used to get user input. It is present in java.util package.
Question 2: What are the different ways to give inputs in a Java Program?
Java provides the following ways to give input in a program:
Using Function Argument.
Using Scanner class.
Question 3: What is the use of the keyword import?
import keyword is used to import built-in and user-defined packages into our Java program.

Question 4: What is a compound statement? Give an example.


Two or more statements can be grouped together by enclosing them between opening and
closing curly braces. Such a group of statements is called a compound statement.
if (a < b) { /*
* All statements within this set of braces
* form the compound statement
*/
System.out.println("a is less than b");
a = 10;
b = 20;
System.out.println("The value of a is " + a);
System.out.println("The value of b is " + b);
}

Question 5:
Write down the syntax to input a character through scanner class with an example.
Syntax:
char <variable name> = <Scanner Object>.next().charAt(0);
Example:
Scanner in = new Scanner(System.in);
char ch = in.next().charAt(0);

Solutions to Unsolved Java Programs


Question 6: The time period of a Simple Pendulum is given by the formula:
T = 2π√(l/g)
Write a program to calculate the time period of a Simple Pendulum by taking length and acceleration
due to gravity (g) as inputs.
import java.util.Scanner;

public class SimplePendulum


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter length: ");
double l = sc.nextDouble();
System.out.print("Enter g: ");
double g = sc.nextDouble();
double t = 2 * (22.0 / 7.0) * Math.sqrt(l/g);
System.out.println("T = " + t);
}
}

Question 2
Write a program by using class 'Employee' to accept Basic Pay of an employee. Calculate the
allowances/deductions as given below.
Allowance / Deduction Rate

Dearness Allowance (DA) 30% of Basic Pay

House Rent Allowance (HRA) 15% of Basic Pay

Provident Fund (PF) 12.5% of Basic Pay


Finally, find and print the Gross and Net pay.
Gross Pay = Basic Pay + Dearness Allowance + House Rent Allowance
Net Pay = Gross Pay - Provident Fund
import java.util.Scanner;

public class Employee


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Basic Pay: ");
double bp = sc.nextDouble();
double da = 0.3 * bp;
double hra = 0.15 * bp;
double pf = 0.125 * bp;
double gp = bp + da + hra;
double np = gp - pf;
System.out.println("Gross Pay = " + gp);
System.out.println("Net Pay = " + np);
}
}

Question 3
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 printed price as an input.
import java.util.Scanner;

public class Camera


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter printed price of Digital Camera:" );
double mrp = sc.nextDouble();
double disc = mrp * 10 / 100.0;
double price = mrp - disc;
double gst = price * 6 / 100.0;
price += gst;
System.out.println("Amount to be paid: " + price);
}
}
Question 4
A shopkeeper offers 30% discount on purchasing articles whereas the other shopkeeper
offers two successive discounts 20% and 10% for purchasing the same articles. Write a
program in Java to compute and display the discounts.
Take the price of an article as the input.
import java.util.Scanner;

public class Discounts


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter price of article: " );
double price = sc.nextDouble();
double d1 = price * 30 / 100.0;
double amt1 = price - d1;
System.out.println("30% discount = " + d1);
System.out.println("Amount after 30% discount = " + amt1);

double d2 = price * 20 / 100.0;


double amt2 = price - d2;
double d3 = amt2 * 10 / 100.0;
amt2 -= d3;
System.out.println("20% discount = " + d2);
System.out.println("10% discount = " + d3);
System.out.println("Amount after successive discounts = " + amt2);
}
}

Question 5
Mr. Agarwal invests certain sum at 5% per annum compound interest for three years. Write a
program in Java to calculate:
(a) the interest for the first year
(b) the interest for the second year
(c) the amount after three years.
Take sum as an input from the user.
Sample Input: Principal = ₹5000, Rate =10%, Time = 3 yrs
Sample Output: Interest for the first year: ₹500
Interest for the second year: ₹550
Interest for the third year: ₹605
import java.util.Scanner;

public class CompoundInterest


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter sum of money: " );
double p = sc.nextDouble();
double interest = p * 5 * 1 / 100.0;
System.out.println("Interest for the first year = " + interest);
p += interest;
interest = p * 5 * 1 / 100.0;
System.out.println("Interest for the second year = " + interest);
p += interest;
interest = p * 5 * 1 / 100.0;
System.out.println("Interest for the third year = " + interest);
}
}

Question 6
A businessman wishes to accumulate 3000 shares of a company. However, he already has
some shares of that company valuing ₹10 (nominal value) which yield 10% dividend per
annum and receive ₹2000 as dividend at the end of the year. Write a program in Java to
calculate the number of shares he has and how many more shares to be purchased to make
his target.
Hint: No. of share = (Annual dividend * 100) / (Nominal value * div%)
public class Shares
{
public static void main(String args[]) {
int sharesHeld = (2000 * 100)/(10 * 10);
System.out.println("No. of shares held currently = "
+ sharesHeld);
int sharesRequired = 3000 - sharesHeld;
System.out.println("No. of shares to purchase = "
+ sharesRequired);
}
}

Question 7
Write a program to input the time in seconds. Display the time after converting them into
hours, minutes and seconds.
Sample Input: Time in seconds 5420
Sample Output: 1 Hour 30 Minutes 20 Seconds
import java.util.Scanner;

public class Time


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter time in seconds: " );
long secs = sc.nextLong();
long hrs = secs / 3600;
secs =sec% 3600;
long mins = secs / 60;
secs %= 60;
System.out.println(hrs + " Hours " + mins
+ " Minutes " + secs + " Seconds");
}
}

Question 10
A shopkeeper sells two calculators for the same price. He earns 20% profit on one and suffers
a loss of 20% on the other. Write a program to find his total cost price of the calculators by
taking selling price as input.
Hint: CP = (SP / (1 + (profit / 100))) (when profit)
CP = (SP / (1 - (loss / 100))) (when loss)
import java.util.Scanner;

public class Shop


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the selling price: " );
double sp = sc.nextDouble();
double cp1 = (sp / (1 + (20 / 100.0)));
double cp2 = (sp / (1 - (20 / 100.0)));
double totalCP = cp1 + cp2;
System.out.println("Total Cost Price = " + totalCP);
}
}

Predict the Output of the Given Snippet, when Executed


Question 1:int a=1,b=1,m=10,n=5;
if((a==1)&&(b==0))
{
System.out.println((m+n));
System.out.println((m—n));
}
if((a==1)&&(b==1))
{
System.out.println((m*n));
System. out.println((m%n));
}
Output
50
0
Explanation
First if condition is false, second if condition is true. Statements inside the code block of second if condition are
executed.
m*n => 10 * 5 => 50
m%n => 10 % 5 => 0

Question 2:int x=1,y=1; Output


if(n>0) (i) n = 1
{ x=2
x=x+1; y=2
y=y+1; (ii) n = 0
} x=1
What will be the value of x and y, if n assumes a y=1
value (i) 1 (ii) 0?
Explanation
When n = 1, if condition is true, its code block is executed adding 1 to both x and y. When n = 0, if condition is
false so x and y retain their original values.

Solutions to Unsolved Java Programs


Question 1:Write a program to input three angles of a triangle and check whether a triangle is possible or
not. If possible then check whether it is an acute-angled triangle, right-angled or an obtuse-angled triangle
otherwise, display 'Triangle not possible'.
Sample Input: Enter three angles: 40, 50, 90
Sample Output: Right=angled Triangle
import java.util.Scanner;

public class Triangle


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first angle: ");
int a1 = sc.nextInt();
System.out.print("Enter second angle: " );
int a2 = sc.nextInt();
System.out.print("Enter third angle: ");
int a3 = sc.nextInt();
int angleSum = a1 + a2 + a3;

if (angleSum == 180 && a1 > 0 && a2 > 0 && a3 > 0) {


if (a1 < 90 && a2 < 90 && a3 < 90) {
System.out.println("Acute-angled Triangle");
}
else if (a1 == 90 || a2 == 90 || a3 == 90) {
System.out.println("Right-angled Triangle" );
}
else {
System.out.println("Obtuse-angled Triangle" );
}
}
else {
System.out.println("Triangle not possible" );
}
}
}

Question 4
Write a program to accept a number and check whether the number is divisible by 3 as well as 5. Otherwise,
decide:
(a) Is the number divisible by 3 and not by 5?
(b) Is the number divisible by 5 and not by 3?
(c) Is the number neither divisible by 3 nor by 5?
The program displays the message accordingly.
import java.util.Scanner;

public class Divisor


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

if (num % 3 == 0 && num % 5 == 0)


System.out.println("Divisible by 3 and 5");
else if (num % 3 == 0)
System.out.println("Divisible by 3 but not by 5");
else if (num % 5 == 0)
System.out.println("Divisible by 5 but not by 3");
else
System.out.println("Neither divisible by 3 nor by 5");
}
}

Question 10
A cloth showroom has announced festival discounts and the gifts on the purchase of items, based on the total cost as given below:
Total Cost Discount Gift

Up to ₹ 2,000 5% Calculator

₹ 2,001 to ₹ 5,000 10% School Bag

₹ 5,001 to ₹ 10,000 15% Wall Clock

Above ₹ 10,000 20% Wrist Watch


Write a program to input the total cost. Compute and display the amount to be paid by the customer along with the gift.
import java.util.Scanner;

public class ClothDiscount


{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter total cost: ");
double cost = sc.nextDouble();
String gift;
double amt;

if (cost <= 2000.0) {


amt = cost - (cost * 5 / 100);
gift = "Calculator";
}
else if (cost <= 5000.0) {
amt = cost - (cost * 10 / 100);
gift = "School Bag";
}
else if (cost <= 10000.0) {
amt = cost - (cost * 15 / 100);
gift = "Wall Clock";
}
else {
amt = cost - (cost * 20 / 100);
gift = "Wrist Watch";
}

System.out.println("Amount to be paid: " + amt);


System.out.println("Gift: " + gift);

}
}

You might also like