0% found this document useful (0 votes)
72 views7 pages

Exercise PDF

The document contains 4 Java exercises that demonstrate various programming concepts like: 1. Printing output to the console 2. Taking user input using the Scanner class 3. Performing calculations on input values and displaying output 4. Defining and calling methods to encapsulate code functionality The exercises get progressively more advanced - starting with simple print statements and increasing in complexity to include arrays, loops, methods and mathematical operations on input. Overall the exercises showcase basic Java programming fundamentals.
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)
72 views7 pages

Exercise PDF

The document contains 4 Java exercises that demonstrate various programming concepts like: 1. Printing output to the console 2. Taking user input using the Scanner class 3. Performing calculations on input values and displaying output 4. Defining and calling methods to encapsulate code functionality The exercises get progressively more advanced - starting with simple print statements and increasing in complexity to include arrays, loops, methods and mathematical operations on input. Overall the exercises showcase basic Java programming fundamentals.
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/ 7

Exercise1

1. import java.util.Scanner;
2.
3. public class Main
4. {
5.
6. public static void main(String[] args) {
7.
8.
9.
10. System.out.println(" ‫ اسمي هو شروق‬my name is shouroj");
11.
12.
13.
14.
15. }
16. }
Exercise 2

1. import java.util.Scanner;
2.
3. public class Main
4. {
5.
6. public static void main(String[] args) {
7.
8.
9. Scanner input = new Scanner (System.in);
10. System.out.print("Input your name: ");
11. String name = input.next();
12. System.out.println("Hello \n"+name);
13.
14.
15.
16.
17. Scanner in = new Scanner(System.in);
18. System.out.print("Input number: ");
19. double val = in.nextDouble();
20. System.out.printf("Square = : "+ val * val);
21.
22. }
23. }
Exercise 3

1. import java.util.Scanner;
2.
3. public class Main
4. {
5. static int num1;
6. static int num2;
7. static int arraysize;
8. public static void main(String[] args) {
9.
10.
11. Scanner input = new Scanner (System.in);
12. System.out.print("Input the first number: ");
13. num1 = input.nextInt();
14. System.out.print("Input the second number: ");
15. num2 = input.nextInt();
16. int sum = num1 + num2;
17. System.out.println();
18. System.out.println("Sum: "+sum);
19.
20.
21. maximum();
22.
23. System.out.println("give a number to be the size of array");
24.
25. arraysize = input.nextInt();
26.
27.
28.
29.
30.
31. int array[] = new int[arraysize];
32.
33. // System.out.println("Enter the " + a + " numbers now.");
34. System.out.println("enter element for the array");
35.
36. for (int i = 0 ; i < array.length; i++ ) {
37. array[i] = input.nextInt();
38. }
39.
40. //you notice that now the elements have been stored in the array ..
array[]
41.
42. System.out.println("These are the numbers you have entered.");
43. printArray(array);
44.
45.
46. System.out.println("");
47.
48.
49.
50.
51.
52.
53. int sumarr = 0;
54. //Advanced for loop
55. for( int num : array) {
56. sumarr = sumarr+num;
57. }
58. System.out.println("Sum of array elements is:"+sumarr);
59.
60.
61.
62.
63.
64. int arrayavg = 0;
65. //Advanced for loop
66. for( int num6 : array) {
67. arrayavg = sumarr/array.length;
68. }
69. System.out.println("average of array elements is:"+arrayavg);
70.
71.
72.
73. int mul = 1;
74. //Advanced for loop
75. for( int i=0;i< arraysize ;i++) {
76. mul = mul*array[i];
77. }
78. System.out.println("multiplication of array elements is:"+mul);
79.
80.
81. }
82.
83. public static int maximum (){
84.
85. if(num1>num2)
86. {
87. System.out.println("the maximum is: "+num1);
88.
89. }
90. else if(num1<num2)
91. {
92. System.out.println("the maximum is: "+num2);
93.
94.
95. }
96.
97.
98. else if(num1==num2)
99. {
100.
101. System.out.println("no maximum" );
102. }
103.
104.
105. return 0;
106.
107. }
108.
109.
110.
111.
112. public static void printArray(int arr[]){
113.
114. int n = arr.length;
115.
116. for (int i = 0; i < n; i++) {
117. System.out.print(arr[i] + " ");
118. }
119. }
120.
121.
122.
123.
124.
125.
126.
127. }
Exercise 4

1. import java.util.Scanner;
2.
3. public class Main
4. {
5.
6. public static void main(String[] args) {
7.
8. predecessor();
9.
10. }
11. static int predecessor ()
12.
13. {
14.
15. Scanner in=new Scanner(System.in);
16.
17.
18. System.out.println("please enter number" );
19.
20. int x=in.nextInt();
21.
22. System.out.println("the predecessor of "+x );
23. System.out.println( x-1 );
24.
25. return 0; }
26.
27.
28. }

You might also like