0% found this document useful (0 votes)
7 views13 pages

Class 9 Programming Q&A Solutions

The document contains Java programming exercises for Class 9, covering various topics such as calculating discounts, determining even or odd numbers, validating triangle angles, and computing areas and volumes of geometric shapes. Each exercise includes a code snippet that prompts user input and performs specific calculations based on the input. The exercises also demonstrate the use of conditional statements and loops in Java.

Uploaded by

hp1894818
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)
7 views13 pages

Class 9 Programming Q&A Solutions

The document contains Java programming exercises for Class 9, covering various topics such as calculating discounts, determining even or odd numbers, validating triangle angles, and computing areas and volumes of geometric shapes. Each exercise includes a code snippet that prompts user input and performs specific calculations based on the input. The exercises also demonstrate the use of conditional statements and loops in Java.

Uploaded by

hp1894818
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

CLASS 9, CHAPTER - 8

PROGRAMMING (Question & Answer)


import [Link];
class Q1_A{
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the price of pen : ");
int price = [Link]();
int dis =0;
if(price < 500)
{
[Link]("Pen price is : "+ price);
[Link]("Got a Gift");
}
else
{
dis = price * 25 / 100;
[Link]("Pen price is : "+ price);
[Link]("Discount : "+ dis);
[Link]("Payble amount : "+ (price-dis));
}
}
}

import [Link];
class Q1_B{
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Please enter any number : ");
int num = [Link]();
if(num <= 0)
{
[Link]("Please enter any posative number : ");
}
else
{
if(num%2 == 0)
{
[Link]("Number is Even");
}
else
{
[Link]("Number is Odd");
}
}
}
}

import [Link];
class Q1_C{
public static void main()
{
Scanner sc = new Scanner([Link]);
int a, b, c, sum;
[Link]("Enter three angles of a triangle: ");
a = [Link]();
b = [Link]();
c = [Link]();
sum = a+b+c;

if (sum == 180)
[Link]("Triangle is valid");
else
[Link]("Triangle is not valid");
}
}

import [Link];
public class Q1_D{
public static void main() {
Scanner in = new Scanner([Link]);
int dayNum;
[Link]("Enter a day number (1-7): ");
dayNum = [Link]();
switch(dayNum) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
case 4:
[Link]("Thursday");
break;
case 5:
[Link]("Friday");
break;
case 6:
[Link]("Saturday");
break;
case 7:
[Link]("Sunday");
break;
default:
[Link]("Invalid day number.");
break;
}
}
}

import [Link];
class Q1_E{
public static void main() {
Scanner sc = new Scanner([Link]);
[Link]("Enter two integer number : ");
int a = [Link]();
int b = [Link]();

if(a==b)
{
[Link]("Number are twice ");
}
else if(a > b)
{
int square = a*a;
[Link]("Square = "+square);
}
else
{
int cube = b*b*b;
[Link]("Cube "+cube);
}
}
}

import [Link];
class Q1_F{
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the marks of Physics : ");
int phy = [Link]();
[Link]("Enter the marks of Chemistry :");
int chem = [Link]();
[Link]("Enter the marks of Math : ");
int math = [Link]();

int totat_marks = phy + chem + math;


float avg = totat_marks / 3;
if(avg >= 90)
{
[Link]("A");
}
else if(avg >= 80)
{
[Link]("B");
}
else if(avg >= 70)
{
[Link]("C");
}
else if(avg >= 60)
{
[Link]("D");
}
else
{
[Link]("F");
}

}
}

import [Link];
class Q1_G{
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the customer's name : ");
String c_name = [Link]();
[Link]("Enter the purchase name : ");
int purAmt = [Link]();
float disAmt=0.0f, totalAmt =0.0f;
if(purAmt<=3000)
{
disAmt = purAmt * 5 / 100;
}
else if(purAmt<5000)
{
disAmt = purAmt * 7 / 100;
}
else
{
disAmt = purAmt * 10 / 100;
}

totalAmt = purAmt - disAmt;

[Link]("Customer Name: "+ c_name);


[Link]("Purchase Amount : "+ purAmt);
[Link]("Discount Amount : "+ disAmt);
[Link]("Payble Amount : "+ totalAmt);
}
}

import [Link];
class Q1_G{
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the customer's name : ");
String c_name = [Link]();
[Link]("Enter the purchase name : ");
int purAmt = [Link]();
float disAmt=0.0f, totalAmt =0.0f;
if(purAmt<=3000)
{
disAmt = purAmt * 5 / 100;
}
else if(purAmt<5000)
{
disAmt = purAmt * 7 / 100;
}
else
{
disAmt = purAmt * 10 / 100;
}

totalAmt = purAmt - disAmt;

[Link]("Customer Name: "+ c_name);


[Link]("Purchase Amount : "+ purAmt);
[Link]("Discount Amount : "+ disAmt);
[Link]("Payble Amount : "+ totalAmt);
}
}

