0% found this document useful (0 votes)
65 views29 pages

Java Printing PDF

The document contains 9 Java classes that solve different programming problems. Each class contains a main method that takes user input, performs calculations, and prints outputs. For example, class Question1 defines a Box class to calculate the volume of a box by taking the length, breadth and height as user input. It then prints the dimensions and calculated volume.

Uploaded by

Anurag Kumar
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)
65 views29 pages

Java Printing PDF

The document contains 9 Java classes that solve different programming problems. Each class contains a main method that takes user input, performs calculations, and prints outputs. For example, class Question1 defines a Box class to calculate the volume of a box by taking the length, breadth and height as user input. It then prints the dimensions and calculated volume.

Uploaded by

Anurag Kumar
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/ 29

Class BOX 1/1

1 //Holiday Homework-Question 2
2 /**
3 Create a class ‘BOX’ to find the volume of a box, taking its length, br e
adth
4 and height from the user . Finally print the given dimensions and the vol
ume of the box.
5 */
6 import java.io.*;
7 public class BOX
8 {
9 InputStreamReader isr = new InputStreamReader (System.in);
10 BufferedReader br = new BufferedReader(isr);
11 public void main(String[]Args) throws IOException
12 {
13 System.out.println("Enter the 'length' of the box " ) ;
14 int l = Integer.parseInt(br.readLine());
15 System.out.println("Enter the 'breadth' of the box " ) ;
16 int b = Integer.parseInt(br.readLine());
17 System.out.println("Enter the 'height' of the box " ) ;
18 int h = Integer.parseInt(br.readLine());
19 int volume=l*b*h;
20 System.out.println("The length is " +l) ;
21 System.out.println("The breadth is " +b) ;
22 System.out.println("The height is " +h) ;
23 System.out.println("The Volume of the BOX is : " +volume) ;
24 }
25 }
26

Nov 3, 2014 10:06:20 PM


Enter the 'length' of the box

5
Enter the 'breadth' of the box

4
Enter the 'height' of the box

2
The length is 5

The breadth is 4

The height is 2

The Volume of the BOX is : 40


Class Question3 1/1

1 //Holiday Homework-Question 3
2 /**
3 WAP to find out whether a number is even or odd or prime.
4 */
5 import java.io.* ;
6 public class Question3
7 {
8 InputStreamReader isr = new InputStreamReader (System.in);
9 BufferedReader br = new BufferedReader(isr);
10 public void main(String[]Args) throws IOException
11 {
12 System.out.println("Enter the number" ) ;
13 int n = Integer.parseInt(br.readLine());
14 int flag=0;
15 if(n%2!=0)
16 {
17 System.out.println("The number is ODD" );
18 for(int i=2; i<n; i++)
19 {
20 if(n%i==0)
21 {
22 flag=1;
23 break;
24 }
25 }
26 if(flag==0)
27 System.out.println("The number is also PRIME" );
28 }
29 else
30 System.out.println("The number is EVEN" );
31 }
32 }
33
34
35

Nov 3, 2014 10:07:48 PM


Enter the number

17
The number is ODD

The number is also PRIME


Class Question4 1/1

1 //Holiday Homework-Question 4
2 /**
3 Program to print the grade sheet of a student.
4 */
5 import java.io.* ;
6 public class Question4
7 {
8 InputStreamReader isr = new InputStreamReader (System.in);
9 BufferedReader br = new BufferedReader(isr);
10 public void main(String[]Args) throws IOException
11 {
12 System.out.println("Enter the number of marks out of 100" ) ;
13 int marks = Integer.parseInt(br.readLine());
14 if(marks>100 || marks<0)
15 {
16 System.out.println("ERROR! Invalid User Input" );
17 }
18 else if (marks>80)
19 {
20 System.out.println("The Student has got GRADE A" );
21 }
22 else if (marks>70)
23 {
24 System.out.println("The Student has got GRADE B" );
25 }
26 else if (marks>60)
27 {
28 System.out.println("The Student has got GRADE C" );
29 }
30 else if (marks>50)
31 {
32 System.out.println("The Student has got GRADE D" );
33 }
34 else if (marks<50)
35 {
36 System.out.println("The Student has got GRADE E" );
37 }
38 }
39 }
40

