[go: up one dir, main page]

0% found this document useful (0 votes)
2 views14 pages

Computer Project Class9(Amsc)

The document contains a series of Java programming exercises for class 9 students, covering topics such as Pythagorean triplets, leap years, number to words conversion, interest calculations, book billing with discounts, electricity bill calculations, prime and palindrome checks, Fibonacci series, pattern printing, and a menu-driven program for various number ranges. Each exercise includes code snippets and instructions on how to implement the required functionality. The exercises aim to enhance students' programming skills and understanding of conditional statements, loops, and user input handling.
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)
2 views14 pages

Computer Project Class9(Amsc)

The document contains a series of Java programming exercises for class 9 students, covering topics such as Pythagorean triplets, leap years, number to words conversion, interest calculations, book billing with discounts, electricity bill calculations, prime and palindrome checks, Fibonacci series, pattern printing, and a menu-driven program for various number ranges. Each exercise includes code snippets and instructions on how to implement the required functionality. The exercises aim to enhance students' programming skills and understanding of conditional statements, loops, and user input handling.
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
You are on page 1/ 14

Computer Project of class 9

1. Write a program to input three positive numbers, then check they are Pythagorean triplet or not. *USE if-
else + , hints : If x>y and x>z and x*x = y*y + z*z.

import java.util.Scanner;

class PythagorasCheck

public static void main (String args*+)

Scanner sc= new Scanner(System.in);

int a = sc.nextInt();

int b = sc.nextInt();

int c = sc.nextInt();

if (a > 0 && b > 0 && c > 0)

if (a > b && a > c && (a * a == b * b + c * c))

System.out.print("Pythagorean triplet");

else if (b > a && b > c && (b * b == a * a + c * c))

System.out.print("Pythagorean triplet");

else if (c > a && c > b && (c * c == a * a + b * b))

System.out.print("Pythagorean triplet");

else

System.out.print("Not Pythagorean triplet");

else

System.out.print("Negative number. Please enter only positive number except 0");


-

2. Write a program to ask the user to enter the year to check whether it is a century leap year or not. * Use if-
else +

import java.util.Scanner;

class LeapYearCheck

public static void main(String args*+)

Scanner sc = new Scanner(System.in);

int year = sc.nextInt();

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

if (year % 400 == 0)

System.out.print("Century leap year");

else

System.out.print("Non century leap year");

else

System.out.print("Not leap year");

3 . Wap to input a number within 0 to 99 .Print the number in words (Use Switch Case) Example 1 : Input 19 Example 1
: Input 93

Output : Nineteen Output : Ninety Three

import java.util.*;

class NumberToWords

, public static void main(String args*+)


, Scanner sc=new Scanner(System.in);

System.out.println("Enter A Number:");

int no=sc.nextInt();

if (num < 0 || num > 99)

System.out.println("Please enter a number between 0 and 99.");

If(no==0)

System.out.print(“Zero “);

else if(no>10 && no<=19)

switch(no)

case 11:System.out.print("Eleven ");break;

case 12:System.out.print("Twelve ");break;

case 13:System.out.print("Thirteen ");break;

case 14:System.out.print("Fourteen ");break;

case 15:System.out.print("Fifteen ");break;

case 16:System.out.print("Sixteen ");break;

case 17:System.out.print("SevenTeen ");break;

case 18:System.out.print("Eighteen ");break;

case 19:System.out.print("Nineteen ");break;

else

switch(no/10)

case 1:System.out.print("Ten ");break;

case 2:System.out.print("Twenty ");break;

case 3:System.out.print("Thirty ");break;

case 4:System.out.print("Forty ");break;

case 5:System.out.print("Fifty ");break;


case 6:System.out.print("Sixty ");break;

case 7:System.out.print("Seventy ");break;

case 8:System.out.print("Eighty ");break;

case 9:System.out.print("Ninety ");break;

switch(no%10)

case 1:System.out.print("One ");break;

case 2:System.out.print("Two ");break;

case 3:System.out.print("Three ");break;

case 4:System.out.print("Four ");break;

case 5:System.out.print("Five ");break;

case 6:System.out.print("Six ");break;

case 7:System.out.print("Seven ");break;

case 8:System.out.print("Eight ");break;

case 9:System.out.print("Nine ");break;

4. Write a program to input principal amount, rate of interest and number of years. Then input choice from user . 1)
Simple interest 2) Compound interest .

