-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcwh_35_Practice_set_on_methods.java
176 lines (154 loc) · 4.3 KB
/
cwh_35_Practice_set_on_methods.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package company;
public class cwh_35_Practice_set_on_methods
{
// Q1:
static void table(int x){
for(int i=1; i<=10; i++){
System.out.println(x+ " x " +i+ " = " +x*i);
}
}
// Q2:
static void pattern(){
for(int i=1; i<=4; i++){
for(int j=1; j<=i; j++){
System.out.print("* ");
}
System.out.println();
}
}
// Q3:
static int sumNatural(int x){
if(x==1){
return 1;
}
return x + sumNatural(x-1);
}
// Q4:
static void pattern2()
{
int n=4;
for(int i=1; i<=4; i++)
{
for(int j=1; j<=n; j++)
{
System.out.print("* ");
}
n--;
System.out.println();
}
}
// Q5:
static int fibNth(int a)
{
// if(a==1){
// return 0;
// }
// else if(a==2){
// return 1;
// }
// OR
if(a==1 || a==2)
{
return a-1;
} // In short for the above if-else.
else
{
return fibNth(a-1)+fibNth(a-2);
}
}
// Q6:
static int sumOfArguments(int ...s)
{
int sum=0;
for(int e: s)
{
sum += e;
}
return sum;
}
// Q7:
static void pattern2_rec(int d)
{
if(d>0)
{
for(int i=1; i<=d; i++)
{
System.out.print("* ");
}
System.out.println();
pattern2_rec(d-1);
}
}
// Q8:
static void pattern_rec(int d)
{
if(d>0)
{
pattern_rec(d-1);
for(int i=1; i<=d; i++)
{
System.out.print("* ");
}
System.out.println();
}
}
// Q9:
static float temp(int y)
{
return (((y*9/5f)+32));
}
// Q10:
static int sumNaturalIterative(int h)
{
int sum = 0;
for(int i=0; i<=h; i++)
{
sum += i;
}
return sum;
}
public static void main(String[] args)
{
// Question - 1 --> multiplication table of n using methods.
int n = 5;
System.out.println("\nThe multiplication table of " +n+ " is :");
table(n);
/* Question - 2 --> WAP to print the pattern:
*
* *
* * *
* * * * */
System.out.println("\nStar pattern using iteration:");
pattern();
// Question - 3 --> Write a recursive function to calculate the sum of first n natural numbers.
int x = 5;
System.out.println("\nThe sum of first " +x+ " natural numbers using recursion is = " +sumNatural(x));
/* Question - 4 --> Function to print the following pattern:
* * * *
* * *
* *
* */
System.out.println("\nStar pattern using iteration:");
pattern2();
// Question - 5 --> Function to print n'th term of fibbonacci series using recursion.
int n1=5;
System.out.println("\nThe " +n1+ "th term of fibbonacci series is = " +fibNth(n1));
// Question - 6 --> Function to find the average of the set of numbers passed as arguments.
int [] arr = {10, 200, 30, 4, 5, 6};
System.out.println("\nThe sum of the array is = " +sumOfArguments(arr));
// Question - 7 --> Repeat Q4 using recursion.
int b1=4;
System.out.println("\nStar pattern using recursion:");
pattern2_rec(b1);
// Question - 8 --> Repeat Q2 using recursion.
int b=4;
System.out.println("\nStar pattern using recursion:");
pattern_rec(b);
// Question - 9 --> Function to convert temp from degree celsius to fahrenheit.
int u=51;
System.out.println("\n" +u+ " degree C = " +temp(u)+ " degree F");
// Question - 10 --> Repeat Q3 using iterative approach.
int r=5;
System.out.println("\nThe sum of first " +r+ " natural numbers using iterative approach is = " +sumNaturalIterative(r)+ "\n");
}
}