Nov 3, 2014 10:08:42 PM


Enter the number of marks out of 100

78
The Student has got GRADE B
Class Question5 1/1

1 /**
2 To write a program to find the sum of the following series:
3 S= 1+4+9+16+……….+ n terms
4 */
5 import java.io.* ;
6 public class Question5
7 {
8 InputStreamReader isr = new InputStreamReader (System.in);
9 BufferedReader br = new BufferedReader(isr);
10 public void main(String[]Args) throws IOException
11 {
12 System.out.println("Enter the number of terms" ) ;
13 int n = Integer.parseInt(br.readLine());
14 double sum=0 ;
15 for(int i=1; i<=n ; i++)
16 {
17 sum+=Math.pow(i,2);
18 }
19 System.out.println("The Sum of the series is : " +sum);
20 }
21 }
22
23

Nov 3, 2014 10:09:29 PM


Enter the number of terms

5
The Sum of the series is : 55.0
Class Question6 1/1

1 //Holiday Homework-Question6
2 /**
3 WAP to read a number from the user and print the reverse of this number.
4 */
5 import java.io.* ;
6 public class Question6
7 {
8 InputStreamReader isr = new InputStreamReader (System.in);
9 BufferedReader br = new BufferedReader(isr);
10 public void main(String[]Args) throws IOException
11 {
12 System.out.println("Enter the number") ;
13 int num = Integer.parseInt(br.readLine());
14 int n = num;
15 int k; int rev=0;
16 while(num>0)
17 {
18 k=num%10 ;
19 rev=(rev*10) + k ;
20 num=num/10 ;
21 }
22 System.out.print("The reverse of the number you entered is : " +r
ev) ;
23 }
24 }
25

Nov 3, 2014 10:10:37 PM


Enter the number

174
The reverse of the number you entered is : 471
Class Question7 1/1

1 //Holiday Homework-Question7
2 /**
3 WAP to find out whether a yearr (entered in 4 digit number) is a leap yea
r .
4 */
5 import java.io.* ;
6 public class Question7
7 {
8 InputStreamReader isr = new InputStreamReader (System.in);
9 BufferedReader br = new BufferedReader(isr);
10 public void main(String[]Args) throws IOException
11 {
12 System.out.println("Enter the number of terms" ) ;
13 int year = Integer.parseInt(br.readLine());
14 if (year <1000)
15 System.out.println("ERROR! Please enter a minimum of 4 digits" );
16 {
17 if (year%100==0)
18 {
19 if (year%400==0)
20 System.out.println("It is a Leap Year");
21 else System.out.println("It is NOT a Leap Year" );
22 }
23 else
24 {
25 if (year%4==0)
26 System.out.println("It is a Leap Year");
27 else System.out.println("It is NOT a Leap Year" );
28 }
29 }
30 }
31 }

Nov 3, 2014 10:11:27 PM


Enter the year number

2000
It is a Leap Year
Class Question8 1/1