import [Link];
class Q1_H{
public static void main()
{
Scanner sc = new Scanner([Link]);
double disAmt = 0.0;
[Link]("Enter the bill amount : ");
int billAmt = [Link]();

[Link]("1. Credit Card. \n2. Debit Card. \n3. E-Wallet. \n4. Cash. ");
[Link]("Mode of payment : ");
int mode = [Link]();

if(mode == 1)
{
disAmt = (billAmt * 1.5) / 100;
[Link]("Mode of payment: Credit Card ");
[Link]("Payble Amount: "+(billAmt-disAmt));
}
else if(mode == 2)
{
disAmt = 10;
[Link]("Mode of payment: Debit Card ");
[Link]("Cash Back: "+disAmt);
[Link]("Payble Amount: "+(billAmt-disAmt));
}
else if(mode == 2)
{
disAmt = 20;
[Link]("Mode of payment: E-Wallet ");
[Link]("Cash Back: "+disAmt);
[Link]("Payble Amount: "+(billAmt-disAmt));
}
else if(mode == 4)
{
disAmt = 0;
[Link]("Mode of payment: Cash ");
[Link]("Discount : "+disAmt);
[Link]("Payble Amount: "+(billAmt-disAmt));
}
else
{
[Link]("Invalid Choice! ");
}

}
}

import [Link];
class Q1_I{
public static void main()
{
Scanner sc = new Scanner([Link]);
double billAmt = 0.0;
[Link]("Enter the number of calles : ");
int calles = [Link]();
if(calles<=50)
{
billAmt = 100;
}
else if(calles <= 100)
{
billAmt = 100 + (calles - 50) * 0.88;
}
else if(calles <= 200)
{
billAmt = 100 + (50*0.80) + (calles - 100) * 0.60;
}
else
{
billAmt = 100 + (50*0.80) + (100*0.60) + (calles - 200) * 40;
}

[Link]("Number of Calles : "+ calles);


[Link]("Total Bill Amount to be paid : "+billAmt);

}
}

import [Link];
class Q1_H{
public static void main() {
Scanner sc = new Scanner([Link]);
int product;
double commission = 0.0;
String gift = "";
[Link]("Enter the number of products : ");
product = [Link]();
if(product <= 50)
{
commission = product * 5 / 100;
gift = "A Parker Pen";
}
else if(product <= 75)
{
commission = product * 7.5 / 100;
gift = "A Micro SD Card";
}
else if(product <= 100)
{
commission = product * 10 / 100;
gift = "A Mobile";
}
else
{
commission = product * 15 / 100;
gift = "A Laptop";
}

[Link]("Number of sold products : "+product);


[Link]("Commission : "+commission);
[Link]("Gift : "+gift);

}
}
____________________________
import [Link];
class Q1_I{
public static void main() {
Scanner sc = new Scanner([Link]);
int salary;
double tax = 0.0;
[Link]("Enter annual taxable income : ");
salary = [Link]();

if(salary <=100000)
{
tax = 0.0;
}
else if(salary <=150000)
{
tax = (salary-100000) * 0.10;
}
else if(salary <=250000)
{
tax = 5000+(50000*0.10)+(salary-150000)*0.20;
}
else
{
tax = 25000+(50000*0.10)+(100000*0.20)+(salary-250000)*0.30;
}

[Link]("Annual Salary : "+salary);


[Link]("Tax amount : "+tax);
[Link]("Total Salary : "+(salary - tax));

}
}

import [Link];
class Q1_J{

public static void main()

Scanner sc = new Scanner([Link]);

[Link]("Enter any letter : ");

char ch = [Link]().charAt(0);

if((ch=='a') || (ch=='e') || (ch=='i') || (ch=='o') || (ch=='u') || (ch=='A') || (ch=='E') || (ch=='I') ||


(ch=='O') || (ch=='U'))

[Link]("Vowels");

else

[Link]("Consonent");

import [Link];
class Q2_A{
public static void main()
{
Scanner sc = new Scanner([Link]);
int r,h;
[Link]("1. Volume of Sphere. \n2. Volume of Cylinder. \n3. Volume of cone");
[Link]("Enter your choice");
int ch = [Link]();

switch(ch)
{
case 1:
[Link]("Enter the value of radius : ");
r = [Link]();
double sphere = 4 / (3 * [Link] * [Link](r,3));
[Link]("Volume of Sphere : "+sphere);
break;

case 2:
[Link]("Enter the value of radius : ");
r = [Link]();
[Link]("Enter the value of height : ");
h = [Link]();

double cylinder = [Link] * [Link](r,2) * h;


[Link]("Volume of Sphere : "+cylinder);
break;

case 3:
[Link]("Enter the value of radius : ");
r = [Link]();
[Link]("Enter the value of height : ");
h = [Link]();
double cone = ([Link] * [Link](r,2) * h)/3;
[Link]("Volume of Sphere : "+cone);
break;

default:
[Link]("Invalid Key Pressed!");
break;
}

}
}

___________________________________

import [Link];

public class Q2_B


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Type 1 to convert from Fahrenheit to Celsius");
[Link]("Type 2 to convert from Celsius to Fahrenheit");
int choice = [Link]();
double ft = 0.0, ct = 0.0;

switch (choice) {
case 1:
[Link]("Enter temperature in Fahrenheit: ");
ft = [Link]();
ct = 5 / 9.0 * (ft - 32);
[Link]("Temperature in Celsius: " + ct);
break;

case 2:
[Link]("Enter temperature in Celsius: ");
ct = [Link]();
ft = 1.8 * ct + 32;
[Link]("Temperature in Fahrenheit: " + ft);
break;

default:
[Link]("Incorrect Choice");
break;
}
}
}

