-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRutvij Dashputre.txt
181 lines (135 loc) · 4.03 KB
/
Rutvij Dashputre.txt
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
177
178
179
180
181
Que 1: Write a program to merge two arrays of integers by reading one number at a time from each array
until one of the array is exhausted, and then concatenating the remaining numbers.
Program:
package Assignment3;
import java.io.*;
class GFG {
static void alternateMerge(int arr1[], int arr2[],
int n1, int n2, int arr3[])
{
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2)
{
arr3[k++] = arr1[i++];
arr3[k++] = arr2[j++];
}
while (i < n1)
arr3[k++] = arr1[i++];
while (j < n2)
arr3[k++] = arr2[j++];
}
public static void main(String args[])
{
int arr1[] = {1, 3, 5, 7, 9, 11};
int n1 = arr1.length;
int arr2[] = {2, 4, 6, 8};
int n2 = arr2.length;
int arr3[] = new int[n1+n2];
alternateMerge(arr1, arr2, n1, n2, arr3);
System.out.println("Array after merging");
for (int i = 0; i < n1 + n2; i++)
System.out.print( arr3[i] + " ");
}
}
Que 2:.Write a program which takes an array of integers and prints the running average of 3 consecutive
integers. In case the array has fewer than 3 integers, there should be no output.
Program:
package Assignment3;
class SumOfThreeConsecutiveNumbersInArray
{ public static void main(String s[])
{
int[] input = {2, 6, 3, 8, 1};
int[] output = sumOfThreeConsecutiveNumbers(input);
System.out.print("Output array is : ");
for(int i = 0; i < output.length; i++)
{
System.out.print(output[i] + ", ");
}
}
public static int[] sumOfThreeConsecutiveNumbers(int[] input)
{
int[] output = null;
return output;
}
}
Que 3: Write a program which generates the series 1,4,27,16,125,36
Program:
package Assignment3;
class GFG {
public static int logicOfSequence(int N)
{
if (N % 2 == 0)
N = N * N;
else
N = N * N * N;
return N;
}
public static void main(String args[])
{
int N = 6;
System.out.println(logicOfSequence(N));
}
}
Que 4:
Program to sort in in acending order:
package Assignment3;
import java.util.Arrays;
public class SortExample
{
public static void main(String[] args)
{
int[] arr = {5,14,35,90,139};
Arrays.sort(arr, 1, 5);
System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
}
Program to sort in in decending order:
package Assignment3;
import java.util.Arrays;
import java.util.Collections;
public class SortExample
{
public static void main(String[] args)
{
Integer[] arr = {5,14,35,90,139};
Arrays.sort(arr, Collections.reverseOrder());
System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
Que 5:Write a function which accepts an floating point number and counts the number of decimal
places in the number without using a string.
Program:
package Assignment3;
public class Third {
public static void main(String args[]){
double no =3.44;
int count =0;
while(no!=((int)no))
{
count++;
no=no*10;
}
System.out.println(count);
}
}
Que 6:Print the third-largest number in an array without sorting it
Program:
package Assignment3;
public class ThirdLargestNumberInAnArray {
public static void main(String args[]){
int temp, size;
int array[] = { 24,54,31,16,82,45,67};
size = array.length;
for(int i = 0; i<size; i++ ){
for(int j = i+1; j<size; j++){
if(array[i]>array[j]){
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
System.out.println("Third largest number is:: "+array[size-3]);
}
}