1 //Holiday Homework-Question 8
2 /**
3 WAP to print Fibonacci series i.e., 0 1 1 2 3 5 8 13 ………..
4 */
5 import java.io.* ;
6 public class Question8
7 {
8 InputStreamReader isr = new InputStreamReader (System.in);
9 BufferedReader br = new BufferedReader(isr);
10 public void main(String[]Args) throws IOException
11 {
12 System.out.println("Enter the number of terms" ) ;
13 int n = Integer.parseInt(br.readLine());
14 int a=0; int b=1; int c=0 ; int i;
15 System.out.println("Here's comes the Fibonacci Series ------->>>>
>");
16 System.out.println(a);
17 System.out.println(b);
18 for(i=3; i<=n ; i++)
19 {
20 c=a+b ;
21 System.out.println(c);
22 a=b;
23 b=c;
24 }
25 }
26 }
27
28

Nov 3, 2014 10:13:58 PM


Enter the number of terms

9
Here's comes the Fibonacci Series ------->>>>>

13

21
Class Question9 1/1

1 //Holiday Homework-Question9
2 /**
3 WAP to find the lCM(least common multiple) and GCD(Greatest Common Diviso
r) of
4 two numbers.
5 */
6 import java.io.* ;
7 public class Question9
8 {
9 InputStreamReader isr = new InputStreamReader (System.in);
10 BufferedReader br = new BufferedReader(isr);
11 public void main(String[]Args) throws IOException
12 {
13 int a,b,lcm=1; int gcd=1;
14 System.out.print("Enter the 1st number : " );
15 a=Integer.parseInt(br.readLine());
16 System.out.print("Enter the 2nd number : " );
17 b=Integer.parseInt(br.readLine());
18 for(int i=a;i<a*b;i++)
19 {
20 if(i%a==0 && i%b==0)
21 {
22 lcm=i;
23 break;
24 }
25 }
26 gcd=a*b/lcm ;
27 System.out.println("L.C.M. = "+lcm);
28 System.out.println("G.C.D. = "+gcd);
29 }
30 }
31
32
33
34
35
36

Nov 3, 2014 10:15:57 PM


Enter the 1st number : 514
Enter the 2nd number : 28
L.C.M. = 7196

G.C.D. = 2
Class Special 1/1

1 //Holiday Homework-Question10
2 /**
3 Design a class Special to check if a given number is a special number or
not. And take
4 the number as an input from the user.
5 */
6 import java.io.*;
7 public class Special
8 {
9 InputStreamReader isr = new InputStreamReader (System.in);
10 BufferedReader br = new BufferedReader(isr);
11 public void main(String[]Args) throws IOException
12 {
13 System.out.println("Enter the number ") ;
14 int n = Integer.parseInt(br.readLine());
15 double sum=0 ; int fact =1;
16 {
17 for(int i=n;i>0;i=i/10)
18 {
19 int j=i%10 ;
20 for(; j >0; j--)
21 {
22 fact*=j ;
23 }
24 sum+=fact ;
25 fact=1 ;
26 }
27 if(sum==n)
28 System.out.println(n+ " is a special number " );
29 else
30 System.out.println(n+ " is not a special number " );
31 }
32 }
33 }
34

Nov 3, 2014 10:16:37 PM


Enter the number

145
145 is a special number
Class Question3 1/1

1 //Worksheet4-Question3
2 /**
3 WAP to find the sum of all the even and odd numbers within the given rang
e.
4 */
5 import java.io.* ;
6 public class Question3
7 {
8 InputStreamReader isr = new InputStreamReader (System.in);
9 BufferedReader br = new BufferedReader(isr);
10 public void main(String[]Args) throws IOException
11 {
12 System.out.println("Enter the lower limit" ) ;
13 int l = Integer.parseInt(br.readLine());
14 System.out.println("Enter the upper limit" ) ;
15 int u = Integer.parseInt(br.readLine());
16 int SumEven=0; int SumOdd=0;
17 for(int i=l; i<=u;i++)
18 {
19 if (i%2==0)
20 {
21 SumEven+=i;
22 }
23 else if (i%2!=0)
24 {
25 SumOdd+=i;
26 }
27 }
28 System.out.println("The sum of even numbers within the range is :
" +SumEven);
29 System.out.println("The sum of even numbers within the range is :
" +SumOdd);
30 }
31 }
32

Nov 3, 2014 10:46:52 PM


Enter the lower limit

5
Enter the upper limit

15
The sum of even numbers within the range is : 50

The sum of even numbers within the range is : 60


Class Question5 1/1

1 //Worksheet4-Question5
2 /**
3 WAP to print the following pattern:
4 For n=4:
5 1234321
6 123 321
7 12 21
8 1 1
9 */
10 import java.io.* ;
11 public class Question5
12 {
13 InputStreamReader isr = new InputStreamReader (System.in);
14 BufferedReader br = new BufferedReader(isr);
15 public void main(String[]Args) throws IOException
16 {
17 System.out.println("Enter the number of lines" ) ;
18 int n = Integer.parseInt(br.readLine());
19 for(int i=n;i>=1;i--)
20 {
21 for(int j=1;j<=i;j++)
22 {
23 System.out.print(j);
24 System.out.print(" ");
25 }
26 for(int k=n; k> i; k--)
27 {
28 System.out.print(" ");
29 }
30 for(int l=i;l>=1;l--)
31 {
32 System.out.print(l);
33 System.out.print(" ");
34 }
35 System.out.println();
36 }
37 }
38 }
39

Nov 3, 2014 10:47:42 PM


Class Question7 1/1

1 //Worksheet4-Question7
2 /**
3 WAP to find whether a number is pal prime or not.
4 */
5 import java.io.* ;
6 public class Question7
7 {
8 InputStreamReader isr = new InputStreamReader (System.in);
9 BufferedReader br = new BufferedReader(isr);
10 public void main(String[]Args) throws IOException
11 {
12 System.out.println("Enter the number") ;
13 int num = Integer.parseInt(br.readLine());
14 int n = num;
15 int k; int rev=0; int flag=0;
16 while(num>0)
17 {
18 k=num%10 ;
19 rev=(rev*10) + k ;
20 num=num/10 ;
21 }
22 {
23 for(int i=2; i<num; i++)
24 {
25 if (i%2==0)
26 {
27 flag=1;
28 }
29 }
30 if (flag==0 && rev==n)
31 {
32 System.out.println("The number you just entered is a 'Pal
Prime'");
33 }
34 }
35 }
36 }
37

Nov 3, 2014 10:48:27 PM


Enter the number

191
The number you just entered is a 'Pal Prime'
Class Question6a 1/1

1 //Worksheet4-Question6(a)
2 /**
3 WAP to find the sum of the following series:
4 a) S= 7+ 77 + 777+.........n terms
5 */
6 import java.io.* ;
7 public class Question6a
8 {
9 InputStreamReader isr = new InputStreamReader (System.in);
10 BufferedReader br = new BufferedReader(isr);
11 public void main(String[]Args) throws IOException
12 {
13 System.out.println("Enter the number of terms" ) ;
14 int num = Integer.parseInt(br.readLine());
15 int sum=0; int term=0;
16 for(int i=1; i<=num; i++)
17 {
18 term=term*10 +7;
19 sum+=term ;
20 }
21 System.out.println("The sum of the series is " +sum);
22 }
23 }
24

Nov 3, 2014 10:49:37 PM


Enter the number of terms

5
The sum of the series is 86415
Class Question6b 1/1

1 //Worksheet4-Question6(b)
2 /**
3 WAP to find the sum of the following series:
4 a) S= 7+ 77 + 777+.........n terms
5 */
6 import java.io.* ;
7 public class Question6b
8 {
9 InputStreamReader isr = new InputStreamReader (System.in);
10 BufferedReader br = new BufferedReader(isr);
11 public void main(String[]Args) throws IOException
12 {
13 System.out.println("Enter the number of terms" ) ;
14 int num = Integer.parseInt(br.readLine());
15 System.out.println("Enter the number") ;
16 int n = Integer.parseInt(br.readLine());
17 int sum=1; double term=0;
18 for(int i=2; i<=num; i++)
19 {
20 term=Math.pow(n,i);
21 sum+=term ;
22 }
23 System.out.println("The sum of the series is " +sum);
24 }
25 }

Nov 3, 2014 10:50:12 PM


Enter the number of terms

8
Enter the number

2
The sum of the series is 509
Class Question10 1/1

1 //Worksheet4-Question10
2 /**
3 Write program to input Basic and Grade and print total salary
4 */
5 import java.io.* ;
6 public class Question10
7 {
8 InputStreamReader isr = new InputStreamReader (System.in);
9 BufferedReader br = new BufferedReader(isr);
10 public void main(String[]Args) throws IOException
11 {
12 System.out.println("Enter the Basic salary") ;
13 int b = Integer.parseInt(br.readLine());
14 double da=0; double hra=0; double total=0;
15 if(b>=10000)
16 {
17 da=(0.4)*b;
18 hra=(0.3)*b;
19 }
20 else if (b>=5000)
21 {
22 da=(0.4)*b;
23 hra=(0.3)*b;
24 }
25 else
26 {
27 da=(0.2)*b;
28 hra=(0.1)*b;
29 }
30 total=da+hra+b;
31 System.out.println("The total salary is : " +total);
32 }
33 }
34

35

Nov 3, 2014 10:51:05 PM


Enter the Basic salary

20000
The total salary is : 34000.0

You might also like