Calculate simple interest or compound interest by using the following formulae. Amount of Simple Interest. Si=p* r*
t/100 ;

Amount of Compound Interest =

P* 1 + ( r /100)+ t – P

* p=> principal amount, t=> number of year, r=> rate +

* Use ternary operator to perform interest calculation of the particular type. +

import java.util.Scanner;

class InterestCalculator

public static void main(String args*+)

,
Scanner sc = new Scanner(System.in);

System.out.print("Enter principal amount: ");

double principal = sc.nextDouble();

System.out.print("Enter rate of interest: ");

double rate = sc.nextDouble();

System.out.print("Enter number of years: ");

double time = sc.nextDouble();

System.out.println("Enter your choice: 1 for Simple Interest, 2 for Compound Interest");

int choice = sc.nextInt();

double interest = (choice == 1) ? (principal * rate * time) / 100 .0 : (Math.pow((1 + rate / 100), time) * principal -

principal);

System.out.println((choice == 1 ? "Simple Interest = " : "Compound Interest = ") + interest);

5. Write a program to input total cost of books purchased by a customer. Then calculate discount as per the following
rules. Finally calculate and print payable amount of the customer. Add 6.5% of payable amount as tax along with the
payable amount.

Total cost ------------------% of discount

<=1000 ------------------- 7% of total cost

>1000 and <=3000 ----------------------- 10% of the total cost

>3000 -------------- 20% of the total cost

import java.util.Scanner;

class BookBill ,

public static void main(String args*+) ,

Scanner sc = new Scanner(System.in);

System.out.print("Enter total cost: ");

double tc = sc.nextDouble();

double d;
if (tc <= 1000)

d = tc * 0.07;

else if (tc <= 3000)

d = tc * 0.10;

else

d = tc * 0.20;

double pa = tc - d;

double tx = pa * 0.065;

double ta = pa + tx;

System.out.println("Total Cost: " + tc);

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

System.out.println("Payable Amount: " + pa);

System.out.println("Tax: " + tx);

System.out.println("Total Amount to Pay: " + ta);

6. Write a program to calculate electricity bill. Enter meter reading of previous month and current month. Find out
how much unit of electricity were consumed by the customer. Then calculate the bill by using the following rules.
Input age of the person.

Charge Rs 800 as the rent of meter machine. In calculated bill add 14.5% tax to get final bill. If senior citizen then give
3% discount of final bill.

Unit consumed ------------- Charge

<=300 -------------- Rs 3 per unit of this range

>300 and <=600 ---------------- Rs. 6 per unit of this range

> 600 ---------------------- Rs. 10 per unit of this range

import java.util.Scanner;

class ElectricityBill ,

public static void main(String*+ args) ,

Scanner s = new Scanner(System.in);

System.out.print("Enter previous month meter reading: ");

int prev = s.nextInt();


System.out.print("Enter current month meter reading: ");

int curr = s.nextInt();

int unit = curr - prev;

System.out.print("Enter age of the customer: ");

int age = s.nextInt();

double charge = 0;

if (unit <= 300) ,

charge = unit * 3;

- else if (unit <= 600) ,

charge = 300 * 3 + (unit - 300) * 6;

- else ,

charge = 300 * 3 + 300 * 6 + (unit - 600) * 10;

double rent = 800;

double bill = charge + rent;

double tax = bill * 0.145;

double total = bill + tax;

if (age >= 60) ,

total = total - total * 0.03;

System.out.println("Units consumed: " + unit);

System.out.println("Charge: Rs " + charge);

System.out.println("Rent: Rs " + rent);

System.out.println("Tax (14.5%): Rs " + tax);

System.out.println("Total bill amount: Rs " + total);

-
7. Write a program which will input a number. Then it will first check the number is a prime number or not * by for
loop+ . Then it will check the number is palindrome number of not by while loop. Both the task will be done in the
same program one after the other.

Ans: import java.util.Scanner;

class Time

public static void main (String args*+)

Scanner sc = new Scanner(System.in);

System.out.print( " Enter number: " );

int n = sc.nextInt();

int c = 0;

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

if ( n % i == 0)

c++;

int b = n; int d = 0; int r = 0;

while ( b > 0 )

d = b % 10 ;

r = r * 10 + d;

b = b / 10;

if ( c == 2 && r == n)

System.out.print( " Prime palindrome ");

else if ( c == 2 )

