Skip to content

Commit 056cce7

Browse files
Chapter 7 Practice Set Solutions
1 parent 95cc453 commit 056cce7

11 files changed

+127
-0
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class cwh_35_ps_pr_01 {
2+
static void multiplication(int n) {
3+
for (int i=1; i<=10; i++) {
4+
System.out.println(n + " * " + i + " = " + (n*i));
5+
}
6+
}
7+
public static void main(String args[]) {
8+
multiplication(9);
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class cwh_35_ps_pr_02 {
2+
static void pattern(int n) {
3+
for (int i = 0; i < n; i++) {
4+
for (int j = 0; j < i + 1; j++) {
5+
System.out.print("* ");
6+
}
7+
System.out.println();
8+
}
9+
}
10+
public static void main(String args[]) {
11+
pattern(4);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class cwh_35_ps_pr_03 {
2+
static int recursive_sum(int n) {
3+
if (n==1) {
4+
return 1;
5+
}
6+
else {
7+
return n + recursive_sum(n-1);
8+
}
9+
10+
}
11+
public static void main(String args[]) {
12+
System.out.println(recursive_sum(5));
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class cwh_35_ps_pr_04 {
2+
static void pattern(int n) {
3+
for (int i = n; i > 0; i--) {
4+
for (int j = 0; j < i; j++) {
5+
System.out.print("* ");
6+
}
7+
System.out.println();
8+
}
9+
}
10+
public static void main(String args[]) {
11+
pattern(5);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class cwh_35_ps_pr_05 {
2+
static int fibonacci(int n) {
3+
if ( n==1 || n == 2) {
4+
return (n-1);
5+
}
6+
else {
7+
return (fibonacci(n-1) + fibonacci(n-2));
8+
}
9+
}
10+
public static void main(String args[]) {
11+
System.out.println(fibonacci(5));
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class cwh_35_ps_pr_06 {
2+
static int avg(int ...arr) {
3+
int len = arr.length;
4+
int sum = 0;
5+
for (int element:arr) {
6+
sum += element;
7+
}
8+
int average = sum / len;
9+
return average;
10+
}
11+
public static void main(String args[]) {
12+
System.out.println(avg(1,2,3,4,5,34,564,234));
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class cwh_35_ps_pr_07 {
2+
static void pattern_rec(int n) {
3+
if (n > 0) {
4+
for (int i = 0; i < n; i++) {
5+
System.out.print("*");
6+
}
7+
System.out.println();
8+
pattern_rec(n - 1);
9+
}
10+
11+
}
12+
public static void main(String args[]) {
13+
pattern_rec(5);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class cwh_35_ps_pr_08 {
2+
static void pattern_rec(int n) {
3+
if (n > 0) {
4+
pattern_rec(n - 1);
5+
for (int i = 0; i < n; i++) {
6+
System.out.print("*");
7+
}
8+
System.out.println();
9+
}
10+
}
11+
public static void main(String args[]) {
12+
pattern_rec(5);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class cwh_35_ps_pr_09 {
2+
static float cel_to_feh(float n) {
3+
float temp = n * (9/5f) + 32;
4+
return temp;
5+
}
6+
public static void main(String args[]) {
7+
System.out.println(cel_to_feh(100.5f));
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class cwh_35_ps_pr_10 {
2+
static int sum(int n) {
3+
int sum = 0;
4+
for (int i=1; i<=n; i++) {
5+
sum += i;
6+
}
7+
return sum;
8+
}
9+
public static void main(String args[]) {
10+
System.out.println(sum(5));
11+
}
12+
}

0 commit comments

Comments
 (0)