Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Computer

Project
Term – 2
AY – 2020 - 2021
Java programs
Prisha Dalal
9-B
1|Page
Index
Sr.no topic Pg. no
1 Program 1 3,4
2 Program 2 5,6
3 Program 3 7,8
4 Program 4 9,10
5 Program 5 11,12,13
6 Program 6 14,15
7 Program 7 16,17,18
8 Program 8 19,20
9 Program 9 21,22
10 Program 10 23,24
11 Program 11 25,26
12 Program 12 27,28
13 Program 13 29,30
14 Program 14 31,32
15 Program 15 33,34
16 acknowledgements 35

2|Page
Program 1
1. Write a program to enter two numbers. If the first number is
greater than display square of the smaller number and cube of
the greater number otherwise vice versa. If the numbers are
equal, display the message “both number are equal.”

Code:

3|Page
Output:

Variable description:
Variable type use
name
a Integer To accept the value from the
user
b Integer To accept the value from the
user
sq Integer To store the value after
squaring
cu integer To store the value after cubing

4|Page
Program 2
2. Write a program to enter three numbers and A character. Find and
print sum of the numbers If a given character is ‘s’ and product of the
numbers If a given character is ‘p’. The program displays a message
“invalid character” if the user enters another alphabet

Code:

5|Page
Output:

Variable description:
name type use
a Integer Accept value from user
b Integer Accept value from user
c Integer Accepter value from user
d Integer To Store sum of numbers
f Integer To Store product of
numbers
e character Accept value from user
6|Page
Program 3
3. Write a program to input three unequal numbers and display the
second smallest number

Code:

7|Page
Output:

Variable description:
Name Type Use
a Integer To accept the number from
the user
b Integer To accept the number from
the user
c Integer To accept the number from
the user
d Integer The store the greater number
from a and b
e integer To store and print the greater
number from c and d

8|Page
Program 4
4. Write a program to enter three sides of a triangle and check whether
the triangle is possible or not. If possible then display whether it is
equilateral, an isosceles or scalene triangle otherwise display the
message “triangle is not possible

Code:

9|Page
Output:

Variable description:
Variable type use
name
a integer To accept the side of the
triangle
b integer To accept the side of the
triangle
c integer To accept the side of the
traingle
10 | P a g e
Program 5
write a program to input the amount of purchase. The customer has to
pay 12.5% as sales tax on price after the discount. Print net amount. Enter
‘1’ for AC and ‘2’ for TV

code:

11 | P a g e
12 | P a g e
Output:

Variable description:
name type use
p Integer To acceptand print amount of
purchase
k Integer To accept users choice
d Double To store and print value of discount
rp Double Used in calculation
np Double To store and print total amount
tax Double To store and print the tax
name string To accept and print name of the
customer
13 | P a g e
Program 6
6.Write a program to accept length and breadth of a rectangle. Calculate
and display the area, perimeter or diagonal of the rectangle as per the
user’s choice

Code:

14 | P a g e
Output:

Variable description:
name type use
l integerTo accept value of length
b IntegerTo accept value of breadth
Ar IntegerTo store and display area
pr IntegerTo store and display perimeter
n integerUsed as the switch case
variable
d Double To store and print the diagonal
15 | P a g e
Program 7
7. Write a program that displays the result of the following evaluations
based on the number entered by the user.
a) absolute value of a number b) square root of a number
c) cube root of a number. d) random number between 0 and 1

code:

16 | P a g e
Output:

17 | P a g e
Variable description:
Variable type Use
name
n Integer To accept the
number and perform
the necessary
calculation
ch integer Used to accept the
users choice and also
as the switch case
variable
18 | P a g e
Program 8
8. 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.

Code:

19 | P a g e
Output:

Variable description:

Variable type use


name
a integer To accept and
store the
value from
the user

20 | P a g e
Program 9
9. Write the program to accept any 10 numbers. Print the sum of only the
negative values

Code:

21 | P a g e
Output:

Variable description:
Name type Use
a Integer As the loop variable
n Integer To accept the value
from the user
s integer Used to store and
print the sum of
negative values

22 | P a g e
Program 10
10. Write a program to display all odd numbers from 1 to 20 by using
continue statement in while loop.

Code:

23 | P a g e
Output:

Variable description:

Name type use


a integer Used as the loop
variable

24 | P a g e
Program 11
11. Write a program to display the sum of positive even numbers and
negative odd numbers entered by the user.

Code:

25 | P a g e
Output:

Variable description:
Name type Use
n Integer As the loop variable
s1 Integer Used to calculate and
print the sum of positive
even numbers
s2 nteger Used to calculate and
print the sum of negative
odd numbers

26 | P a g e
Program 12
12. Write a program to display 10 natural numbers from 1 to 10

Code:

27 | P a g e
Output:

Variable description:
Name Type Use
a integer Loop variable

28 | P a g e
Program 13
13. Write a program to display the sum of any two numbers for ten
iterations. If sum of two numbers is negative then the program terminates

Code:

29 | P a g e
Output:

Variable description:
Name Type Use
a Integer To accept the number from the
user
b Integer To accept the number from the
user
c Integer To store and print the sum of a
and b if it is more than 0
i Integer As the loop variable
30 | P a g e
Program 14
14. Write a program in java to display the first 10 numbers of the Fibonacci
series
0,1,1,2,3,5,8…

Code:

31 | P a g e
Output:

Variable description:
Name Type Use
n Integer To accept the length of the
series
a Integer Used in the calculation
b Integer To store the sum of a and b
and print its value
i Integer As the loop variable
temp Integer Used in the calculation

32 | P a g e
Program 15
15. Write a program to input any 10 numbers (including positive and
negative).
Perform the following tasks:
(a) Count the positive numbers
(b) Count the negative numbers
(c) Sum of positive numbers
(d) Sum of negative numbers

Code:

33 | P a g e
Output:

Variable description:
Name Type Use
pSum Integer To store and print the sum of
positive numbers
pCount Integer To keep a count and print the
number of positive numbers
nSum Integer To store and print the sum of
negative numbers
nCount Integer To keep a count and print the
number of negative numbers
i Integer As the loop variable
n integer To accept the number from the
user
34 | P a g e
Acknowledgements:
I would like to thank my:-
▪ Teachers
▪ Books
▪ Parents
▪ Friends
▪ Google
▪ Internet
To help me finish this project and make it so
well. I couldn't have done it without them.

35 | P a g e

You might also like