System.out.print( " Prime but not palindrome ");

else if ( r == n )

,
System.out.print( " Palindrome but not prime ");

else

System.out.print( " Neither prime nor palindrome ");

8. Write a program to print first n terms of the Fibonacci series by for loop. Print their sum.

Then perform the same task but the terms which are even in Fibonacci series within n terms, only those terms will be
printed ( by while loop ).

import java.util.Scanner;

class FibonacciSeries ,

public static void main(String*+ args) ,

Scanner sc = new Scanner(System.in);

System.out.print("Enter number of terms (n): ");

int n = sc.nextInt();

//Print first n Fibonacci terms using for loop and sum them

int a = 0, b = 1, sum = 0;

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

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

System.out.print(a + " ");

sum += a;

int c = a + b;

a = b;

b = c;

System.out.println("\nSum of first " + n + " terms = " + sum);

//Print even Fibonacci terms within n terms using while loop

System.out.println("Even Fibonacci terms within first " + n + " terms:");

a = 0; b = 1;
int count = 1;

while (count <= n) ,

if (a % 2 == 0) ,

System.out.print(a + " ");

int c = a + b;

a = b;

b = c;

count++;

9. Write a program to print the following pattern. Full Height will be the input. Use only single loops ( do-while) in this
program.

1234321

111 111

12 21

1 1

12 21

111 111

1234321

import java.util.Scanner;

class Pat,

public static void main(String*+ args) ,

Scanner sc = new Scanner(System.in);

System.out.print("Enter odd height (>=3): ");

int n = sc.nextInt();

if (n < 3 || n % 2 == 0) ,

System.out.println("Please enter an odd number >= 3");

return;

int i = 1;

int mid = (n + 1) / 2;
do ,

int row = (i - 1) / n + 1;

int col = (i - 1) % n + 1;

if (row == 1 || row == n) ,

if (col <= mid)

System.out.print(col);

else

System.out.print(2 * mid - col);

- else if (row == 2 || row == n - 1) ,

if (col != mid)

System.out.print("1");

else

System.out.print(" ");

- else if (row == mid) ,

if (col == 1 || col == n)

System.out.print("1");

else

System.out.print(" ");

- else ,

int mirrorRow = row < mid ? row : n - row + 1;

if (col == 1 || col == 2)

System.out.print(col);

else if (col == n - 1 || col == n)

System.out.print(2 * mid - col);

else

System.out.print(" ");

if (col == n)
System.out.println();

i++;

- while (i <= n * n);

10. Write a program to print a menu

1. Buzz numbers in the range a to b.

2. Numbers which are multiple of 3 or 5 in the range a to b.

3. Numbers which is having odd value in unit and tens position in the range 100 to 999

import java.util.Scanner;

class MenuProgram ,

public static void main(String*+ args) ,

Scanner sc = new Scanner(System.in);

System.out.println("Menu:");

System.out.println("1. Buzz numbers in the range a to b.");

System.out.println("2. Numbers which are multiple of 3 or 5 in the range a to b.");

System.out.println("3. Numbers with odd unit and tens digits in the range 100 to 999.");

System.out.print("Enter your choice: ");

int choice = sc.nextInt();

int a = 0, b = 0;

switch(choice) ,

case 1:

System.out.print("Enter start of range (a): ");

a = sc.nextInt();

System.out.print("Enter end of range (b): ");


b = sc.nextInt();

System.out.println("Buzz numbers between " + a + " and " + b + ":");

for(int i = a; i <= b; i++) ,

if(i % 7 == 0 || i % 10 == 7) ,

System.out.print(i + " ");

break;

case 2:

System.out.print("Enter start of range (a): ");

a = sc.nextInt();

System.out.print("Enter end of range (b): ");

b = sc.nextInt();

System.out.println("Numbers multiple of 3 or 5 between " + a + " and " + b + ":");

for(int i = a; i <= b; i++) ,

if(i % 3 == 0 || i % 5 == 0) ,

System.out.print(i + " ");

break;

case 3:

System.out.println("Numbers between 100 and 999 with odd unit and tens digits:");

for(int i = 100; i <= 999; i++) ,

int lastTwo = i % 100;

int unit = lastTwo % 10;

int tens = lastTwo / 10;

if(unit % 2 == 1 && tens % 2 == 1) ,

System.out.print(i + "\n");

-
break;

default:

System.out.println("Invalid choice!");

You might also like