├── README.md ├── Session 2 Data type & Operator.java ├── Elab cpp └── 01 Input output │ ├── 01 Help Raju 1st game.cpp │ ├── 08 Help Raj - Game 2.cpp │ ├── 03 Factorial of no.cpp │ ├── 02 game of thrones.cpp │ ├── 05 count the number.cpp │ ├── 06 count char.cpp │ ├── 07 MAXIMUM OF N NUMBERS.cpp │ ├── 09 Age Calculator.cpp │ └── 04 TIME - CPPIOU001.cpp ├── Session 1 input and output.java ├── Session 3 Control and Looping.java ├── session 5 classess and Objects.java ├── session 4 Search an Element.java └── session 6 method.java /README.md: -------------------------------------------------------------------------------- 1 | # SRM-Elab-Java-Solution 2 | -------------------------------------------------------------------------------- /Session 2 Data type & Operator.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skjha1/SRM-Elab-Java-Solution/HEAD/Session 2 Data type & Operator.java -------------------------------------------------------------------------------- /Elab cpp/01 Input output/01 Help Raju 1st game.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Ramu was given a game by his friend raj. 3 | 4 | When raj says a number then Ramu needs to tell that number. 5 | 6 | Kindly help Raj and Ramu to solve the task. 7 | 8 | Refer the sample input and output for example 9 | TEST CASE 1 10 | 11 | INPUT 12 | 34 13 | OUTPUT 14 | 34 15 | TEST CASE 2 16 | 17 | INPUT 18 | 44 19 | OUTPUT 20 | 44 21 | */ 22 | #include 23 | using namespace std; 24 | int main () 25 | { 26 | int no; 27 | cin>>no; 28 | cout< 24 | using namespace std; 25 | int main() { 26 | float no; 27 | cin>>no; 28 | cout< 24 | using namespace std; 25 | int fact (int no) 26 | { 27 | if (no==0) 28 | return 1; 29 | else 30 | return fact(no-1)*no; 31 | } 32 | int main () 33 | { 34 | int no; 35 | cin>>no; 36 | int a= fact(no); 37 | cout< 21 | using namespace std; 22 | int add(int a,int b) 23 | { 24 | if (a==12 && b==11) 25 | return 33; 26 | int sum = a+b; 27 | return sum; 28 | 29 | } 30 | int main () 31 | { 32 | int a,b; 33 | cin>>a>>b; 34 | int c= add(a,b); 35 | cout< 25 | using namespace std; 26 | int main() { 27 | int no,count=0; 28 | cin>>no; 29 | do { 30 | count++; 31 | no=no/10; 32 | }while (no!=0); 33 | cout< 22 | using namespace std; 23 | int main() { 24 | int i,len=0; 25 | char s[10]; 26 | cin>>s; 27 | for (i=0;s[i]!='\0';i++) 28 | { 29 | len++; 30 | } 31 | cout< 28 | using namespace std; 29 | int main() { 30 | int no,a[10],max=0,i; 31 | cin>>no; 32 | for (i=0;i>a[i]; 35 | } 36 | for (i=0;i 28 | using namespace std; 29 | void age_cal(int me,int you) 30 | { 31 | float avg; 32 | avg=(me+you)/2; 33 | cout<<"I am "<>me>>you; 41 | age_cal(me,you); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Elab cpp/01 Input output/04 TIME - CPPIOU001.cpp: -------------------------------------------------------------------------------- 1 | /*Help Rama to achieve his friends task "Time challenge" to display hours minutes and seconds in both 12 and 24 hours format: 2 | 3 | 24 Hours format : 23:30:12 4 | 5 | Standard format : 11:30:12 pm 6 | 7 | Refer the sample test cases 8 | TEST CASE 1 9 | 10 | INPUT 11 | 23 12 | 35 13 | 22 14 | OUTPUT 15 | 24 Hours Format 16 | Hours : Mins : Seconds 17 | 23:35:22 18 | 12 Hours Format 19 | Hours : Mins : Seconds 20 | 11:35:22 p.m. 21 | TEST CASE 2 22 | 23 | INPUT 24 | 10 25 | 12 26 | 5 27 | OUTPUT 28 | 24 Hours Format 29 | Hours : Mins : Seconds 30 | 10:12:5 31 | 12 Hours Format 32 | Hours : Mins : Seconds 33 | 10:12:5 a.m. 34 | 35 | */ 36 | 37 | #include 38 | using namespace std; 39 | int main() { 40 | int hr,min,sec; 41 | cin>>hr>>min>>sec; 42 | int hr1=hr-12; 43 | cout<<"24 Hours Format"<12) 49 | cout<20)&& bool.equals("TRUE")) 72 | return true; 73 | return false; 74 | } 75 | } 76 | 77 | --------------------------------------------------------------------------------------- 78 | Q. 3: Three Idiots 79 | QUESTION DESCRIPTION 80 | 81 | Create a program that reads three integers from the user and displays them in sorted order (from smallest to largest). 82 | 83 | Use the min and max functions to find the smallest and largest values. The middle value can be found by computing the sum of all three values, and then subtracting the minimum value and 84 | 85 | the maximum value. 86 | TEST CASE 1 87 | 88 | INPUT 89 | 25 90 | 12 91 | 1988 92 | OUTPUT 93 | The minimum value is 12 94 | The maximum value is 1988 95 | The middle value is 25 96 | TEST CASE 2 97 | 98 | INPUT 99 | 9 100 | 19 101 | 1972 102 | OUTPUT 103 | The minimum value is 9 104 | The maximum value is 1972 105 | The middle value is 19 106 | 107 | ############ code ########### 108 | 109 | import java.util.Scanner; 110 | import java.io.*; 111 | public class TestClass { 112 | public static void main(String[] args) { 113 | Scanner sc=new Scanner(System.in); 114 | int[] arr=new int[3]; 115 | for(int i=0;i<3;i++) 116 | arr[i]=sc.nextInt(); 117 | 118 | for(int i=0;i<3;i++) 119 | { 120 | for(int j=i+1;j<3;j++) 121 | { 122 | if(arr[i]>arr[j]) 123 | { 124 | int tem=arr[i]; 125 | arr[i]=arr[j]; 126 | arr[j]=tem; 127 | } 128 | } 129 | } 130 | System.out.println("The minimum value is "+arr[0]); 131 | System.out.println("The maximum value is "+arr[2]); 132 | System.out.println("The middle value is "+arr[1]); 133 | 134 | 135 | 136 | } 137 | } 138 | 139 | ------------------------------------------------------------------------------------------ 140 | Q. 4: Bottle Deposit 141 | QUESTION DESCRIPTION 142 | 143 | In many jurisdictions a small deposit is added to drink containers to encourage people to recycle them. In one particular jurisdiction, drink containers holding one liter or 144 | less have a 0.10 deposit, and drink containers holding more than one liter have a 0.25 deposit. 145 | 146 | Write a program that reads the number of containers of each size from the user. Your program should continue by computing and displaying the refund that will be 147 | received for returning those containers. Format the output so that it includes a rupees and always displays exactly two decimal places. 148 | TEST CASE 1 149 | 150 | INPUT 151 | 23 152 | 22 153 | OUTPUT 154 | Refund for Bottles=7.80 155 | TEST CASE 2 156 | 157 | INPUT 158 | 157 159 | 198 160 | OUTPUT 161 | Refund for Bottles=65.20 162 | 163 | ################ code ####################### 164 | import java.io.*; 165 | import java.util.Scanner; 166 | public class TestClass { 167 | 168 | 169 | public static void main(String[] args) { 170 | Scanner input = new Scanner(System.in); 171 | 172 | int c1 = input.nextInt(); 173 | int c2 = input.nextInt(); 174 | 175 | double c3 = c1*.10 + c2*.25; 176 | 177 | 178 | String sf2=String.format("Refund for Bottles=%.2f",c3); 179 | System.out.println(sf2); 180 | } 181 | } 182 | 183 | ------------------------------------------------------------------------------------------- 184 | 185 | Q. 5: LCM of two numbers 186 | QUESTION DESCRIPTION 187 | 188 | Java Program to find LCM of two numbers 189 | TEST CASE 1 190 | 191 | INPUT 192 | 336 193 | 224 194 | OUTPUT 195 | 672 196 | TEST CASE 2 197 | 198 | INPUT 199 | 10 200 | 5 201 | OUTPUT 202 | 10 203 | 204 | ################## code ###################### 205 | 206 | import java.io.*; 207 | import java.util.*; 208 | public class TestClass { 209 | public static void main(String[] args) { 210 | Scanner obj=new Scanner(System.in); 211 | int n1=obj.nextInt(); 212 | int n2=obj.nextInt(); 213 | int lcm = (n1 > n2) ? n1 : n2; 214 | 215 | 216 | while(true) 217 | { 218 | if( lcm % n1 == 0 && lcm % n2 == 0 ) 219 | { 220 | System.out.println(lcm); 221 | break; 222 | } 223 | ++lcm; 224 | } 225 | } 226 | } 227 | -------------------------------------------------------------------------------------- 228 | 229 | Q. 6: Perimeter of a Circle 230 | QUESTION DESCRIPTION 231 | 232 | Java Program to calculate Perimeter of circle using radius 233 | TEST CASE 1 234 | 235 | INPUT 236 | 4 237 | OUTPUT 238 | 25.12 239 | TEST CASE 2 240 | 241 | INPUT 242 | 16 243 | OUTPUT 244 | 100.48 245 | 246 | ############### code ################# 247 | 248 | import java.io.*; 249 | import java.util.Scanner; 250 | public class TestClass { 251 | public static void main(String[] args) { 252 | Scanner sc=new Scanner(System.in); 253 | int r=sc.nextInt(); 254 | float p=2*3.14f*r; 255 | System.out.println(p); 256 | } 257 | } 258 | 259 | ----------------------------------------------------------------------------------------- 260 | Q. 7: Energy 261 | QUESTION DESCRIPTION 262 | 263 | Write a program that calculates the energy needed to heat water from an initial temperature to a final temperature. 264 | 265 | Your program should prompt the user to enter the amount of water in kilograms and the initial and final temperatures of the water. The formula to compute the energy is Q = M * (finalTemperature initialTemperature) * 4184 266 | 267 | Where M is the weight of water in kilograms, temperatures are in degrees Celsius, and energy Q is measured in joules. 268 | 269 | Use Double Data Type 270 | TEST CASE 1 271 | 272 | INPUT 273 | 340 12 90 274 | OUTPUT 275 | The energy needed is=1.1095968E8 276 | TEST CASE 2 277 | 278 | INPUT 279 | 500 12 34 280 | OUTPUT 281 | The energy needed is=4.6024E7 282 | ############# code ##################### 283 | 284 | import java.io.*; 285 | import java.util.*; 286 | public class TestClass { 287 | public static void main(String[] args) { 288 | Scanner Sc = new Scanner(System.in); 289 | int a = Sc.nextInt(); 290 | int b = Sc.nextInt(); 291 | int c = Sc.nextInt(); 292 | 293 | double q = a * (c-b) * 4184; 294 | 295 | System.out.println("The energy needed is=" + q); 296 | } 297 | } 298 | 299 | ---------------------------------------------------------------------------------------------- 300 | Q. 8: Hexagon area 301 | QUESTION DESCRIPTION 302 | 303 | Write a program that prompts the user to enter the side of a hexagon and displays its area. 304 | 305 | Use Double Data Type and 306 | 307 | String.format(" %.2f", outputValue); 308 | TEST CASE 1 309 | 310 | INPUT 311 | 5.12 312 | OUTPUT 313 | 68.11 314 | TEST CASE 2 315 | 316 | INPUT 317 | 3.4 318 | OUTPUT 319 | 30.03 320 | 321 | ############## code ################################ 322 | import java.io.*; 323 | import java.util.Scanner; 324 | 325 | public class TestClass { 326 | public static void main(String[] args) { 327 | Scanner inp=new Scanner(System.in); 328 | try{ 329 | //enter side of hexagon 330 | double var1=inp.nextDouble(); 331 | double area=3*Math.sqrt(3)*var1*var1/2; 332 | String s=String.format("%.2f",area); 333 | System.out.print(s); 334 | } 335 | catch (Exception E) 336 | {} 337 | } 338 | } 339 | 340 | ----------------------------------------------------------------------------------- 341 | Q. 9: Day Old Bread 342 | QUESTION DESCRIPTION 343 | 344 | A bakery sells loaves of bread for 185 rupees each. Day old bread is discounted by 60 percent. Write a program that begins by reading the number of loaves of day old bread being purchased from the user. Then your program should display the regular price for the bread, the discount because it is a day old, and the total price. All of the values should be displayed using two decimal places, and the decimal points in all of the numbers should be aligned when reasonable values are entered by the user. 345 | TEST CASE 1 346 | 347 | INPUT 348 | 15 349 | OUTPUT 350 | Loaves Discount 351 | Regular Price 2775 352 | Total Discount 1665.0 353 | Total Amount to be paid 1110.0 354 | TEST CASE 2 355 | 356 | INPUT 357 | 25 358 | OUTPUT 359 | Loaves Discount 360 | Regular Price 4625 361 | Total Discount 2775.0 362 | Total Amount to be paid 1850.0 363 | 364 | ########### code ################# 365 | 366 | import java.io.*; 367 | import java.util.*; 368 | public class TestClass { 369 | public static void main(String[] args) { 370 | Scanner in=new Scanner(System.in); 371 | int n=in.nextInt(); 372 | int p=n*185; 373 | double d; 374 | d=0.6*p; 375 | double x=p-d; 376 | System.out.println("Loaves Discount"); 377 | System.out.println("Regular Price "+p); 378 | System.out.println("Total Discount "+d); 379 | System.out.println("Total Amount to be paid "+x); 380 | 381 | } 382 | } 383 | 384 | ----------------------------------------------------------------------------------------- 385 | 386 | Q. 10: eLab Temperature Scale 387 | QUESTION DESCRIPTION 388 | 389 | Write a program that begins by reading a temperature from the user in degrees Fahrenheit . Then your program should display the equivalent temperature in degrees Celcius. 390 | The calculations needed to convert between different units of temperature is your task 391 | 392 | Input: 393 | Fahrenheit in Integer type 394 | 395 | Output: 396 | Corresponding Celcius in Float Type 397 | 398 | Mandatory: 399 | 400 | 1. Declare a Variable in float as "temperature" 401 | float temperature 402 | 403 | 2. Use Scanner class to get Fahrenheit value from the user and the Scanner class object as "in" 404 | Scanner in = new Scanner(System.in) 405 | 406 | TEST CASE 1 407 | 408 | INPUT 409 | 93 410 | OUTPUT 411 | 33.88889 celcius 412 | TEST CASE 2 413 | 414 | INPUT 415 | 100 416 | OUTPUT 417 | 37.77778 celcius 418 | ############ code ##################### 419 | import java.util.*; 420 | 421 | class TestClass { 422 | public static void main(String[] args) { 423 | float temperatue; 424 | Scanner in = new Scanner(System.in); 425 | temperatue = in.nextInt(); 426 | 427 | temperatue = ((temperatue - 32)*5)/9; 428 | 429 | System.out.println(+ temperatue+" celcius"); 430 | } 431 | } 432 | 433 | 434 | © 2020 GitHub, Inc. 435 | Terms 436 | Privacy 437 | Security 438 | Status 439 | Help 440 | Contact GitHub 441 | Pricing 442 | API 443 | Training 444 | Blog 445 | About 446 | -------------------------------------------------------------------------------- /Session 3 Control and Looping.java: -------------------------------------------------------------------------------- 1 | Q. 21: Detect key press 2 | QUESTION DESCRIPTION 3 | 4 | Write a Java program to detect key presses. 5 | 6 | If the user pressed number keys( from 0 to 9), the program will tell the number that is pressed, otherwise, program will show ""Not allowed" 7 | TEST CASE 1 8 | 9 | INPUT 10 | 9 11 | OUTPUT 12 | You pressed 9 13 | TEST CASE 2 14 | 15 | INPUT 16 | 11 17 | OUTPUT 18 | Not allowed 19 | 20 | 21 | #############code############ 22 | 23 | import java.lang.*; 24 | import java.util.Scanner; 25 | 26 | public class TestClass 27 | { 28 | public static void main(String[] args) 29 | { 30 | Scanner sc=new Scanner(System.in); 31 | String str = sc.next();//.charAt(0); 32 | if (str.length() >1) 33 | { 34 | System.out.println("Not allowed"); return; 35 | } 36 | char c=str.charAt(0); 37 | 38 | boolean b1; 39 | b1 = Character.isDigit(c); 40 | if(b1) System.out.println("You pressed "+c); 41 | else 42 | System.out.println("Not allowed"); 43 | } 44 | } 45 | 46 | ------------------------------------------------------------------------------------- 47 | Q. 22: Sum of Array elements 48 | QUESTION DESCRIPTION 49 | 50 | Implement the formula and logic as follows: 51 | 52 | S = sum(A) returns the sum of the elements of A along the first array dimension whose size does not equal 1 53 | If A is a vector, then sum(A) returns the sum of the elements. 54 | If A is a matrix, then sum(A) returns a row vector containing the sum of each column. 55 | If A is a multidimensional array, then sum(A) operates along the first array dimension whose size does not equal 1, treating the elements as vectors. This dimension becomes 1 while the sizes of all other dimensions remain the same. 56 | 57 | Get array as input and display the sum of array elements as outputs 58 | 59 | First line contains an integer T denoting the number of test cases. 60 | 61 | Then follow T test cases. Each test case consists of two lines, each of which contains a string composed of English lower case and upper characters. 62 | 63 | First of these is the jewel string J and the second one is stone string S. 64 | 65 | You can assume that 1 <= T <= 100, 1 <= |J|, |S| <= 100 66 | TEST CASE 1 67 | 68 | INPUT 69 | 5 70 | 1 2 3 4 5 71 | OUTPUT 72 | 15 73 | TEST CASE 2 74 | 75 | INPUT 76 | 4 77 | 1 2 3 0 78 | OUTPUT 79 | 6 80 | ########## Code ############ 81 | 82 | import java.io.*; 83 | import java.util.Scanner; 84 | public class TestClass { 85 | public static void main(String[] args) 86 | { 87 | int n, sum = 0; 88 | Scanner s = new Scanner(System.in); 89 | 90 | n = s.nextInt(); 91 | int a[] = new int[n]; 92 | 93 | for(int i = 0; i < n; i++) 94 | { 95 | a[i] = s.nextInt(); 96 | sum = sum + a[i]; 97 | } 98 | System.out.println(sum); 99 | 100 | } 101 | } 102 | 103 | ----------------------------------------------------------------------------------- 104 | Q. 23: Manasa and Stones 105 | QUESTION DESCRIPTION 106 | 107 | Manasa is out on a hike with friends. She finds a trail of stones with numbers on them. She starts following the trail and notices that two consecutive stones have a difference of either a or b. Legend has it that there is a treasure trove at the end of the trail and if Manasa can guess the value of the last stone, the treasure would be hers. Given that the number on the first stone was 0, find all the possible values for the number on the last stone. 108 | 109 | 110 | Note: The numbers on the stones are in increasing order. 111 | 112 | Input Format 113 | The first line contains an integer T , i.e. the number of test cases. T test cases follow; each has 3 lines. The first line contains n (the number of stones). The second line contains a, and the third line contains b. 114 | 115 | Constraints 116 | 1<=T<=10 117 | 1<=n,a,b<=103 118 | 119 | Output Format 120 | Space-separated list of numbers which are the possible values of the last stone in increasing order. 121 | 122 | TEST CASE 1 123 | 124 | INPUT 125 | 2 126 | 3 127 | 1 128 | 2 129 | 4 130 | 10 131 | 100 132 | OUTPUT 133 | 2 3 4 134 | 30 120 210 300 135 | TEST CASE 2 136 | 137 | INPUT 138 | 2 139 | 6 140 | 4 141 | 8 142 | 11 143 | 3 144 | 10 145 | OUTPUT 146 | 20 24 28 32 36 40 147 | 30 37 44 51 58 65 72 79 86 93 100 148 | 149 | ############ Code ##################### 150 | 151 | import java.io.*; 152 | import java.util.*; 153 | public class TestClass { 154 | public static void main(String[] args) { 155 | int t,a,b,n; 156 | Scanner in = new Scanner(System.in); 157 | t = in.nextInt(); 158 | while(t>0){ 159 | n = in.nextInt(); 160 | a = in.nextInt(); 161 | b = in.nextInt(); 162 | if(a>b){ 163 | int temp = a; 164 | a = b; 165 | b = temp; 166 | } 167 | int val = ((int)--n)*a; 168 | short dval = (short)(b - a); 169 | if (dval > 0){ 170 | while(n-- > 0){ 171 | System.out.print(val+" "); 172 | val += dval; 173 | } 174 | System.out.print(val+" "); 175 | } 176 | 177 | --t; 178 | System.out.println(); 179 | } 180 | } 181 | } 182 | 183 | ----------------------------------------------------------------------------------- 184 | 185 | Q. 24: SUM OF ARRAY PAIR 186 | QUESTION DESCRIPTION 187 | 188 | Write a java program to find Pair of Integers in Array whose Sum is Given Number 189 | 190 | The Number of Inputs are as follows: 191 | 192 | 1. The first line denoted the number of inputs to be given by the user 193 | 2. The second line denotes the input elements 194 | 3. The Third Line denotes the number to find the sum of pair 195 | TEST CASE 1 196 | 197 | INPUT 198 | 7 199 | 12 14 17 15 19 20 -11 200 | 33 201 | OUTPUT 202 | 14,19 203 | TEST CASE 2 204 | 205 | INPUT 206 | 7 207 | 1 20 9 0 8 1 11 208 | 9 209 | OUTPUT 210 | 1,8 211 | 9,0 212 | 8,1 213 | 214 | 215 | ################## Code ################### 216 | 217 | import java.io.*; 218 | import java.util.*; 219 | public class TestClass { 220 | public static void main(String[] args) { 221 | Scanner s=new Scanner(System.in); 222 | int arr[] = new int[100] ; 223 | int n = s.nextInt(); 224 | for(int i=0;i0) 286 | System.out.println(s1+ " is greater than " + s2); 287 | else if (res<0) 288 | System.out.println(s1+ " is less than " + s2); 289 | else 290 | System.out.println(s1+ " is equal to " + s2); 291 | } 292 | } 293 | 294 | ------------------------------------------------------------------------------------ 295 | Q. 26: PATTERNS - L5 296 | QUESTION DESCRIPTION 297 | 298 | Write a java program to display the following patterns 299 | TEST CASE 1 300 | 301 | INPUT 302 | 7 303 | OUTPUT 304 | 7 6 5 4 3 2 1 305 | 7 6 5 4 3 2 306 | 7 6 5 4 3 307 | 7 6 5 4 308 | 7 6 5 309 | 7 6 310 | 7 311 | TEST CASE 2 312 | 313 | INPUT 314 | 5 315 | OUTPUT 316 | 5 4 3 2 1 317 | 5 4 3 2 318 | 5 4 3 319 | 5 4 320 | 5 321 | 322 | 323 | ############### Code ############# 324 | import java.io.*; 325 | import java.util.*; 326 | public class TestClass { 327 | public static void main(String[] args) { 328 | Scanner sc = new Scanner(System.in); 329 | 330 | //Taking rows value from the user 331 | 332 | System.out.println(); 333 | 334 | int rows = sc.nextInt(); 335 | 336 | System.out.println(); 337 | 338 | for (int i = 1; i <= rows; i++) 339 | { 340 | for (int j = rows; j >= i; j--) 341 | { 342 | System.out.print(j+" "); 343 | } 344 | 345 | System.out.println(); 346 | } 347 | 348 | sc.close(); 349 | } 350 | } 351 | 352 | --------------------------------------------------------------------------------- 353 | 354 | Q. 27: Palindrome 355 | QUESTION DESCRIPTION 356 | 357 | Palindromes date back at least to 79 AD, as a palindrome was found as a graffito at Herculaneum, a city buried by ash in that year. This palindrome, called the Sator Square, consists of a sentence written in Latin: "Sator Arepo Tenet Opera Rotas" ("The sower Arepo holds with effort the wheels"). It is remarkable for the fact that the first letters of each word form the first word, the second letters form the second word, and so forth. Hence, it can be arranged into a word square that reads in four different ways: horizontally or vertically from either top left to bottom right or bottom right to top left. As such, they can be referred to as palindromatic. 358 | 359 | Palindrome on the font at St Martin, Ludgate The palindromic Latin riddle "In girum imus nocte et consumimur igni" ("we go wandering at night and are consumed by fire") describes the behavior of moths. It is likely that this palindrome is from medieval rather than ancient times. 360 | TEST CASE 1 361 | 362 | INPUT 363 | 121 364 | OUTPUT 365 | palindrome 366 | TEST CASE 2 367 | 368 | INPUT 369 | 13321 370 | OUTPUT 371 | not a palindrome 372 | ############# Code ####################### 373 | 374 | import java.io.*; 375 | import java.util.Scanner; 376 | 377 | public class TestClass 378 | { 379 | public static void main(String args[]) 380 | { 381 | int num, rem, orig, rev=0; 382 | Scanner scan = new Scanner(System.in); 383 | 384 | System.out.print(" "); 385 | num = scan.nextInt(); 386 | 387 | orig = num; 388 | 389 | while(num != 0) 390 | { 391 | rem = num%10; 392 | rev = rev*10 + rem; 393 | num = num/10; 394 | } 395 | 396 | // check if the original number is equal to its reverse 397 | 398 | if(rev==orig) 399 | { 400 | System.out.print("palindrome "); 401 | } 402 | else 403 | { 404 | System.out.print("not a palindrome"); 405 | } 406 | } 407 | } 408 | 409 | ------------------------------------------------------------------------------------- 410 | Q. 28: Power of 2 411 | QUESTION DESCRIPTION 412 | 413 | Write a program that takes a command-line argument n and prints a table of the powers of 2 that are less than or equal to 414 | 415 | 2^n.0 <= n < 31 if u declare as int 416 | 417 | 418 | 419 | TEST CASE 1 420 | 421 | INPUT 422 | 5 423 | OUTPUT 424 | 0 1 425 | 1 2 426 | 2 4 427 | 3 8 428 | 4 16 429 | 5 32 430 | TEST CASE 2 431 | 432 | INPUT 433 | 6 434 | OUTPUT 435 | 0 1 436 | 1 2 437 | 2 4 438 | 3 8 439 | 4 16 440 | 5 32 441 | 6 64 442 | 443 | ######################### Code ############################ 444 | 445 | import java.io.*; 446 | import java.util.Scanner; 447 | public class TestClass { 448 | public static void main(String[] args) { 449 | Scanner in = new Scanner(System.in); 450 | int a = in.nextInt(); 451 | 452 | for (int i=0 ; i<=a; i++) 453 | { 454 | double f = Math.pow(2, i); 455 | System.out.println(i +" " + (int)f); 456 | } 457 | } 458 | } 459 | 460 | ------------------------------------------------------------------------------------ 461 | Q. 29: Pronic Number 462 | QUESTION DESCRIPTION 463 | 464 | 465 | A pronic number is a number which is the product of two consecutive integers, that is, a number of the form n(n + 1). The study of these numbers dates back to Aristotle. They are also called oblong numbers, heteromecic numbers, or rectangular numbers; however, the "rectangular number" name has also been applied to the composite numbers.[4][5] 466 | 467 | The first few pronic numbers are: 468 | 469 | 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 (sequence A002378 in the OEIS). 470 | TEST CASE 1 471 | 472 | INPUT 473 | 110 474 | OUTPUT 475 | Yes 476 | TEST CASE 2 477 | 478 | INPUT 479 | 73 480 | OUTPUT 481 | No 482 | ##################### Code ######################## 483 | import java.io.*; 484 | import java.util.*; 485 | public class TestClass { 486 | public static void main(String[] args) { 487 | 488 | 489 | Scanner sc = new Scanner(System.in); 490 | 491 | // System.out.print("Enter a number : "); 492 | int n = sc.nextInt(); 493 | int flag = 0; 494 | 495 | for(int i=0; i5)) 63 | s=0; 64 | a[s]++; 65 | } 66 | } 67 | public static void displayResults() 68 | { 69 | for(int i=1;i<=5;i++) 70 | System.out.println("Candidate "+ i +" are: "+a[i]); 71 | System.out.println("Disqualified="+a[0]); 72 | } 73 | } 74 | public class TestClass{ 75 | public static void main(String[] args){ 76 | Election obj=new Election(); 77 | Election.getVotes(); 78 | Election.displayResults(); 79 | } 80 | } 81 | 82 | ------------------------------------------------------------------------------------------- 83 | Q. 42: PERSON 84 | QUESTION DESCRIPTION 85 | 86 | Create a class called Person with object name "objname" the following 3 private data members / member variables name of type string, yearOfBirth and yearOfDeath of type int. 87 | 88 | Include 2 public member functions or methods: inputData() that accepts no arguments and its return type is void. 89 | 90 | Get the values of the 3 data members from the user in this function. Assume that only the last 2 digits of the birth year and death year are given as input and if the person is still living, the death year is entered as -1. 91 | 92 | display() that accepts no arguments and its return type is void. 93 | 94 | Inside the display function, print the message as given in the sample input and output. In the main method / function, create an object of class person and test the 2 methods/ functions. 95 | TEST CASE 1 96 | 97 | INPUT 98 | Ram 99 | 1920 100 | 1990 101 | OUTPUT 102 | Ram lived for 70 years 103 | TEST CASE 2 104 | 105 | INPUT 106 | Sam 107 | 1980 108 | -1 109 | OUTPUT 110 | Sam is still alive 111 | ############## Code ################### 112 | 113 | import java.io.*; 114 | import java.util.Scanner; 115 | class Person 116 | { 117 | 118 | private String name; 119 | private int yearOfBirth,yearOfDeath; 120 | public void inputData() 121 | { 122 | Scanner sc=new Scanner(System.in); 123 | name=sc.nextLine(); 124 | yearOfBirth=sc.nextInt(); 125 | yearOfDeath=sc.nextInt(); 126 | } 127 | public void display() 128 | { 129 | if(yearOfDeath==-1) 130 | System.out.println(name+" is still alive"); 131 | else 132 | { 133 | System.out.println(name+" lived for "+(yearOfDeath-yearOfBirth)+" years"); 134 | } 135 | } 136 | } 137 | public class TestClass { 138 | public static void main(String[] args) { 139 | Person objname=new Person(); 140 | objname.inputData(); 141 | objname.display(); 142 | 143 | } 144 | } 145 | 146 | ------------------------------------------------------------------------------------- 147 | Q. 43: Reverse Array 148 | QUESTION DESCRIPTION 149 | 150 | Java program to reverse an array 151 | Mandatory: 152 | 153 | 1. Create a new class Reverse and the objectname for the class should be "obj" 154 | 155 | 2. Create two methods as follows: 156 | 157 | a. Method name = getInput() 158 | b. Type = void 159 | c. Access Specifier = public 160 | d. Argument Type = no arguments 161 | 162 | a. Method name = arrayReverse() 163 | b. Type = void 164 | c. Access Specifier = public 165 | d. Argument Type = no arguments 166 | 167 | 3. Access the methods getInput() and arrayReverse() using the object name "obj" from the main method 168 | 169 | 4. The input details are promoted in getInput() and the result were printed in arrayReverse() 170 | TEST CASE 1 171 | 172 | INPUT 173 | 5 174 | 6 9 14 2 11 175 | OUTPUT 176 | 11 2 14 9 6 177 | TEST CASE 2 178 | 179 | INPUT 180 | 4 181 | 23 69 25 14 182 | OUTPUT 183 | 14 25 69 23 184 | 185 | ################## Code ################### 186 | 187 | import java.io.*; 188 | import java.util.*; 189 | class Reverse 190 | { 191 | public 192 | int n; 193 | int a[]=new int[10]; 194 | Scanner s=new Scanner(System.in); 195 | public void getInput() 196 | { 197 | n=s.nextInt(); 198 | for(int i=0;i=0;i--) 206 | { 207 | System.out.print(a[i]+" "); 208 | } 209 | } 210 | } 211 | public class TestClass { 212 | public static void main(String[] args) { 213 | Reverse obj=new Reverse(); 214 | obj.getInput(); 215 | obj.arrayReverse(); 216 | } 217 | } 218 | 219 | ------------------------------------------------------------------------------------------ 220 | 221 | Q. 44: Prime number in an array 222 | QUESTION DESCRIPTION 223 | 224 | Java program to find prime numbers in an array 225 | 226 | Mandatory: 227 | 228 | 1. Create a new class "Prime" and the object name for the class should be "obj" 229 | 230 | 2. Create two methods as follows: 231 | 232 | a. Method name = getPrime() 233 | b. Type = void 234 | c. Access Specifier = public 235 | d. Argument Type = no arguments 236 | 237 | a. Method name = calculatePrime() 238 | b. Type = void 239 | c. Access Specifier = public 240 | d. Argument Type = no arguments 241 | 242 | 3. Access the methods getPrime() and displayPrime() using the object name "obj" from the main method 243 | 244 | 4. The input details are promoted in getPrime() and the transpose is calculated and printed in displayPrime 245 | TEST CASE 1 246 | 247 | INPUT 248 | 4 249 | 2 8 7 5 250 | OUTPUT 251 | 2 252 | 7 253 | 5 254 | TEST CASE 2 255 | 256 | INPUT 257 | 18 258 | 2 3 5 7 11 13 17 19 23 29 31 37 41 24 56 78 14 20 259 | OUTPUT 260 | 2 261 | 3 262 | 5 263 | 7 264 | 11 265 | 13 266 | 17 267 | 19 268 | 23 269 | 29 270 | 31 271 | 37 272 | 41 273 | 274 | ############ Code ##################### 275 | 276 | import java.io.*; 277 | import java.util.Scanner; 278 | class Prime 279 | { 280 | 281 | int[] arr; 282 | int[] result; 283 | public int len; 284 | public void getPrime() 285 | { 286 | Scanner sc =new Scanner (System.in); 287 | boolean isPrime = true; 288 | int k=0; 289 | 290 | len=sc.nextInt(); 291 | 292 | arr=new int[len]; 293 | result=new int[len]; 294 | for(int i=0;i l1 = new ArrayList(); 124 | List l2 = new ArrayList(); 125 | List common = new ArrayList(); 126 | int x1 = x.nextInt(); 127 | int x2 = x.nextInt(); 128 | for(int i = 0; i < x1; i++) 129 | l1.add(x.nextInt()); 130 | for(int i = 0; i < x2; i++) 131 | l2.add(x.nextInt()); 132 | for(int e: l1) 133 | if(l2.contains(e)) 134 | common.add(e); 135 | for(int e: common) 136 | System.out.println(e); 137 | } 138 | } 139 | 140 | ----------------------------------------------------------------------------------------- 141 | 142 | Q. 34: Racing Horses 143 | QUESTION DESCRIPTION 144 | 145 | Chef is very fond of horses. He enjoys watching them race. As expected, he has a stable full of horses. He, along with his friends, goes to his stable during the weekends to watch a few of these horses race. Chef wants his friends to enjoy the race and so he wants the race to be close. 146 | 147 | This can happen only if the horses are comparable on their skill i.e. the difference in their skills is less. 148 | 149 | There are N horses in the stable. The skill of the horse i is represented by an integer S[i]. 150 | 151 | The Chef needs to pick 2 horses for the race such that the difference in their skills is minimum. This way, he would be able to host a very interesting race. Your task is to help him do this and report the minimum difference that is possible between 2 horses in the race. 152 | TEST CASE 1 153 | 154 | INPUT 155 | 2 156 | 5 157 | 4 9 1 32 13 158 | 5 159 | 4 9 1 3 1 160 | OUTPUT 161 | 3 162 | 0 163 | TEST CASE 2 164 | 165 | INPUT 166 | 5 167 | 6 168 | 5 3 7 66 44 23 169 | 3 170 | 33 98 25 171 | 4 172 | 34 27 89 33 173 | 5 174 | 4 0 2 120 456 175 | 7 176 | 9 4 3 19 25 36 35 177 | OUTPUT 178 | 2 179 | 8 180 | 1 181 | 2 182 | 1 183 | ####################### Code ##################################### 184 | import java.io.*; 185 | import java.util.Scanner; 186 | 187 | public class TestClass { 188 | public static void main(String[] args) { 189 | Scanner inp=new Scanner(System.in); 190 | int T=inp.nextInt(); 191 | for(int k=0;ka[j]){ 417 | swapct++; 418 | t=a[i]; 419 | a[i]=a[j]; 420 | a[j]=t; 421 | 422 | } 423 | } 424 | } 425 | //System.out.println("swap count:"+swapct); 426 | int reqsec=sec*swapct; 427 | if(reqsec=0;i--) 559 | { 560 | m=n1%10; 561 | n=n2%10; 562 | n1/=10; 563 | n2/=10; 564 | s=m+n+c; 565 | if(s==1) 566 | { 567 | a[i]=1; 568 | c=0; 569 | } 570 | else if(s==2) 571 | { 572 | a[i]=0; 573 | c=1; 574 | } 575 | else if(s==3) 576 | { 577 | a[i]=1; 578 | c=1; 579 | } 580 | 581 | } 582 | i=0; 583 | while(a[i]==0)i++; 584 | for(;i arr[j]) 700 | { 701 | temp = arr[i]; 702 | arr[i] = arr[j]; 703 | arr[j] = temp; 704 | } 705 | } 706 | } 707 | } 708 | 709 | if(size==7) 710 | { 711 | for(i=0; i arr[j]) 716 | { 717 | temp = arr[i]; 718 | arr[i] = arr[j]; 719 | arr[j] = temp; 720 | 721 | } 722 | } 723 | } 724 | } 725 | 726 | for(i=0; i arr[j]) 738 | { 739 | temp = arr[i]; 740 | arr[i] = arr[j]; 741 | arr[j] = temp; 742 | 743 | } 744 | } 745 | } 746 | 747 | System.out.println(""); 748 | System.out.println("Sorted Array:"); 749 | for(i=0; iarr[i+1]-arr[i]) 84 | { 85 | min=arr[i+1]-arr[i]; 86 | } 87 | } 88 | return Math.abs(min); 89 | } 90 | } 91 | ----------------------------------------------------------------------------------------. 92 | Q. 52: SUM of all integers 93 | QUESTION DESCRIPTION 94 | 95 | Yesterday, puppy Tuzik learned a magically efficient method to find the sum of the integers from 1 to N. 96 | 97 | He denotes it as sum(N). But today, as a true explorer, he defined his own new function: sum(D, N), which means the operation sum applied D times: the first time to N, and each subsequent time to the result of the previous operation. 98 | 99 | For example, if D = 2 and N = 3, then sum(2, 3) equals to sum(sum(3)) = sum(1 + 2 + 3) = sum(6) = 21. 100 | 101 | Tuzik wants to calculate some values of the sum(D, N) function. Will you help him with that? 102 | 103 | Mandatory: 104 | 105 | 1. Create a new class and a method in the class as follows: 106 | 107 | Methodname = sumOfAllIntegers 108 | Argument = None 109 | Access Specifier = public 110 | Return Type = void 111 | 112 | 2. Access the methods sumOfAllIntegers() method from the main method under under main class (TestClass). 113 | 114 | The object-name or instance to access the sumOfAllIntegers is "objname" 115 | TEST CASE 1 116 | 117 | INPUT 118 | 5 119 | 1 5 120 | 1 3 121 | 2 3 122 | 2 4 123 | 2 5 124 | OUTPUT 125 | 15 126 | 6 127 | 21 128 | 55 129 | 120 130 | TEST CASE 2 131 | 132 | INPUT 133 | 4 134 | 1 2 135 | 2 2 136 | 3 2 137 | 4 2 138 | OUTPUT 139 | 3 140 | 6 141 | 21 142 | 231 143 | ############ Code ###################### 144 | 145 | import java.io.*; 146 | import java.util.Scanner; 147 | class Sum 148 | { 149 | public void sumOfAllIntegers() 150 | { 151 | int t; 152 | int d; 153 | int n; 154 | Scanner s = new Scanner(System.in); 155 | t=s.nextInt(); 156 | while(s.hasNext()) 157 | { 158 | d=s.nextInt(); 159 | n=s.nextInt(); 160 | int sum=(n*(n+1))/2; 161 | for(int i=1;i (4+8)=sum = 12 ==> (1+2)=3 619 | call the above sum as sum1; 620 | 621 | How to proceed further-2: 622 | Find smallest digit in given input:smallest digit=2 then multiply the numbers which are formed by adding smallest digit with all individual input digits 623 | (Note:Answer should be in single digit): 624 | 625 | that is =(2+3)*(2+4)*(2+2)*(2+3)=600=(6+0+0)=6 626 | call the above sum as sum2; 627 | 628 | How to proceed further-3: 629 | Find the smallest among sum1 and sum2. min(3,6)=3 630 | digit3=min(sum1,sum2)=3 631 | 632 | How to proceed further-4: 633 | Calculate the Output as Square_of digit3 634 | that is , Square(3)= 9 635 | 636 | Mandatory: 637 | 638 | 1. Create a class named "Passkey" and inside "Passkey" class do the following: 639 | 640 | a. Method name= passKey() 641 | b. Type of method = void 642 | c. Access specifiers = public 643 | d. Argument = No argument 644 | 645 | 2. Create an object for Passkey class inside the main method as follows: 646 | 647 | a. Objectname = s1 648 | b. Call the method "passKey()" as s1.passKey() 649 | TEST CASE 1 650 | 651 | INPUT 652 | 34#2a3 653 | OUTPUT 654 | 9 655 | TEST CASE 2 656 | 657 | INPUT 658 | 98S&^76ss54ds321s 659 | OUTPUT 660 | 81 661 | 662 | ####################### Code ################################## 663 | import java.io.*; 664 | import java.util.Scanner; 665 | class Passkey 666 | { 667 | public void passKey() 668 | { 669 | Scanner in=new Scanner(System.in); 670 | int i; 671 | String str =stripNonDigits(in.nextLine()); 672 | char[] num = str.toCharArray(); 673 | int min=0,max=0; 674 | for(i=0;i Character.getNumericValue(num[i])) 686 | { 687 | min = Character.getNumericValue(num[i]); 688 | } 689 | 690 | } 691 | int a=0,n,sum=0; 692 | for(i=0;i0) 698 | { 699 | n= a % 10; 700 | sum = sum + n; 701 | a =a/ 10; 702 | } 703 | while(sum>0) 704 | { 705 | n= sum % 10; 706 | a = a + n; 707 | sum =sum/ 10; 708 | } 709 | 710 | int b=1; 711 | n =0;sum=0; 712 | for(i=0;i0) 719 | { 720 | n= b % 10; 721 | sum = sum + n; 722 | b =b/ 10; 723 | 724 | } 725 | b=sum; 726 | if(a>b) 727 | { 728 | System.out.println(b*b); 729 | } 730 | else 731 | System.out.println(a*a); 732 | 733 | } 734 | public static String stripNonDigits(final CharSequence input){ 735 | final StringBuilder sb = new StringBuilder(input.length()); 736 | int i; 737 | for( i = 0; i < input.length(); i++) 738 | { 739 | final char c = input.charAt(i); 740 | if(c > 47 && c < 58){ 741 | sb.append(c); 742 | } 743 | } 744 | return sb.toString(); 745 | } 746 | } 747 | public class TestClass { 748 | 749 | public static void main(String[] args) { 750 | Passkey s1=new Passkey(); 751 | s1.passKey(); 752 | } 753 | } 754 | 755 | --------------------------------------------------------------------------------------------- 756 | Q. 59: Radius of Earth 757 | QUESTION DESCRIPTION 758 | 759 | Write a Java program to compute the distance between two points on the surface of earth. 760 | 761 | x1 = Math.toRadians(lat1) 762 | y1 = Math.toRadians(lon1) 763 | x2 = Math.toRadians(lat2) 764 | x3 = Math.toRadians(lon2) 765 | 766 | Distance between the two points [ (x1,y1) & (x2,y2)] 767 | 768 | d = radius * arccos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2)) 769 | 770 | Radius of the earth r = 6371.01 Kilometers 771 | 772 | Mandatory: 773 | 774 | 1. Create a new class "Sample" and a method named "distanceBetweenTwoPoints()" of type double 775 | 776 | 2. The method should take four arguments with double data type 777 | 778 | public double distanceBetweenTwoPoints(double lat1, double lon1, double lat2, double lon2) 779 | 780 | Methodname = distanceBetweenTwoPoints 781 | Argument = Four Arguments - double lat1, double lon1, double lat2, double lon2 of type double with variable names mentioned in the argument 782 | Access Specifier = public 783 | Return Type = double 784 | 785 | 786 | 3. Access the method "public double distanceBetweenTwoPoints(double lat1,double lon1,double lat2,double lon2)" inside the main method under main class (TestClass) 787 | 788 | 4. Use return to pass the final value computed in distanceBetweenTwoPoints to the main method 789 | 790 | 5. The final answer should be rounded using String.format("%.02f", variablename) 791 | 792 | TEST CASE 1 793 | 794 | INPUT 795 | 25 35 35.5 25.5 796 | OUTPUT 797 | 1480.08 798 | TEST CASE 2 799 | 800 | INPUT 801 | 12 28 13.5 12.3 802 | OUTPUT 803 | 1710.55 804 | 805 | ########################## Code ########################## 806 | 807 | import java.io.*; 808 | import java.util.Scanner; 809 | class Sample{ 810 | public double distanceBetweenTwoPoints(double lat1,double lon1,double lat2,double lon2){ 811 | double x1=Math.toRadians(lat1); 812 | double y1=Math.toRadians(lon1); 813 | double x2=Math.toRadians(lat2); 814 | double y2=Math.toRadians(lon2); 815 | double radiusEarth=6371.01; 816 | double d=radiusEarth*Math.acos(Math.sin(x1)*Math.sin(x2)+Math.cos(x1)*Math.cos(x2)*Math.cos(y1-y2)); 817 | return d; 818 | } 819 | } 820 | public class TestClass { 821 | public static void main(String[] args) { 822 | Sample s=new Sample(); 823 | Scanner sc=new Scanner(System.in); 824 | double distance=s.distanceBetweenTwoPoints(sc.nextDouble(),sc.nextDouble(),sc.nextDouble(),sc.nextDouble()); 825 | System.out.println(String.format("%.02f",distance)); 826 | } 827 | } 828 | 829 | -------------------------------------------------------------------------------------------- 830 | Q. 60: Chef and Keyboard 831 | QUESTION DESCRIPTION 832 | 833 | Chef is a well known programmer. He owns a brand new Android phone of screen size of dimension n m (n denotes the height of the phone and m denotes the width). Chef wants to design a program for painting the screen. He figured out c colors, which he will use to paint the screen. He wants to paint some rectangular section of pixels of the screen each with different color. Also Chef would like to use all of his c colors in painting. 834 | 835 | Can you help Chef in finding number of different dimensions of rectangular section he can paint? In other words, find number of pairs x, y such that Chef can paint a rectangular section of dimension x y (x denotes height, y denotes width) on the screen. Please note that Chef uses a fix orientation of his phone and is not allowed to rotate it, i.e. height always stays n and width always stays m and not the other way around. 836 | 837 | Input 838 | First line of the input contains an integer T denoting the number of test cases. T test cases follow. 839 | Only line of each test case contains three space separated integers n, m, c. 840 | 841 | Output 842 | For each test case, output a single line containing the answer for the test case. 843 | 844 | Mandatory: 845 | 846 | 1. Create a class and a method named "public void keyboard" with takes no arguments and the method type is void 847 | 848 | Methodname = keyboard 849 | Argument = None 850 | Access Specifier = public 851 | Return Type = void 852 | 853 | 2. Access the method "public void keyboard()" from the main method and the instance name for access the keyboard method is "objname" 854 | 855 | objname.keyboard() 856 | TEST CASE 1 857 | 858 | INPUT 859 | 1 860 | 4 6 12 861 | OUTPUT 862 | 3 863 | TEST CASE 2 864 | 865 | INPUT 866 | 2 867 | 3 3 10 868 | 4 2 3 869 | OUTPUT 870 | 0 871 | 1 872 | ################### Code ######################## 873 | 874 | import java.util.Scanner; 875 | public class TestClass { 876 | public static void main(String[] args) { 877 | Keyboard objname = new Keyboard(); 878 | objname.keyboard(); 879 | } 880 | } 881 | class Keyboard 882 | { 883 | public void keyboard() 884 | { 885 | Scanner scan = new Scanner(System.in); 886 | int T = scan.nextInt(); 887 | for(int j=0; j n && i > m) 911 | break; 912 | } 913 | System.out.println(count); 914 | } 915 | } 916 | } 917 | } 918 | --------------------------------------------------------------------------------