0% found this document useful (0 votes)
7 views

JAVA CODES

The document provides a comprehensive overview of programming concepts, specifically focusing on control flow statements such as 'if', 'if-else', and 'if-else-if' constructs in Java. It includes syntax examples, explanations of normal and conditional flow of control, types of errors, and several programming exercises with solutions. Key topics covered also include compound statements, error distinctions, and practical applications of programming logic.

Uploaded by

bhuwaniadarsh
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)
7 views

JAVA CODES

The document provides a comprehensive overview of programming concepts, specifically focusing on control flow statements such as 'if', 'if-else', and 'if-else-if' constructs in Java. It includes syntax examples, explanations of normal and conditional flow of control, types of errors, and several programming exercises with solutions. Key topics covered also include compound statements, error distinctions, and practical applications of programming logic.

Uploaded by

bhuwaniadarsh
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/ 13

Question 1

Write the syntax of 'if' statement.

Answer

The syntax of 'if' statement is as follows:

if(condition )
Statement

OR

if(condition)
{
Statement 1
Statement 2
:
:
Statement n
}

Question 2

What is a compound statement? Give an example.

Answer

A set of two or more statements which are executed together and are placed within curly
brackets { } is called compound statement.

For example,

int n=5;
if(n>0)
{
System.out.println(n);
n=n-1;
}
In this snippet, the statements System.out.println(n); and n=n-1; are compound
statements as they will be executed only when the condition of if is true.

Question 3

Define the following:

(a) Normal flow of control

(b) Conditional flow of control


Answer

(a) Normal flow of control

It is a sequential movement of the control from one statement to another from beginning to
the end of the program. It is the simplest way of executing a program, where there is no
restriction on flow of control during the process.

(b) Conditional flow of control

Sometimes, it may happen that a statement or a set of statements is executed only when a
given condition results in true. However, if the condition results in false, then these
statements are ignored and the control moves forward to the next statement of the program.
This type of flow of control is known as the conditional flow of control and the statements
are known as the decision making statements.

It can be achieved by using the following statements:

1. if statement
2. if-else statement
3. if-else-if statement

Question 4

Write down the syntax of the following with reference to Java Programming:

(a) to accept an integer value using main( )

(b) to accept a fractional number using main( )

Answer

(a) public static void main(int variable_name)


(b) public static void main(float variable_name)

Question 5

What are the different types of errors that may occur during the execution of a program?

Answer

Logical errors and Run-Time errors occur during the execution of the program.

Question 6

Distinguish between Syntax error and Logical error.

Answer
Syntax Errors Logical Errors

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

Program compiles and executes but d


Program fails to compile and execute.
desired output.

Logic errors need to be found and cor


Syntax Errors are caught by the compiler.
people working on the program.

Question 7

Explain the following:

(a) if-else construct

(b) if-else-if construct

Answer

(a) if-else construct

In if-else construct, the user defines the statements to be executed when the condition results
in true or false. If the given condition results in true, the statements in the 'if' block are
executed and if the given condition results in false, the statements in the 'else' block are
executed.

The syntax of if-else construct is as follows:

if(condition)
Statement 1 //executed if condition is true
else
Statement 2 //executed if condition is false
(b) if-else-if construct

It is used when more than one condition are to be evaluated and only one of them is true.

The syntax of if-else-if construct is as follows:

if(condition 1)
Statement 1 //executed if condition 1 is true
else if(condition 2)
Statement 2 //executed if condition 2 is true
else
Statement 3 //executed if both conditions 1 and 2 are
false
When 'condition 1' is 'True' then it will execute statement 1. In case 'condition 1' is false, the
control will move to 'else if' part and will check 'condition 2'. If it is true, the control will
execute 'Statement 2' otherwise it will execute 'Statement 3'.

Solutions to unsolved programs

Question 1

Write a program to calculate and display the value of the given expression:
(a2+b2)/(a-b),when a=20, b=15

public class KboatEvalExp


{
public static void main(String args[])
{
int a = 20, b = 15;
double result;
result = (a * a + b * b) / (a - b);
System.out.println("Result = " + result);
}
}

