Computer Java Project
Computer Java Project
Project
Name-Roshan Sharma
Class-9
Roll no-17
Topic-25 Java programming
School-DSM school for
excellence
Given by-Arijit Sir
Content
Serial no Topic Page
no
1. Acknowledgment 2
3. If else 3
4. Else if 14
5. For loop 30
6. While loop 36
7. Bibliography 41
1
Acknowledgment
I am over helmed in all humbleness and
gratefulness to acknowledge my depth to all
those who have helped me to put these ideas,
well above the level of simplicity and into
something concrete.
I would like to express my special thanks of
gratitude to my teacher as well as our principal
who gave me the golden opportunity to do this
wonderful project on the topic Java programing,
which also helped me in doing a lot of Research
and i came to know about so many new things. I
am really thankful to them.
I would like to thank my parents who helped me
a lot in gathering different information,
collecting data and guiding me from time to
time in making this project, despite of their busy
2
schedules, they gave me different ideas in
making this project unique.
3
BufferedReader in =new
BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the C.P and
S.P :");
int a,b, profit=0,loss=0;
a=Integer.parseInt(in.readLine());
b=Integer.parseInt(in.readLine());
if(a<b){
profit=b-a;
System.out.println("It’s profit of " +profit);
}
else
{
loss=a-b;
System.out.println("It’s loss of " +loss);
4
}
}}
Output:
Enter the C.P and S.P :
1000
800
It’s loss of 200
5
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
int num = sc.nextInt();
if(num % 2 == 0)
{
System.out.println("Number " +num+ " is
even");
}
else
{
System.out.println("Number " +num+" is
odd");
}
}
}
6
Output:
Enter a number:
28
Number 28 is even
7
if (age>=18)
{
System.out.println(“eligible to vote”);
}
else
{
System.out.println(“not eligible to vote”);
}}}
Output-
Enter an age-
23
Eligible to vote
Q4 WAP to check whether the number is
divisible by 5 or not.
Program:-
import.java.io.*;
class program
8
{
public static void main (String args[])throws
IOException
{
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
int n;
System.out.println(“enter a number”);
n =Integer.parseInt(in.readLine());
if (n%5==0)
{
System.out.println(“Divisible”);
}
else
{
System.out.println(“Not divisible”);
}}}
9
Give output-
Enter a number
625
Divisible
11
Program:-
import java.util.*;
class main
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the sides of triangle”);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if((a+b>c)&&(a+c>b)&&(b+c>a))
{
System.out.println(“Its possible”);
}
else
{
12
System.out.println(“Its not possible”);
}
}
}
Output:-
Enter the sides of triangle
5
3
7
Its possible
13
import java.util.*;
class program{
public static void main(String args[])throws
IOException{
Scanner sc=new Scanner(System.in));
System.out.println(“Enter a year”);
int year=sc.nextInt();
if (year%4==0){
System.out.println(“Leap year”);
}
else{
System.out.println(“Not a leap year”);
}}}
Output:-
Enter year
2020
Leap year
14
Q8 Wap to accept 3 numbers and find the
greatest number.
Program:-
import.java.io.*;
class program
{
public static void main(String args[])throws
IOException
{
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
int a,b,c;
System.out.println(“Enter three numbers”);
a =Integer.parseInt(in.readLine());
b =Integer.parseInt(in.readLine());
c =Integer.parseInt(in.readLine());
if (a>b&&a>c)
{
15
System.out.println(a+“is the greatest number”);
}
else if (b>a&&b>c)
{
System.out.println(b+" is the greatest number”);
}
else
{
System.out.println(c+“is the greatest number”);
}
}
}
Give output-
Enter three number
18
20
54
16
54 is the greatest number
Q9 W.A.P to create a printing application
program where we will take the number of
copies to be printed as input from the user and
then prints the price per copy and the total price
for the printing copies.
The chart price to print the number of copies is
given below:
0 – 99: $0.30 per
copy
100 – 499 $0.28 per
copy
500 – 799 $0.27 per
copy
800 – $0.26 per Program:-
1000 copy
Over $0.25 per import java.util.Scanner;
1000 copy
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
17
System.out.println("Enter the number of copies
to be printed:");
int noOfCopies = sc.nextInt();
if(noOfCopies > 0 && noOfCopies < 100){
double pricePerCopies = 0.30;
System.out.println("Price per copy: "+"$"
+pricePerCopies);
double totalCost = noOfCopies *
pricePerCopies;
System.out.println("Total cost is "+"$"
+totalCost);
}
else if(noOfCopies >= 100 && noOfCopies <
500){
double pricePerCopies = 0.28;
System.out.println("Price per copy: "+"$"
+pricePerCopies);
18
double totalCost = noOfCopies *
pricePerCopies;
System.out.println("Total cost is "+"$"
+totalCost);
}
else if(noOfCopies >= 500 && noOfCopies <
800){
double pricePerCopies = 0.27;
System.out.println("Price per copy: "
+"$"+pricePerCopies);
double totalCost = noOfCopies *
pricePerCopies;
System.out.println("Total cost is "+"$"
+totalCost); }
else if(noOfCopies >= 800 && noOfCopies <
1000){
double pricePerCopies = 0.26;
System.out.println("Price per copy: "+"$"
+pricePerCopies);
19
double totalCost = noOfCopies *
pricePerCopies;
System.out.println("Total cost is "+"$"
+totalCost);
}
else {
double pricePerCopies = 0.25;
System.out.println("Price per copy: "+"$"
+pricePerCopies);
double totalCost = noOfCopies *
pricePerCopies;
System.out.println("Total cost is "
+"$"+totalCost);
}}}
Output:
Enter the number of copies to be printed:
1151
Price per copy: $0.25
20
Total cost is $287.75
Q10 WAP to check whether a number is
positive, negative or zero.
import java.io.*;
class program
{
public static void main(String args[])throws
IOException
{
BufferedReader in=new
BufferedReader(new
InputStreamReader(System.in));
int n;
System.out.println(“Enter a number”);
n =Integer.parseInt(in.readLine());
if (n>0)
{
21
System.out.println(“Positive number”);
}
else if(n<0)
{
System.out.println(“Negative number”);
}
else
{
System.out.println(“Zero”);
}
}
}
Give output-
Enter a number-
46
22
Positive number.
Q11 Write a program to input 3 unique
integers and print the smallest among them.
Program:
import java.util.*;
class main
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println(“Enter 3 integers:”);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a<b && a<c)
23
System.out.println(“Smallest:”+a);
else if(b<a && b<c)
System.out.println(“Smallest:”+b);
else
System.out.println(“Smallest:”+c);
}
}
Output:-
Enter 3 integers:
4
25
1
Smallest:1
24
Q12 Write a program to accept three sides of a
triangle as parameter and check whether it can
form a triangle or not. If it forms a triangle,
check whether it is an acute angled, obtuse
angled
or right-angled triangle.
(Hint: To form a triangle, each side should be
less the sum of the other two sides.
To form an acute angled triangle the square of
every side should be less than the sum of the
squares of the other two sides.
To form an obtuse angled triangle the square of
any side should be greater than the sum of the
squares of the other two sides.
To form an right angled triangle the square of
any side should be equal to the sum of the
squares of the other two sides.)
25
Program:
import java.util.*;
class main
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println(“Enter 3 sides:”);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a<b+c && b<a+c && c<a+b)
{
if(a*a<b*b+c*c && b*b<a*a+c*c &&
c*c<a*a+b*b)
26
{
System.out.println(“Acute angled triangle”);
}
else if(a*a>b*b+c*c || b*b>a*a+c*c ||
c*c>a*a+b*b){
System.out.println(“Obtuse angled triangle”);
}
else{
System.out.println(“Right angled triangle”);
}}
else{
System.out.println(“Cannot form a triangle”);
}}}
Output:
Enter 3 sides:
3
5
27
7
Obtuse angled triangle
import java.util.*;
class main
{
public static void main(String[]args){
28
Scanner sc=new Scanner(System.in);
int c;
System.out.println(“Enter marks in Computer
science:”);
c=sc.nextInt();
if(c>=90)
System.out.println(“Grade=A”);
else if(c>=70 && c<90)
System.out.println(“Grade=B”);
else if(c>=50 && c<70)
System.out.println(“Grade=C”);
else
System.out.println(“Grade=D”);
}
}
Output:-
29
Enter marks in Computer science:
88
Grade=B
30
{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
float tc,d,ap;
System.out.println("Enter the total cost of the
items:");
tc=sc.nextFloat();
if(tc<=2000)
d=5/100f*tc;
else if(tc>=2001 && tc<=5000)
d=25/100f*tc;
else if(tc>=5001 && tc<=10000)
d=35/100f*tc;
else
d=50/100f*tc;
ap=tc-d;
System.out.println("Amount Payable:"+ap);
31
}
}
Output:-
Enter the total cost of the items:
5456
Amount Payable:3546.4
Q15 Write a program to find the sum of all 3
digit odd natural numbers, which are multiples
of 5.
class ans
{
public static void main(String[]args)
{
int i,s=0;
for(i=101;i<=999;i+=2)
{
if(i%5==0)
32
s+=i;
}
System.out.print(“Sum=”+s);
}
}
Output:-
Sum=49500
Q16 Write a program to input an integer and
find its factorial.
import java.util.*;
class ans1{
public static void main(String[]args){
int i,n,f=1;
Scanner sc=new Scanner(System.in);
System.out.print(“Enter an integer:”);
n=sc.nextInt();
for(i=1;i<=n;i++){
33
f=f*i;
}
System.out.print(“Factotrial:”+f);
}}
Output:-
Enter an integer:
5
Factotrial:120
Q17 Write programs for each of the following to
print the series: 1, 2, 4, 7, 11, 16, 22, 29, …,
upto n terms.
import java.util.*;
class Sol17{
public static void main(String[]args){
int i,n,s=1;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of
terms:");
34
n=sc.nextInt();
for(i=1;i<=n;i++){
System.out.print(s+" ");
s=s+i;
}}}
Output:-
Enter the number of terms:
15
1 2 4 7 11 16 22 29 37 46 56 67 79 92 106
Q18 Write programs for each of the following to
print the series: 2, 4, 8, 14, 22, 32, 44, 58, …,
upto n terms
import java.util.*;
class Sol{
public static void main(String[]args){
int i,n,s=2,c=2;
Scanner sc=new Scanner(System.in);
35
System.out.print("Enter the number of terms:");
n=sc.nextInt();
for(i=1;i<=n;i++){
System.out.print(s+" ");
s=s+c;
c=c+2;
}}}
Output:-
Enter the number of terms:15
2 4 8 14 22 32 44 58 74 92 112 134 158 184 212
Q19 Write programs for each of the following to
print the series: 1, 2, 5, 10, 17, 26, 37, 50, …,
upto n terms.
import java.util.*;
class Sol{
public static void main(String[]args){
int i,n,s=1,c=1;
36
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of terms:");
n=sc.nextInt();
for(i=1;i<=n;i++){
System.out.print(s+" ");
s=s+c;
c=c+2;
}}}
Output:-
Enter the number of terms:15
1 2 5 10 17 26 37 50 65 82 101 122 145 170 197
Q20 WAP to print the series: 1, 1, 2, 3, 5, 8, 13,
…, upto n terms.
import java.util.*;
class Sol20{
public static void main(String[]args){
int i,n,f=1,s=0,t;
37
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of terms:");
n=sc.nextInt();
for(i=1;i<=n;i++){
t=f+s;
System.out.print(t+" ");
f=s;
s=t;
}}}
Output:-
Enter the number of terms:15
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
Q21 W.A.P to add the number from 1 to 10
using while loop and display the sum on the
console.
Program:-
public class Test {
38
public static void main(String[] args)
{
int sum = 0, i = 1;
while (i <= 10) {
sum = sum + i;
i++;
}
System.out.println("sum is " + sum);
}
}
Output:-sum is 55
39
public static void main(String[] args)
{
int x = 10, y = 20;
while (++x < --y); // No body in this loop.
System.out.println("Mid value is " + x);
}
}
Output:
Mid value is 15
40
System.out.println(i);
i--;
}}}
Output:-10
9
8
7
6
5
4
3
2
1
Q24 Wap a program where we will display
numbers from 1 to 6.
Program:-
public class Test {
41
public static void main(String[] args)
{
int x;
x = 1; // starting number is 1.
while(x <= 6)
{
System.out.print(x+“ “); // print x value.
x++; // increment x value by 1.
} // This statement will be executed as long as
x <= 6.
}
}
Output:1 2 3 4 5 6
42
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int num = 0, sum = 0;
Scanner sc = new Scanner(System.in);
while(num != 0)
{
// Read the next input.
System.out.println("Enter an integer
number");
num = sc.nextInt();
sum = sum + num;
}
System.out.println("Sum of numbers: " +sum);
}
43
}
Output:
Enter an integer number
20
Enter an integer number
10
Enter an integer number
5
Enter an integer number
40
Enter an integer number
0
Sum of numbers: 75
44
Bibliography
In this project I have taken help from my
course book
ICSE UNDERSTANDING COMPUTER
APPLICATIOS WITH BLUEJ CLASS
IX written by Vijay kumar pandey and
Dilip kumar dey and published by Arya
Publishing Company
45