______________________________________

import [Link];
public class Q2_C {
public static void main() {
Scanner sc = new Scanner([Link]);
int num,i;
[Link]("1. Prime Number \n2. Factorial");
int choice = [Link]();

switch(choice)
{
case 1:
int count=0;
[Link]("Enter any Number : ");
num = [Link]();
for(i=2; i<num; i++)
{
if(num%i == 0)
{
count++;
break;
}
}

if(count == 0)
{
[Link]("This is a Prime Number");
}
else
{
[Link]("This is not a Prime Number");
}
break;

case 2:
[Link]("Enter any Number : ");
num = [Link]();
long factorial = 1;
for(i = 1; i <= num; ++i)
{
factorial *= i;
}
[Link]("Factorial =" + factorial);
break;
}
}
}

______________________________________

import [Link];
class Q2_D{
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("1. Square Root of 9. \n2. Absolute Value of 126.4. \n3. Smallest value
of 56.7. \n4. Print any value from 0 and 1");
[Link]("Enter your choice");
int ch = [Link]();

switch(ch)
{
case 1:
[Link]("Square Root : "+[Link](9));
break;

case 2:
[Link]("Absolute Value : "+[Link](126.4));
break;

case 3:
[Link]("Smallest Value : "+[Link](56.7));
break;

case 4:
[Link]("Value between 0 and 1 : "+[Link]());
break;

default:
[Link]("Invalid Key Pressed!");
break;
}
}
}

___________________________________

import [Link];
class Q2_E{
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("1. Volume of Sphere. \n2. Volume of Cylinder. \n3. Volume of cone");
[Link]("Enter your choice");
int ch = [Link]();

switch(ch)
{
case 1:
[Link]("Enter the value of lenghth and Breadth : ");
int l = [Link]();
int b = [Link]();
double aor = l * b;
[Link]("Area of Rectangle : "+aor);
break;

case 2:
[Link]("Enter the value of side : ");
int side = [Link]();
double aos = [Link](side,2);
[Link]("Area of square : "+aos);
break;

case 3:
[Link]("Enter the value of radius : ");
int r = [Link]();
double aoc = [Link] * [Link](r,2);
[Link]("Are of Square : "+aoc);
break;

default:
[Link]("Invalid Key Pressed!");
break;
}

}
}

Common questions

Powered by AI

Switch-case statements in the Java program facilitate day-of-the-week displays based on user numerical input (1-7). They map each number to a specific weekday, and if an invalid number is entered, they default to informing the user of an invalid selection .

The Java program checks the validity of a triangle by taking three angle inputs and calculating their sum. If the sum equals 180 degrees, the program concludes the triangle is valid, otherwise it is not valid .

The program uses an if-else structure to classify numbers as even or odd. A number is even if it leaves no remainder when divided by 2 (num%2 == 0), otherwise it is odd. This logical structure allows decisions and outputs based on the parity of a user-input number .

The program ensures a logical input check by initially prompting for positive numbers only. It asks again if input is zero or negative, preventing invalid parity logic execution for non-positive numbers .

The logic error in evaluating the mode of payment arises in the assignment of cashback for E-Wallet, where mode 2 should instead be mode 3 based on the options provided. This duplication error could lead to unintended execution of discounted cash logic for Debit Card and skipping E-wallet logic entirely .

The tax in the Java program is calculated based on tiers of annual taxable income. If the income is up to 100,000, no tax is owed. Between 100,000 and 150,000, 10% is applied on the amount above 100,000. Between 150,000 and 250,000, an additional 20% is applied on income over 150,000, along with a fixed 5,000 from the previous band. Above 250,000, an additional 30% is applied to the amount above 250,000, with prior bands accounted for .

The program offers conversions between Fahrenheit and Celsius. The user selects 1 to convert Fahrenheit to Celsius using the formula ct = 5/9 * (ft - 32), or 2 to convert Celsius to Fahrenheit using ft = 1.8 * ct + 32. User choice manages which set of operations executes through a switch-case structure .

The program assigns gifts based on the number of products sold using conditional if-else statements. For up to 50 products, a Parker Pen is given; up to 75, a Micro SD Card; up to 100, a Mobile; beyond that, a Laptop. This graduated gift scheme incentivizes higher sales through more attractive rewards .

The program uses a switch-case statement allowing user choice between identifying prime numbers or calculating factorials. For primes, it checks divisibility from 2 to n-1; no divisors mean prime. For factorials, it iteratively multiplies numbers 1 through n. This choice combined with loops addresses different mathematical functionalities .

The discount for purchases over 5,000 in the Java program is 10% of the purchase amount. This discount is subtracted from the purchase amount to yield the payable total .

You might also like