Output

Question 2

Write a program to calculate the gross salary of an employee when


Basic Salary = Rs.8600
DA = 20% of Salary
HRA = 10% of Salary
CTA = 12% of the Salary.
Gross Salary = (Salary + DA + HRA + CTA)
Display the basic and gross salary.

public class KboatSalary


{
public static void main(String args[]) {
int sal = 8600;
double da = 20 / 100.0 * sal;
double hra = 10 / 100.0 * sal;
double cta = 12 / 100.0 * sal;
double gross = sal + da + hra + cta;
System.out.println("Gross Salary = " + gross);
}
}

Output

Question 3

Write a program to accept Principal, Rate and Time. Calculate and display the interest
accumulated for the first year, second year and the third year compound annually.

Sample Input:
Principal = ₹5,000, Rate = 10% per annum, Time = 3 years

Sample Output:
Interest for the first year: ₹500
Interest for the second year: ₹550
Interest for the third year: ₹605
public class KboatInterest
{
public static void main(double p, double r, int t) {
double i1 = p * r * 1 / 100.0;
double amt = p + i1;
System.out.println("Interest for the first year: " +
i1);

double i2 = amt * r * 1 / 100.0;


amt = amt + i2;
System.out.println("Interest for the second year: " +
i2);

double i3 = amt * r * 1 / 100.0;


amt = amt + i3;
System.out.println("Interest for the third year: " +
i3);
}
}

Output
Question 4

A shopkeeper announces two successive discounts 20% and 10% on purchasing of goods on
the marked price. Write a program to input marked price. Calculate and display the selling
price of the article.

public class KboatSuccessiveDiscount


{
public static void main(double mp) {
double d1 = mp * 20 / 100.0;
double amt1 = mp - d1;

double d2 = amt1 * 10 / 100.0;


double amt2 = amt1 - d2;

System.out.print("Selling Price = " + amt2);


}
}

Output
Question 5

Write a program to input two unequal numbers. Display the numbers after swapping their
values in the variables without using a third variable.
Sample Input: a=23, b= 56
Sample Output: a=56, b=23

public class KboatNumberSwap


{
public static void main(int a, int b) {
a = a + b;
b = a - b;
a = a - b;

System.out.println("Numbers after Swap:");


System.out.println("a = " + a + "\t" + "b = " + b);
}
}

Output

Question 6

Write a program to input the temperature in Celsius and then convert it into Fahrenheit. If the
temperature in Fahrenheit is more than 98.6°F then display "Fever", otherwise "Normal".

public class KboatTemperature


{
public static void main(double cel) {

double far = 1.8 * cel + 32;


if (far > 98.6)
System.out.println("Fever");
else
System.out.println("Normal");
}
}

Output

Question 7

Write a program to accept the marks, obtained in five different subjects (English, Physics,
Chemistry, Biology, Maths) by a student and find the average marks. If the average is 80 or
more, then display he/she is eligible to get "Computer Science', otherwise "Biology".

public class KboatMarks


{
public static void main(int eng, int phy, int chem,
int bio, int math)
{
double avg = (eng + phy + chem + bio + math) / 5.0;

if (avg >= 80) {


System.out.println("Eligible for Computer
Science");
}
else {
System.out.println("Eligible for Biology");
}
}
}

Output
Question 8

'Mega Market' has announced festival discounts on the purchase of items, based on the total
cost of the items purchased:

Total cost Discount

Up to ₹2,000 5%

₹2,001 to ₹5,000 10%

₹5,001 to ₹10,000 15%

Above ₹10,000 20%

Write a program to input the total cost. Display the discount and amount to be paid after
discount.

public class KboatDiscount


{
public static void main(double c) {
int d = 0;

if (c <= 2000)
d = 5;
else if (c <= 5000)
d = 10;
else if (c <= 10000)
d = 15;
else
d = 20;

double disc = c * d / 100.0;


double amt = c - disc;

System.out.println("Discount: " + disc);


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

You might also like