└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Question 1 2 | 3 | ### **Question:** 4 | 5 | > ***Write a program to print Hello World!.*** 6 | 7 | --------------------------------------- 8 | 9 | Solution: 10 | 11 | ```java language 12 | public class MyClass { 13 | public static void main(String[] args) { 14 | System.out.print("Hello,world!"); 15 | } 16 | } 17 | ``` 18 | ---------------------------------------- 19 | 20 | 21 | # Question 2 22 | 23 | ### **Question:** 24 | 25 | > ***Write a program to compute the perimeter and area of a rectangle.*** 26 | 27 | --------------------------------------- 28 | 29 | Solution: 30 | 31 | ```java language 32 | public class MyClass { 33 | public static void main(String[] args) { 34 | int height = 8; 35 | int width = 5; 36 | int perimeter = 2 * (height + width); 37 | System.out.println("Perimeter of the rectangle is: " + perimeter + " cm"); 38 | int area = height * width; 39 | System.out.println("Area of the rectangle is: " + area + " square cm"); 40 | } 41 | } 42 | ``` 43 | ---------------------------------------- 44 | 45 | 46 | # Question 3 47 | 48 | ### **Question:** 49 | 50 | > ***Write a program to compute the perimeter and area of a circle.*** 51 | 52 | --------------------------------------- 53 | 54 | Solution: 55 | 56 | ```java language 57 | public class MyClass { 58 | public static void main(String[] args) { 59 | int radius = 4; 60 | float perimeter = (float)(2 * 3.14 * radius); 61 | System.out.printf("Perimeter of the circle is: %f cm\n", perimeter); 62 | float area = (float)(3.14 * radius * radius); 63 | System.out.printf("Area of the circle is: %f square cm\n", area); 64 | } 65 | } 66 | ``` 67 | ---------------------------------------- 68 | 69 | 70 | # Question 4 71 | 72 | ### **Question:** 73 | 74 | > ***Write a program that accepts two numbers from the user and calculate the sum of the two numbers.*** 75 | 76 | --------------------------------------- 77 | 78 | Solution: 79 | 80 | ```java language 81 | import java.util.Scanner; 82 | 83 | public class MyClass { 84 | public static void main(String[] args) { 85 | int a, b, sum; 86 | System.out.print("\nEnter the first number: "); 87 | a = STDIN_SCANNER.nextInt(); 88 | System.out.print("\nEnter the second number: "); 89 | b = STDIN_SCANNER.nextInt(); 90 | sum = a + b; 91 | System.out.print("\nSum of the above two numbers is: " + sum); 92 | } 93 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 94 | } 95 | ``` 96 | ---------------------------------------- 97 | 98 | 99 | # Question 5 100 | 101 | ### **Question:** 102 | 103 | > ***Write a program that accepts two numbers from the user and calculate the product of the two numbers.*** 104 | 105 | --------------------------------------- 106 | 107 | Solution: 108 | 109 | ```java language 110 | import java.util.Scanner; 111 | 112 | public class MyClass { 113 | public static void main(String[] args) { 114 | int a, b, mult; 115 | System.out.print("\nEnter the first number: "); 116 | a = STDIN_SCANNER.nextInt(); 117 | System.out.print("\nEnter the second number: "); 118 | b = STDIN_SCANNER.nextInt(); 119 | mult = a * b; 120 | System.out.print("\nProduct of the above two numbers is: " + mult); 121 | } 122 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 123 | } 124 | ``` 125 | ---------------------------------------- 126 | 127 | 128 | # Question 6 129 | 130 | ### **Question:** 131 | 132 | > ***Write a program that accepts three numbers and find the largest of three.*** 133 | 134 | --------------------------------------- 135 | 136 | Solution: 137 | 138 | ```java language 139 | import java.util.Scanner; 140 | 141 | public class MyClass { 142 | public static void main(String[] args) { 143 | int x, y, z; 144 | System.out.print("\nEnter the first number: "); 145 | x = STDIN_SCANNER.nextInt(); 146 | System.out.print("\nEnter the second number: "); 147 | y = STDIN_SCANNER.nextInt(); 148 | System.out.print("\nEnter the third number: "); 149 | z = STDIN_SCANNER.nextInt(); 150 | 151 | // if x is greater than both y and z, x is the largest 152 | if(x >= y && x >= z) { 153 | System.out.print("\n" + x + " is the largest number."); 154 | } 155 | 156 | // if y is greater than both x and z, y is the largest 157 | if(y >= x && y >= z) { 158 | System.out.print("\n" + y + " is the largest number."); 159 | } 160 | 161 | // if z is greater than both x and y, z is the largest 162 | if(z >= x && z >= y) { 163 | System.out.print("\n" + z + " is the largest number."); 164 | } 165 | } 166 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 167 | } 168 | 169 | ``` 170 | ---------------------------------------- 171 | 172 | # Question 7 173 | 174 | ### **Question:** 175 | 176 | > ***Write a program that reads three floating values and check if it is possible to make a triangle with them. Also calculate the perimeter of the triangle if the entered values are valid.*** 177 | 178 | --------------------------------------- 179 | 180 | Solution: 181 | 182 | ```java language 183 | 184 | import java.util.Scanner; 185 | 186 | public class MyClass { 187 | public static void main(String[] args) { 188 | float x, y, z; 189 | System.out.print("\nEnter the first number: "); 190 | x = STDIN_SCANNER.nextFloat(); 191 | System.out.print("\nEnter the second number: "); 192 | y = STDIN_SCANNER.nextFloat(); 193 | System.out.print("\nEnter the third number: "); 194 | z = STDIN_SCANNER.nextFloat(); 195 | 196 | if(x < y + z && y < x + z && z < y + x) { 197 | System.out.printf("\nPerimeter of the triangle is: %f\n", x + y + z); 198 | } else { 199 | System.out.print("\nIt is impossible to form a triangle."); 200 | } 201 | } 202 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 203 | } 204 | 205 | ``` 206 | ---------------------------------------- 207 | 208 | # Question 8 209 | 210 | ### **Question:** 211 | 212 | > ***Write a program that reads an integer between 1 and 7 and print the day of the week in English.*** 213 | 214 | --------------------------------------- 215 | 216 | Solution: 217 | 218 | ```java language 219 | 220 | import java.util.Scanner; 221 | 222 | public class MyClass { 223 | public static void main(String[] args) { 224 | int day; 225 | System.out.print("\nEnter a number between 1 to 7 to get the day name: "); 226 | day = STDIN_SCANNER.nextInt(); 227 | switch(day) { 228 | case 1: 229 | System.out.println("Monday"); 230 | break; 231 | case 2: 232 | System.out.println("Tuesday"); 233 | break; 234 | case 3: 235 | System.out.println("Wednesday"); 236 | break; 237 | case 4: 238 | System.out.println("Thursday"); 239 | break; 240 | case 5: 241 | System.out.println("Friday"); 242 | break; 243 | case 6: 244 | System.out.println("Saturday"); 245 | break; 246 | case 7: 247 | System.out.println("Sunday"); 248 | break; 249 | default: 250 | System.out.print("Enter a number between 1 to 7."); 251 | } 252 | } 253 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 254 | } 255 | ``` 256 | ---------------------------------------- 257 | 258 | # Question 9 259 | 260 | ### **Question:** 261 | 262 | > ***Write a program to find the sum of two numbers.*** 263 | 264 | --------------------------------------- 265 | 266 | Solution: 267 | 268 | ```java language 269 | public class MyClass { 270 | public static void main(String[] args) { 271 | int a, b, sum; 272 | a = 1; 273 | b = 2; 274 | sum = a + b; 275 | System.out.print("The sum of a and b = " + sum); 276 | } 277 | } 278 | ``` 279 | ---------------------------------------- 280 | 281 | # Question 10 282 | 283 | ### **Question:** 284 | 285 | > ***Write a program to find the square of a number.*** 286 | 287 | --------------------------------------- 288 | 289 | Solution: 290 | 291 | ```java language 292 | public class MyClass { 293 | public static void main(String[] args) { 294 | int a, b; 295 | a = 2; 296 | b = (int)Math.pow(a, 2); 297 | System.out.print("The square of a = " + b); 298 | } 299 | } 300 | ``` 301 | ---------------------------------------- 302 | 303 | # Question 11 304 | 305 | ### **Question:** 306 | 307 | > ***Write a program to find the greatest of two numbers.*** 308 | 309 | --------------------------------------- 310 | 311 | Solution: 312 | 313 | ```java language 314 | public class MyClass { 315 | public static void main(String[] args) { 316 | int a, b; 317 | a = 2; 318 | b = 3; 319 | if(a > b) { 320 | System.out.print("a is greater than b"); 321 | } else { 322 | System.out.print("b is greater than a"); 323 | } 324 | } 325 | } 326 | ``` 327 | ---------------------------------------- 328 | 329 | # Question 12 330 | 331 | ### **Question:** 332 | 333 | > ***Write a program to print the average of the elements in the array.*** 334 | 335 | --------------------------------------- 336 | 337 | Solution: 338 | 339 | ```java language 340 | public class MyClass { 341 | public static void main(String[] args) { 342 | int avg = 0, sum = 0; 343 | int[] num = {16, 18, 20, 25, 36}; 344 | for(int i = 0; i < 5; i++) { 345 | sum = sum + num[i]; 346 | avg = sum / 5; 347 | } 348 | System.out.println("Sum of the Elements in the array is: " + sum); 349 | System.out.println("Average of the elements in the array is: " + avg); 350 | } 351 | } 352 | ``` 353 | ---------------------------------------- 354 | 355 | # Question 13 356 | 357 | ### **Question:** 358 | 359 | > ***Write a program that prints all even numbers between 1 and 25.*** 360 | 361 | --------------------------------------- 362 | 363 | Solution: 364 | 365 | ```java language 366 | public class MyClass { 367 | public static void main(String[] args) { 368 | System.out.println("Even numbers between 1 to 25:"); 369 | for(int i = 1; i <= 25; i++) { 370 | if(i % 2 == 0) { 371 | System.out.print(i + " "); 372 | } 373 | } 374 | } 375 | } 376 | ``` 377 | ---------------------------------------- 378 | 379 | 380 | # Question 14 381 | 382 | ### **Question:** 383 | 384 | > ***Write a program that prints all odd numbers between 1 and 50.*** 385 | 386 | --------------------------------------- 387 | 388 | Solution: 389 | 390 | ```java language 391 | public class MyClass { 392 | public static void main(String[] args) { 393 | System.out.println("Odd numbers between 1 to 50:"); 394 | for(int i = 1; i <= 50; i++) { 395 | if(i % 2 != 0) { 396 | System.out.print(i + " "); 397 | } 398 | } 399 | } 400 | } 401 | ``` 402 | ---------------------------------------- 403 | 404 | 405 | # Question 15 406 | 407 | ### **Question:** 408 | 409 | > ***Write a program to print the first 10 numbers starting from one together with their squares and cubes.*** 410 | 411 | --------------------------------------- 412 | 413 | Solution: 414 | 415 | ```java language 416 | public class MyClass { 417 | public static void main(String[] args) { 418 | for(int i = 1; i <= 10; i++) { 419 | System.out.println("Number = " + i + " its square = " + (i * i) + " its cube = " + (i * i * i)); 420 | } 421 | } 422 | } 423 | ``` 424 | ---------------------------------------- 425 | 426 | # Question 16 427 | 428 | ### **Question:** 429 | 430 | > ***Write a program:
431 | If you enter a character M
432 | Output must be: ch = M.*** 433 | 434 | --------------------------------------- 435 | 436 | Solution: 437 | 438 | ```java language 439 | public class MyClass { 440 | public static void main(String[] args) throws Exception { 441 | char c; 442 | System.out.print("Enter a character: "); 443 | c = (char)System.in.read(); 444 | System.out.println("ch = " + c); 445 | } 446 | } 447 | ``` 448 | ---------------------------------------- 449 | 450 | # Question 17 451 | 452 | ### **Question:** 453 | 454 | > ***Write a program to print the multiplication table of a number entered by the user.*** 455 | 456 | --------------------------------------- 457 | 458 | Solution: 459 | 460 | ```java language 461 | import java.util.Scanner; 462 | 463 | public class MyClass { 464 | public static void main(String[] args) { 465 | int n; 466 | System.out.print("Enter any number: "); 467 | n = STDIN_SCANNER.nextInt(); 468 | for(int i = 1; i <= 5; i++) { 469 | System.out.println(n + " * " + i + " = " + (n * i)); 470 | } 471 | } 472 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 473 | } 474 | ``` 475 | ---------------------------------------- 476 | 477 | 478 | # Question 18 479 | 480 | ### **Question:** 481 | 482 | > ***Write a program to print the product of the first 10 digits.*** 483 | 484 | --------------------------------------- 485 | 486 | Solution: 487 | 488 | ```java language 489 | public class MyClass { 490 | public static void main(String[] args) { 491 | int product = 1; 492 | for(int i = 1; i <= 10; i++) { 493 | product = product * i; 494 | } 495 | System.out.print("The product of the first 10 digits is: " + product); 496 | } 497 | } 498 | ``` 499 | ---------------------------------------- 500 | 501 | # Question 19 502 | 503 | ### **Question:** 504 | 505 | > ***Write a program to print whether the given number is positive or negative.*** 506 | 507 | --------------------------------------- 508 | 509 | Solution: 510 | 511 | ```java language 512 | public class MyClass { 513 | public static void main(String[] args) { 514 | int a; 515 | a = -35; 516 | if(a > 0) { 517 | System.out.print("Number is positive"); 518 | } else { 519 | System.out.print("Number is negative"); 520 | } 521 | } 522 | } 523 | ``` 524 | ---------------------------------------- 525 | 526 | # Question 20 527 | 528 | ### **Question:** 529 | 530 | > ***Write a program to check the equivalence of two numbers entered by the user.*** 531 | 532 | --------------------------------------- 533 | 534 | Solution: 535 | 536 | ```java language 537 | import java.util.Scanner; 538 | 539 | public class MyClass { 540 | public static void main(String[] args) { 541 | int x, y; 542 | System.out.print("\nEnter the first number: "); 543 | x = STDIN_SCANNER.nextInt(); 544 | System.out.print("\nEnter the second number: "); 545 | y = STDIN_SCANNER.nextInt(); 546 | if(x - y == 0) { 547 | System.out.print("\nThe two numbers are equivalent"); 548 | } else { 549 | System.out.print("\nThe two numbers are not equivalent"); 550 | } 551 | } 552 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 553 | } 554 | ``` 555 | ---------------------------------------- 556 | 557 | # Question 21 558 | 559 | ### **Question:** 560 | 561 | > ***Write a program to print the remainder of two numbers entered by the user.*** 562 | 563 | --------------------------------------- 564 | 565 | Solution: 566 | 567 | ```java language 568 | import java.util.Scanner; 569 | 570 | public class MyClass { 571 | public static void main(String[] args) { 572 | int a, b, c; 573 | System.out.print("\nEnter the first number: "); 574 | a = STDIN_SCANNER.nextInt(); 575 | System.out.print("\nEnter the second number: "); 576 | b = STDIN_SCANNER.nextInt(); 577 | c = a % b; 578 | System.out.print("\n The remainder of " + a + " and " + b + " is: " + c); 579 | } 580 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 581 | } 582 | ``` 583 | ---------------------------------------- 584 | 585 | # Question 22 586 | 587 | ### **Question:** 588 | 589 | > ***Write a program to print the characters from A to Z.*** 590 | 591 | --------------------------------------- 592 | 593 | Solution: 594 | 595 | ```java language 596 | public class MyClass { 597 | public static void main(String[] args) { 598 | for(byte i = 'A'; i <= 'Z'; i++) { 599 | System.out.println((char)Byte.toUnsignedInt(i)); 600 | } 601 | } 602 | } 603 | ``` 604 | ---------------------------------------- 605 | 606 | 607 | # Question 23 608 | 609 | ### **Question:** 610 | 611 | > ***Write a program to print the length of the entered string.*** 612 | 613 | --------------------------------------- 614 | 615 | Solution: 616 | 617 | ```java language 618 | import java.util.Scanner; 619 | public class MyClass { 620 | public static void main(String[] args) { 621 | String a; 622 | Scanner scan = new Scanner(System.in); 623 | System.out.print("Enter Your Name : "); 624 | a = scan.nextLine(); 625 | System.out.println("The length of the String is: " + a.length()); 626 | } 627 | } 628 | ``` 629 | ---------------------------------------- 630 | 631 | 632 | # Question 24 633 | 634 | ### **Question:** 635 | 636 | > ***Write a program to check whether the given character is a lower case letter or not.*** 637 | 638 | --------------------------------------- 639 | 640 | Solution: 641 | 642 | ```java language 643 | public class MyClass { 644 | public static void main(String[] args) { 645 | char ch = 'a'; 646 | if(Character.isLowerCase(ch)) { 647 | System.out.println("The given character is a lower case letter"); 648 | } 649 | else { 650 | System.out.println("The given character is a upper case letter"); 651 | } 652 | } 653 | } 654 | ``` 655 | ---------------------------------------- 656 | 657 | 658 | # Question 25 659 | 660 | ### **Question:** 661 | 662 | > ***Write a program to check whether the given character is a upper case letter or not.*** 663 | 664 | --------------------------------------- 665 | 666 | Solution: 667 | 668 | ```java language 669 | public class MyClass { 670 | public static void main(String[] args) { 671 | char ch = 'A'; 672 | if(Character.isUpperCase(ch)) { 673 | System.out.println("The given character is a upper case letter"); 674 | } 675 | else { 676 | System.out.println("The given character is a lower case letter"); 677 | } 678 | } 679 | } 680 | 681 | ``` 682 | ---------------------------------------- 683 | 684 | 685 | # Question 26 686 | 687 | ### **Question:** 688 | 689 | > ***Write a program to convert the lower case string to upper case string.*** 690 | 691 | --------------------------------------- 692 | 693 | Solution: 694 | 695 | ```java language 696 | public class MyClass { 697 | public static void main(String[] args) { 698 | String a = "albert einstein"; 699 | System.out.println(a.toUpperCase()); 700 | } 701 | } 702 | ``` 703 | ---------------------------------------- 704 | 705 | # Question 27 706 | 707 | ### **Question:** 708 | 709 | > ***Write a program that takes a distance in centimeters and outputs the corresponding value in inches.*** 710 | 711 | --------------------------------------- 712 | 713 | Solution: 714 | 715 | ```java language 716 | import java.util.Scanner; 717 | 718 | public class MyClass { 719 | public final static double X = 2.54; 720 | public static void main(String[] args) { 721 | double inch, cm; 722 | System.out.print("Enter the distance in cm: "); 723 | cm = STDIN_SCANNER.nextDouble(); 724 | inch = cm / X; 725 | System.out.printf("\nDistance of %.2f cms is equal to %.2f inches", cm, inch); 726 | } 727 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 728 | } 729 | ``` 730 | ---------------------------------------- 731 | 732 | 733 | # Question 28 734 | 735 | ### **Question:** 736 | 737 | > ***Write a program to print the output:
738 | Einstein [0] = E
739 | Einstein [1] = I
740 | Einstein [2] = N
741 | Einstein [3] = S
742 | Einstein [4] = T
743 | Einstein [5] = E
744 | Einstein [6] = I
745 | Einstein [7] = N
*** 746 | 747 | --------------------------------------- 748 | 749 | Solution: 750 | 751 | ```java language 752 | public class MyClass { 753 | public static void main(String[] args) throws Exception{ 754 | int i; 755 | char [] num = {'E' , 'I', 'N', 'S', 'T', 'E', 'I', 'N'}; 756 | for(i=0; i<8; i++) 757 | System.out.println("Einstein [" + i + " ] = " + num[i]); 758 | } 759 | } 760 | ``` 761 | ---------------------------------------- 762 | 763 | 764 | # Question 29 765 | 766 | ### **Question:** 767 | 768 | > ***Write a program to print "Hello World" 10 times.*** 769 | 770 | --------------------------------------- 771 | 772 | Solution: 773 | 774 | ```java language 775 | public class MyClass { 776 | public static void main(String[] args) { 777 | for(int i = 1; i <= 10; i++) { 778 | System.out.println("Hello World "); 779 | } 780 | } 781 | } 782 | ``` 783 | ---------------------------------------- 784 | 785 | 786 | # Question 30 787 | 788 | ### **Question:** 789 | 790 | > ***Write a program to print first 5 numbers using do while loop statement.*** 791 | 792 | --------------------------------------- 793 | 794 | Solution: 795 | 796 | ```java language 797 | public class MyClass { 798 | public static void main(String[] args) { 799 | int i = 1; 800 | do { 801 | System.out.println(i++); 802 | } while(i <= 5); 803 | } 804 | } 805 | ``` 806 | ---------------------------------------- 807 | 808 | 809 | 810 | # Question 31 811 | 812 | ### **Question:** 813 | 814 | > ***Write a program to check whether a character is an alphabet or not.*** 815 | 816 | --------------------------------------- 817 | 818 | Solution: 819 | 820 | ```java language 821 | import java.util.Scanner; 822 | 823 | public class Main { 824 | public static void main(String[] args) { 825 | Scanner scanner = new Scanner(System.in); 826 | System.out.println("Enter any caracter: "); 827 | char c = scanner.next().charAt(0); 828 | if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { 829 | System.out.println(c + " is a Alphabet."); 830 | } else { 831 | System.out.println(c + " is not a Alphabet."); 832 | } 833 | } 834 | } 835 | ``` 836 | ---------------------------------------- 837 | 838 | 839 | # Question 32 840 | 841 | ### **Question:** 842 | 843 | > ***Write a program to check whether a entered number is even or odd.*** 844 | 845 | --------------------------------------- 846 | 847 | Solution: 848 | 849 | ```java language 850 | import java.util.Scanner; 851 | 852 | public class MyClass { 853 | public static void main(String[] args) { 854 | int a; 855 | System.out.print("Enter any number: "); 856 | a = STDIN_SCANNER.nextInt(); 857 | if(a % 2 == 0) { 858 | System.out.print("The entered number is even"); 859 | } else { 860 | System.out.print("The entered number is odd"); 861 | } 862 | } 863 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 864 | } 865 | ``` 866 | ---------------------------------------- 867 | 868 | 869 | # Question 33 870 | 871 | ### **Question:** 872 | 873 | > ***Write a program to print the ASCII value of the given character.*** 874 | 875 | --------------------------------------- 876 | 877 | Solution: 878 | 879 | ```java language 880 | public class MyClass { 881 | public static void main(String[] args) { 882 | byte ch = 'A'; 883 | System.out.print("The ASCII value of " + ((char)Byte.toUnsignedInt(ch)) + " is: " + ch); 884 | } 885 | } 886 | ``` 887 | ---------------------------------------- 888 | 889 | 890 | # Question 34 891 | 892 | ### **Question:** 893 | 894 | > ***Write a program that will print all numbers between 1 to 50 which divided by a specified number and the remainder will be 2.*** 895 | 896 | --------------------------------------- 897 | 898 | Solution: 899 | 900 | ```java language 901 | import java.util.Scanner; 902 | 903 | public class MyClass { 904 | public static void main(String[] args) { 905 | int x; 906 | System.out.print("Enter a number: "); 907 | x = STDIN_SCANNER.nextInt(); 908 | for(int i = 1; i <= 50; i++) { 909 | if(i % x == 2) { 910 | System.out.println(i); 911 | } 912 | } 913 | } 914 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 915 | } 916 | ``` 917 | ---------------------------------------- 918 | 919 | 920 | # Question 35 921 | 922 | ### **Question:** 923 | 924 | > ***Write a program to determine whether two numbers in a pair are in ascending or descending order.*** 925 | 926 | --------------------------------------- 927 | 928 | Solution: 929 | 930 | ```java language 931 | import java.util.Scanner; 932 | 933 | public class MyClass { 934 | public static void main(String[] args) { 935 | int a, b; 936 | System.out.print("\nEnter a pair of numbers (for example 22,12 | 12,22): "); 937 | System.out.print("\nEnter the first number: "); 938 | a = STDIN_SCANNER.nextInt(); 939 | System.out.print("\nEnter the second number: "); 940 | b = STDIN_SCANNER.nextInt(); 941 | if(a > b) { 942 | System.out.print("\nThe two numbers in a pair are in descending order."); 943 | } else { 944 | System.out.print("\nThe two numbers in a pair are in ascending order."); 945 | } 946 | } 947 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 948 | } 949 | 950 | ``` 951 | ---------------------------------------- 952 | 953 | 954 | 955 | # Question 36 956 | 957 | ### **Question:** 958 | 959 | > ***Write a program that reads two numbers and divides one by the other. Specify "Division not possible" if that is not possible.*** 960 | 961 | --------------------------------------- 962 | 963 | Solution: 964 | 965 | ```java language 966 | import java.util.Scanner; 967 | 968 | public class MyClass { 969 | public static void main(String[] args) { 970 | int a, b; 971 | float c; 972 | System.out.print("\nEnter the first number: "); 973 | a = STDIN_SCANNER.nextInt(); 974 | System.out.print("\nEnter the second number: "); 975 | b = STDIN_SCANNER.nextInt(); 976 | if(b != 0) { 977 | c = (float)a / (float)b; 978 | System.out.printf("\n%d/%d = %.1f", a, b, c); 979 | } else { 980 | System.out.println("\nDivision not possible."); 981 | } 982 | } 983 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 984 | } 985 | ``` 986 | ---------------------------------------- 987 | 988 | # Question 37 989 | 990 | ### **Question:** 991 | 992 | > ***Write a program that will print all numbers between 1 to 50 which divided by a specified number and the remainder is equal to 2 or 3.*** 993 | 994 | --------------------------------------- 995 | 996 | Solution: 997 | 998 | ```java language 999 | import java.util.Scanner; 1000 | 1001 | public class MyClass { 1002 | public static void main(String[] args) { 1003 | int x; 1004 | System.out.print("Enter a number: "); 1005 | x = STDIN_SCANNER.nextInt(); 1006 | for(int i = 1; i <= 50; i++) { 1007 | if(i % x == 2 || i % x == 3) { 1008 | System.out.println(i); 1009 | } 1010 | } 1011 | } 1012 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1013 | } 1014 | ``` 1015 | ---------------------------------------- 1016 | 1017 | 1018 | 1019 | # Question 38 1020 | 1021 | ### **Question:** 1022 | 1023 | > ***Write a program that adds up all numbers between 1 and 100 that are not divisible by 12.*** 1024 | 1025 | --------------------------------------- 1026 | 1027 | Solution: 1028 | 1029 | ```java language 1030 | public class MyClass { 1031 | public static void main(String[] args) { 1032 | int x = 12, sum = 0; 1033 | for(int i = 1; i <= 100; i++) { 1034 | if(i % x != 0) { 1035 | sum += i; 1036 | } 1037 | } 1038 | System.out.println("\nSum: " + sum); 1039 | } 1040 | } 1041 | ``` 1042 | ---------------------------------------- 1043 | 1044 | 1045 | # Question 39 1046 | 1047 | ### **Question:** 1048 | 1049 | > ***Write a program to calculate the value of x where x = 1 + 1/2 + 1/3 + … + 1/50.*** 1050 | 1051 | --------------------------------------- 1052 | 1053 | Solution: 1054 | 1055 | ```java language 1056 | public class MyClass { 1057 | public static void main(String[] args) { 1058 | float x = 0; 1059 | for(int i = 1; i <= 50; i++) { 1060 | x += (float)1 / i; 1061 | } 1062 | System.out.printf("Value of x: %.2f\n", x); 1063 | } 1064 | } 1065 | ``` 1066 | ---------------------------------------- 1067 | 1068 | 1069 | # Question 40 1070 | 1071 | ### **Question:** 1072 | 1073 | > ***Write a program that reads a number and find all its divisor.*** 1074 | 1075 | --------------------------------------- 1076 | 1077 | Solution: 1078 | 1079 | ```java language 1080 | import java.util.Scanner; 1081 | 1082 | public class MyClass { 1083 | public static void main(String[] args) { 1084 | int x; 1085 | System.out.print("\nEnter a number: "); 1086 | x = STDIN_SCANNER.nextInt(); 1087 | System.out.print("All the divisor of " + x + " are: "); 1088 | for(int i = 1; i <= x; i++) { 1089 | if(x % i == 0) { 1090 | System.out.print("\n" + i); 1091 | } 1092 | } 1093 | } 1094 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1095 | } 1096 | ``` 1097 | ---------------------------------------- 1098 | 1099 | 1100 | # Question 41 1101 | 1102 | ### **Question:** 1103 | 1104 | > ***Write a program to find the incremented and decremented values of two numbers.*** 1105 | 1106 | --------------------------------------- 1107 | 1108 | Solution: 1109 | 1110 | ```java language 1111 | public class MyClass { 1112 | public static void main(String[] args) { 1113 | int a, b, c, d, e, f; 1114 | a = 10; 1115 | b = 12; 1116 | c = a + 1; 1117 | d = b + 1; 1118 | e = a - 1; 1119 | f = b - 1; 1120 | System.out.print("\nThe incremented value of a =" + c); 1121 | System.out.print("\nThe incremented value of b =" + d); 1122 | System.out.print("\nThe decremented value of a =" + e); 1123 | System.out.print("\nThe decremented value of b =" + f); 1124 | } 1125 | } 1126 | ``` 1127 | ---------------------------------------- 1128 | 1129 | 1130 | # Question 42 1131 | 1132 | ### **Question:** 1133 | 1134 | > ***Write a program to find square of a entered number using functions.*** 1135 | 1136 | --------------------------------------- 1137 | 1138 | Solution: 1139 | 1140 | ```java language 1141 | import java.util.Scanner; 1142 | 1143 | public class MyClass { 1144 | public static void main(String[] args) { 1145 | int answer; 1146 | answer = square(); 1147 | System.out.print("The square of the entered number is: " + answer); 1148 | } 1149 | 1150 | public static int square() { 1151 | int x; 1152 | System.out.print("Enter any number: "); 1153 | x = STDIN_SCANNER.nextInt(); 1154 | return x * x; 1155 | } 1156 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1157 | } 1158 | ``` 1159 | ---------------------------------------- 1160 | 1161 | 1162 | # Question 43 1163 | 1164 | ### **Question:** 1165 | 1166 | > ***Write a program that accepts principal amount, rate of interest, time and compute the simple interest.*** 1167 | 1168 | --------------------------------------- 1169 | 1170 | Solution: 1171 | 1172 | ```java language 1173 | import java.util.Scanner; 1174 | 1175 | public class MyClass { 1176 | public static void main(String[] args) { 1177 | int p, r, t, SI; 1178 | System.out.print("\nEnter the principal amount: "); 1179 | p = STDIN_SCANNER.nextInt(); 1180 | System.out.print("\nEnter the rate of interest: "); 1181 | r = STDIN_SCANNER.nextInt(); 1182 | System.out.print("\nEnter the time: "); 1183 | t = STDIN_SCANNER.nextInt(); 1184 | SI = (p * r * t) / 100; 1185 | System.out.print("\nSimple interest is: " + SI); 1186 | } 1187 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1188 | } 1189 | ``` 1190 | ---------------------------------------- 1191 | 1192 | 1193 | # Question 44 1194 | 1195 | ### **Question:** 1196 | 1197 | > ***Write a program that swaps two numbers without using third variable.*** 1198 | 1199 | --------------------------------------- 1200 | 1201 | Solution: 1202 | 1203 | ```java language 1204 | import java.util.Scanner; 1205 | 1206 | public class MyClass { 1207 | public static void main(String[] args) { 1208 | int a, b; 1209 | System.out.print("\nEnter the value for a: "); 1210 | a = STDIN_SCANNER.nextInt(); 1211 | System.out.print("\nEnter the value for b: "); 1212 | b = STDIN_SCANNER.nextInt(); 1213 | System.out.print("\nBefore swapping: " + a + " " + b); 1214 | a = a + b; 1215 | b = a - b; 1216 | a = a - b; 1217 | System.out.print("\nAfter swapping: " + a + " " + b); 1218 | } 1219 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1220 | } 1221 | ``` 1222 | ---------------------------------------- 1223 | 1224 | # Question 45 1225 | 1226 | ### **Question:** 1227 | 1228 | > ***Write a program to compute the area of a hexagon.*** 1229 | 1230 | --------------------------------------- 1231 | 1232 | Solution: 1233 | 1234 | ```java language 1235 | import java.util.Scanner; 1236 | public class MyClass { 1237 | public static void main(String[] args) { 1238 | Scanner input = new Scanner(System.in); 1239 | System.out.print("Enter the length of a side of the hexagon: "); 1240 | double s = input.nextDouble(); 1241 | double area = (6*(s*s))/(4*Math.tan(Math.PI/6)); 1242 | System.out.print("The area of the hexagon is: " + area); 1243 | } 1244 | } 1245 | 1246 | ``` 1247 | ---------------------------------------- 1248 | 1249 | # Question 46 1250 | 1251 | ### **Question:** 1252 | 1253 | > ***Write a program to print the output:
1254 | body [b] = b
1255 | body [o] = o
1256 | body [d] = d
1257 | body [y] = y
*** 1258 | 1259 | --------------------------------------- 1260 | 1261 | Solution: 1262 | 1263 | ```java language 1264 | public class MyClass { 1265 | public static void main(String[] args) throws Exception{ 1266 | int i; 1267 | char [] body = {'b', 'o', 'd', 'y'}; 1268 | for(i=0; i<4; i++) { 1269 | System.out.println("body [" + body [i] + " ] = " + body [i]); 1270 | } 1271 | } 1272 | } 1273 | ``` 1274 | ---------------------------------------- 1275 | 1276 | # Question 47 1277 | 1278 | ### **Question:** 1279 | 1280 | > ***Write a program to calculate the discounted price and the total price after discount
1281 | Given:
1282 | If purchase value is greater than 1000, 10% discount
1283 | If purchase value is greater than 5000, 20% discount
1284 | If purchase value is greater than 10000, 30% discount.*** 1285 | 1286 | --------------------------------------- 1287 | 1288 | Solution: 1289 | 1290 | ```java language 1291 | import java.util.Scanner; 1292 | public class MyClass { 1293 | public static void main(String[] args) { 1294 | double pv; 1295 | System.out.print("Enter purchased value: "); 1296 | pv = STDIN_SCANNER.nextDouble(); 1297 | if(pv > 1000) { 1298 | System.out.printf("\n Discount = %f", pv * 0.1); 1299 | System.out.printf("\n Total = %f", pv - pv * 0.1); 1300 | } else if(pv > 5000) { 1301 | System.out.printf("\n Discount = %f", pv * 0.2); 1302 | System.out.printf("\n Total = %f", pv - pv * 0.2); 1303 | } else { 1304 | System.out.printf("\n Discount = %f", pv * 0.3); 1305 | System.out.printf("\n Total = %f", pv - pv * 0.3); 1306 | } 1307 | } 1308 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1309 | } 1310 | ``` 1311 | ---------------------------------------- 1312 | 1313 | # Question 48 1314 | 1315 | ### **Question:** 1316 | 1317 | > ***Write a program to print the first ten natural numbers using while loop statement.*** 1318 | 1319 | --------------------------------------- 1320 | 1321 | Solution: 1322 | 1323 | ```java language 1324 | public class MyClass { 1325 | public static void main(String[] args) { 1326 | int i = 1; 1327 | while(i <= 10) { 1328 | System.out.println(i++); 1329 | } 1330 | } 1331 | } 1332 | ``` 1333 | ---------------------------------------- 1334 | 1335 | 1336 | # Question 49 1337 | 1338 | ### **Question:** 1339 | 1340 | > ***Write a program to shift inputted data by two bits to the left.*** 1341 | 1342 | --------------------------------------- 1343 | 1344 | Solution: 1345 | 1346 | ```java language 1347 | import java.util.Scanner; 1348 | 1349 | public class MyClass { 1350 | public static void main(String[] args) { 1351 | int x; 1352 | System.out.print("Enter the integer from keyboard: "); 1353 | x = STDIN_SCANNER.nextInt(); 1354 | System.out.print("\nEntered value: " + x + " "); 1355 | System.out.print("\nThe left shifted data is: " + (x <<= 2) + " "); 1356 | } 1357 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1358 | } 1359 | ``` 1360 | ---------------------------------------- 1361 | 1362 | # Question 50 1363 | 1364 | ### **Question:** 1365 | 1366 | > ***Write a program to shift inputted data by two bits to the Right.*** 1367 | 1368 | --------------------------------------- 1369 | 1370 | Solution: 1371 | 1372 | ```java language 1373 | import java.util.Scanner; 1374 | 1375 | public class MyClass { 1376 | public static void main(String[] args) { 1377 | int x; 1378 | System.out.print("Enter the integer from keyboard: "); 1379 | x = STDIN_SCANNER.nextInt(); 1380 | System.out.print("\nEntered value: " + x + " "); 1381 | System.out.print("\nThe right shifted data is: " + (x >>= 2) + " "); 1382 | } 1383 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1384 | } 1385 | ``` 1386 | ---------------------------------------- 1387 | 1388 | 1389 | 1390 | # Question 51 1391 | 1392 | ### **Question:** 1393 | 1394 | > ***Write a program to calculate the exact difference between x and 21. Return three times the absolute difference if x is greater than 21.*** 1395 | 1396 | --------------------------------------- 1397 | 1398 | Solution: 1399 | 1400 | ```java language 1401 | import java.util.Scanner; 1402 | 1403 | public class MyClass { 1404 | public static void main(String[] args) { 1405 | int x; 1406 | System.out.print("Enter the value for x: "); 1407 | x = STDIN_SCANNER.nextInt(); 1408 | if(x <= 21) { 1409 | System.out.print(Math.abs(x - 21)); 1410 | } else if(x >= 21) { 1411 | System.out.print(Math.abs(x - 21) * 3); 1412 | } 1413 | } 1414 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1415 | } 1416 | ``` 1417 | ---------------------------------------- 1418 | 1419 | 1420 | # Question 52 1421 | 1422 | ### **Question:** 1423 | 1424 | > ***Write a program that reads in two numbers and determine whether the first number is a multiple of the second number.*** 1425 | 1426 | --------------------------------------- 1427 | 1428 | Solution: 1429 | 1430 | ```java language 1431 | import java.util.Scanner; 1432 | 1433 | public class MyClass { 1434 | public static void main(String[] args) { 1435 | int x, y; 1436 | System.out.print("\nEnter the first number: "); 1437 | x = STDIN_SCANNER.nextInt(); 1438 | System.out.print("\nEnter the second number: "); 1439 | y = STDIN_SCANNER.nextInt(); 1440 | if(x % y == 0) { 1441 | System.out.println("\n" + x + " is a multiple of " + y + "."); 1442 | } else { 1443 | System.out.println("\n" + x + " is not a multiple of " + y + "."); 1444 | } 1445 | } 1446 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1447 | } 1448 | 1449 | ``` 1450 | ---------------------------------------- 1451 | 1452 | 1453 | # Question 53 1454 | 1455 | ### **Question:** 1456 | 1457 | > ***Write a program to display the system time.*** 1458 | 1459 | --------------------------------------- 1460 | 1461 | Solution: 1462 | 1463 | ```java language 1464 | public class MyClass { 1465 | public static void main(String[] args) { 1466 | System.out.format("\nCurrent Date time: %tc%n\n", System.currentTimeMillis()); 1467 | } 1468 | } 1469 | ``` 1470 | ---------------------------------------- 1471 | 1472 | 1473 | # Question 54 1474 | 1475 | ### **Question:** 1476 | 1477 | > ***Write a program to convert Celsius into Fahrenheit.*** 1478 | 1479 | --------------------------------------- 1480 | 1481 | Solution: 1482 | 1483 | ```java language 1484 | public class MyClass { 1485 | public static void main(String[] args) { 1486 | float fahrenheit, celsius; 1487 | celsius = 36; 1488 | fahrenheit = (celsius * 9) / 5 + 32; 1489 | System.out.printf("\nTemperature in fahrenheit is: %f", fahrenheit); 1490 | } 1491 | } 1492 | ``` 1493 | ---------------------------------------- 1494 | 1495 | 1496 | # Question 55 1497 | 1498 | ### **Question:** 1499 | 1500 | > ***Write a program that will examine two inputted integers and return true if either of them is 50 or if their sum is 50.*** 1501 | 1502 | --------------------------------------- 1503 | 1504 | Solution: 1505 | 1506 | ```java language 1507 | import java.util.Scanner; 1508 | 1509 | public class MyClass { 1510 | public static void main(String[] args) { 1511 | int x, y; 1512 | System.out.print("\nEnter the value for x: "); 1513 | x = STDIN_SCANNER.nextInt(); 1514 | System.out.print("\nEnter the value for y: "); 1515 | y = STDIN_SCANNER.nextInt(); 1516 | if(x == 50 || y == 50 || x + y == 50) { 1517 | System.out.print("\nTrue"); 1518 | } else { 1519 | System.out.print("\nFalse"); 1520 | } 1521 | } 1522 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1523 | } 1524 | ``` 1525 | ---------------------------------------- 1526 | 1527 | 1528 | # Question 56 1529 | 1530 | ### **Question:** 1531 | 1532 | > ***Write a program that counts the even, odd, positive, and negative values among eighteen integer inputs.*** 1533 | 1534 | --------------------------------------- 1535 | 1536 | Solution: 1537 | 1538 | ```java language 1539 | import java.util.Scanner; 1540 | 1541 | public class MyClass { 1542 | public static void main(String[] args) { 1543 | int x, even = 0, odd = 0, positive = 0, negative = 0; 1544 | System.out.println("\nPlease enter 18 numbers:"); 1545 | for(int i = 0; i < 18; i++) { 1546 | x = STDIN_SCANNER.nextInt(); 1547 | if(x > 0) { 1548 | positive++; 1549 | } 1550 | if(x < 0) { 1551 | negative++; 1552 | } 1553 | if(x % 2 == 0) { 1554 | even++; 1555 | } 1556 | if(x % 2 != 0) { 1557 | odd++; 1558 | } 1559 | } 1560 | System.out.print("\nNumber of even values: " + even); 1561 | System.out.print("\nNumber of odd values: " + odd); 1562 | System.out.print("\nNumber of positive values: " + positive); 1563 | System.out.print("\nNumber of negative values: " + negative); 1564 | } 1565 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1566 | } 1567 | ``` 1568 | ---------------------------------------- 1569 | 1570 | 1571 | # Question 57 1572 | 1573 | ### **Question:** 1574 | 1575 | > ***Write a program to check whether the person is a senior citizen or not.*** 1576 | 1577 | --------------------------------------- 1578 | 1579 | Solution: 1580 | 1581 | ```java language 1582 | import java.util.Scanner; 1583 | 1584 | public class MyClass { 1585 | public static void main(String[] args) { 1586 | int age; 1587 | System.out.print("Enter age: "); 1588 | age = STDIN_SCANNER.nextInt(); 1589 | if(age >= 60) { 1590 | System.out.print("Senior citizen"); 1591 | } else { 1592 | System.out.print("Not a senior citizen"); 1593 | } 1594 | } 1595 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1596 | } 1597 | ``` 1598 | ---------------------------------------- 1599 | 1600 | 1601 | # Question 58 1602 | 1603 | ### **Question:** 1604 | 1605 | > ***Write a program that reads a student's three subject scores (0-100) and computes the average of those scores.*** 1606 | 1607 | --------------------------------------- 1608 | 1609 | Solution: 1610 | 1611 | ```java language 1612 | import java.util.Scanner; 1613 | 1614 | public class MyClass { 1615 | public static void main(String[] args) { 1616 | float score, totalScore = 0; 1617 | int subject = 0; 1618 | System.out.println("Enter three subject scores (0-100):"); 1619 | while(subject != 3) { 1620 | score = STDIN_SCANNER.nextFloat(); 1621 | if(score < 0 || score > 100) { 1622 | System.out.println("Please enter a valid score."); 1623 | } else { 1624 | totalScore += score; 1625 | subject++; 1626 | } 1627 | } 1628 | System.out.printf("Average score = %.2f\n", totalScore / 3); 1629 | } 1630 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1631 | } 1632 | ``` 1633 | ---------------------------------------- 1634 | 1635 | 1636 | # Question 59 1637 | 1638 | ### **Question:** 1639 | 1640 | > ***What results would the following programs produce?*** 1641 | 1642 | ---------------------------------------- 1643 | 1644 | ```java language 1645 | public class MyClass { 1646 | public static void main(String[] args) { 1647 | for(int i = 1; i <= 5; i++) { 1648 | if(i == 3) { 1649 | break; 1650 | } 1651 | System.out.println(i); 1652 | } 1653 | } 1654 | } 1655 | ``` 1656 | ---------------------------------------- 1657 | 1658 | Solution: 1659 | 1660 | ```java language 1661 | 1 1662 | 2 1663 | ``` 1664 | ---------------------------------------- 1665 | 1666 | ```java language 1667 | public class MyClass { 1668 | public static void main(String[] args) { 1669 | System.out.println(-7 + 9 * 5); 1670 | System.out.println((68+7) % 8); 1671 | System.out.println(50 + -6*5 / 5); 1672 | System.out.println(6 + 25 / 3 * 6 - 8 % 2); 1673 | } 1674 | } 1675 | 1676 | ``` 1677 | ---------------------------------------- 1678 | 1679 | Solution: 1680 | 1681 | ```java language 1682 | 38 1683 | 3 1684 | 44 1685 | 54 1686 | ``` 1687 | ---------------------------------------- 1688 | 1689 | ```java language 1690 | public class MyClass { 1691 | public static void main(String[] args) { 1692 | for(;;) { 1693 | System.out.println("This loop will run forever."); 1694 | } 1695 | } 1696 | } 1697 | ``` 1698 | ---------------------------------------- 1699 | 1700 | Solution: 1701 | 1702 | ```java language 1703 | This loop will run forever. 1704 | This loop will run forever. 1705 | This loop will run forever. 1706 | This loop will run forever. 1707 | This loop will run forever. 1708 | This loop will run forever. ......... 1709 | ``` 1710 | ---------------------------------------- 1711 | 1712 | 1713 | ```java language 1714 | public class MyClass { 1715 | public static void main(String[] args) { 1716 | System.out.println((35.5 * 3.7 - 3.6 * 7.5) / (60.8 - 8.9)); 1717 | } 1718 | } 1719 | ``` 1720 | ---------------------------------------- 1721 | 1722 | Solution: 1723 | 1724 | ```java language 1725 | 2.010597302504817 1726 | ``` 1727 | ---------------------------------------- 1728 | 1729 | ```java language 1730 | public class MyClass { 1731 | public static void main(String[] args) { 1732 | System.out.println("linux"); 1733 | System.exit(0); 1734 | System.out.println("php"); 1735 | } 1736 | } 1737 | ``` 1738 | ---------------------------------------- 1739 | 1740 | Solution: 1741 | 1742 | ```java language 1743 | linux 1744 | ``` 1745 | ---------------------------------------- 1746 | 1747 | ```java language 1748 | public class MyClass { 1749 | public static void main(String[] args) { 1750 | for(int i = 1; i <= 5; i++) { 1751 | if(i == 3) { 1752 | continue; 1753 | } 1754 | System.out.print(i + "\n "); 1755 | } 1756 | } 1757 | } 1758 | ``` 1759 | ---------------------------------------- 1760 | 1761 | Solution: 1762 | 1763 | ```java language 1764 | 1 1765 | 2 1766 | 4 1767 | 5 1768 | ``` 1769 | ---------------------------------------- 1770 | 1771 | ```java language 1772 | public class MyClass { 1773 | public static void main(String[] args) { 1774 | int a = 10, b = 20, c; 1775 | c = a < b ? a : b; 1776 | System.out.print(c); 1777 | } 1778 | } 1779 | 1780 | ``` 1781 | ---------------------------------------- 1782 | 1783 | Solution: 1784 | 1785 | ```java language 1786 | 10 1787 | 1788 | ``` 1789 | 1790 | ---------------------------------------- 1791 | 1792 | ```java language 1793 | public class MyClass { 1794 | public final static int A = 15; 1795 | public static void main(String[] args) { 1796 | int x; 1797 | x = A; 1798 | System.out.print(x); 1799 | } 1800 | } 1801 | 1802 | ``` 1803 | ---------------------------------------- 1804 | 1805 | Solution: 1806 | 1807 | ```java language 1808 | 15 1809 | 1810 | ``` 1811 | 1812 | ---------------------------------------- 1813 | 1814 | 1815 | ```java language 1816 | public class MyClass { 1817 | public static void main(String[] args) { 1818 | for(int i = 1; i <= 3; i++) { 1819 | System.out.print((i & 1) != 0 ? "odd\n" : "even\n"); 1820 | } 1821 | System.exit(0); 1822 | } 1823 | } 1824 | 1825 | ``` 1826 | ---------------------------------------- 1827 | 1828 | Solution: 1829 | 1830 | ```java language 1831 | odd 1832 | even 1833 | odd 1834 | 1835 | ``` 1836 | 1837 | ---------------------------------------- 1838 | 1839 | 1840 | ```java language 1841 | public class MyClass { 1842 | public static void main(String[] args) { 1843 | double a, b; 1844 | a = -2.5; 1845 | b = Math.abs(a); 1846 | System.out.printf("|%.2f| = %.2f\n", a, b); 1847 | } 1848 | } 1849 | 1850 | ``` 1851 | ---------------------------------------- 1852 | 1853 | Solution: 1854 | 1855 | ```java language 1856 | |-2.50| = 2.50 1857 | 1858 | ``` 1859 | 1860 | ---------------------------------------- 1861 | 1862 | ```java language 1863 | public class MyClass { 1864 | public static void main(String[] args) { 1865 | int x = 12, y = 3; 1866 | System.out.println(Math.abs(-x - y)); 1867 | } 1868 | } 1869 | 1870 | ``` 1871 | ---------------------------------------- 1872 | 1873 | Solution: 1874 | 1875 | ```java language 1876 | 15 1877 | 1878 | ``` 1879 | 1880 | ---------------------------------------- 1881 | 1882 | ```java language 1883 | public class MyClass { 1884 | public static void main(String[] args) { 1885 | int x = 12, y = 3; 1886 | System.out.println(-(-x - y)); 1887 | } 1888 | } 1889 | ``` 1890 | ---------------------------------------- 1891 | 1892 | Solution: 1893 | 1894 | ```java language 1895 | 15 1896 | 1897 | ``` 1898 | 1899 | ---------------------------------------- 1900 | 1901 | ```java language 1902 | public class MyClass { 1903 | public static void main(String[] args) { 1904 | int x = 12, y = 3; 1905 | System.out.println(x - -y); 1906 | } 1907 | } 1908 | ``` 1909 | ---------------------------------------- 1910 | 1911 | Solution: 1912 | 1913 | ```java language 1914 | 15 1915 | 1916 | ``` 1917 | 1918 | ---------------------------------------- 1919 | 1920 | ```java language 1921 | public class MyClass { 1922 | static void myMethod() { 1923 | System.out.println("Anyone who has never made a mistake has never tried anything new."); 1924 | } 1925 | 1926 | public static void main(String[] args) { 1927 | myMethod(); 1928 | myMethod(); 1929 | myMethod(); 1930 | } 1931 | } 1932 | 1933 | ``` 1934 | ---------------------------------------- 1935 | 1936 | Solution: 1937 | 1938 | ```java language 1939 | Anyone who has never made a mistake has never tried anything new. 1940 | Anyone who has never made a mistake has never tried anything new. 1941 | Anyone who has never made a mistake has never tried anything new. 1942 | 1943 | ``` 1944 | 1945 | ---------------------------------------- 1946 | 1947 | 1948 | # Question 60 1949 | 1950 | ### **Question:** 1951 | 1952 | > ***Write a program to find the size of an array.*** 1953 | 1954 | ---------------------------------------- 1955 | 1956 | Solution: 1957 | 1958 | ```java language 1959 | public class MyClass { 1960 | public static void main(String[] args) { 1961 | int[] num = {11, 22, 33, 44, 55, 66}; 1962 | int n = (int)num.length; 1963 | System.out.println("Size of the array is: " + n); 1964 | } 1965 | } 1966 | ``` 1967 | ---------------------------------------- 1968 | 1969 | # Question 61 1970 | 1971 | ### **Question:** 1972 | 1973 | > ***Write a program that prints a sequence from 1 to a given integer, inserts a plus sign between these numbers, and then removes the plus sign at the end of the sequence.*** 1974 | 1975 | ---------------------------------------- 1976 | 1977 | Solution: 1978 | 1979 | ```java language 1980 | import java.util.Scanner; 1981 | 1982 | public class MyClass { 1983 | public static void main(String[] args) { 1984 | int x, i; 1985 | System.out.println("\nEnter a integer: "); 1986 | x = STDIN_SCANNER.nextInt(); 1987 | if(x > 0) { 1988 | System.out.println("Sequence from 1 to " + x + ":"); 1989 | for(i = 1; i < x; i++) { 1990 | System.out.print(i + "+"); 1991 | } 1992 | System.out.println(i); 1993 | } 1994 | } 1995 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 1996 | } 1997 | ``` 1998 | ---------------------------------------- 1999 | 2000 | # Question 62 2001 | 2002 | ### **Question:** 2003 | 2004 | > ***Write a program to verify whether a triangle's three sides form a right angled triangle or not.*** 2005 | 2006 | ---------------------------------------- 2007 | 2008 | Solution: 2009 | 2010 | ```java language 2011 | import java.util.Scanner; 2012 | 2013 | public class MyClass { 2014 | public static void main(String[] args) { 2015 | int a, b, c; 2016 | System.out.println("Enter the three sides of a triangle: "); 2017 | a = STDIN_SCANNER.nextInt(); 2018 | b = STDIN_SCANNER.nextInt(); 2019 | c = STDIN_SCANNER.nextInt(); 2020 | if(a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) { 2021 | System.out.println("Triangle's three sides form a right angled triangle."); 2022 | } else { 2023 | System.out.println("Triangle's three sides does not form a right angled triangle."); 2024 | } 2025 | } 2026 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2027 | } 2028 | ``` 2029 | ---------------------------------------- 2030 | 2031 | # Question 63 2032 | 2033 | ### **Question:** 2034 | 2035 | > ***Write a program that will find the second-largest number among the user's input of three numbers.*** 2036 | 2037 | ---------------------------------------- 2038 | 2039 | Solution: 2040 | 2041 | ```java language 2042 | import java.util.Scanner; 2043 | 2044 | public class MyClass { 2045 | public static void main(String[] args) { 2046 | int a, b, c; 2047 | System.out.print("\nEnter the first number: "); 2048 | a = STDIN_SCANNER.nextInt(); 2049 | System.out.print("\nEnter the second number: "); 2050 | b = STDIN_SCANNER.nextInt(); 2051 | System.out.print("\nEnter the third number: "); 2052 | c = STDIN_SCANNER.nextInt(); 2053 | if(a > b && a > c) { 2054 | if(b > c) { 2055 | System.out.print("\n" + b + " is second largest number among three numbers"); 2056 | } else { 2057 | System.out.print("\n" + c + " is second largest number among three numbers"); 2058 | } 2059 | } else if(b > c && b > a) { 2060 | if(c > a) { 2061 | System.out.print("\n" + c + " is second largest number among three numbers"); 2062 | } else { 2063 | System.out.print("\n" + a + " is second largest number among three numbers"); 2064 | } 2065 | } else if(a > b) { 2066 | System.out.print("\n" + a + " is second largest number among three numbers"); 2067 | } else { 2068 | System.out.print("\n" + b + " is second largest number among three numbers"); 2069 | } 2070 | } 2071 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2072 | } 2073 | ``` 2074 | ---------------------------------------- 2075 | 2076 | # Question 64 2077 | 2078 | ### **Question:** 2079 | 2080 | > ***Write a program to calculate the sum of the two given integer values. Return three times the sum of the two values if they are equal.*** 2081 | 2082 | ---------------------------------------- 2083 | 2084 | Solution: 2085 | 2086 | ```java language 2087 | public class MyClass { 2088 | public static void main(String[] args) { 2089 | System.out.print(myfunc(3, 5)); 2090 | System.out.print("\n" + myfunc(6, 6)); 2091 | } 2092 | public static int myfunc(int a, int b) { 2093 | return a == b ? (a + b) * 3 : a + b; 2094 | } 2095 | } 2096 | ``` 2097 | ---------------------------------------- 2098 | 2099 | # Question 65 2100 | 2101 | ### **Question:** 2102 | 2103 | > ***Write a program that accepts minutes as input, and display the total number of hours and minutes.*** 2104 | 2105 | ---------------------------------------- 2106 | 2107 | Solution: 2108 | 2109 | ```java language 2110 | import java.util.Scanner; 2111 | 2112 | public class MyClass { 2113 | public static void main(String[] args) { 2114 | int mins, hrs; 2115 | System.out.print("Input minutes: "); 2116 | mins = STDIN_SCANNER.nextInt(); 2117 | hrs = mins / 60; 2118 | mins = mins % 60; 2119 | System.out.println("\n" + hrs + " Hours, " + mins + " Minutes."); 2120 | } 2121 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2122 | } 2123 | ``` 2124 | ---------------------------------------- 2125 | 2126 | 2127 | # Question 66 2128 | 2129 | ### **Question:** 2130 | 2131 | > ***Write a program to determine whether a positive number entered by the user is a multiple of three or five.*** 2132 | 2133 | ---------------------------------------- 2134 | 2135 | Solution: 2136 | 2137 | ```java language 2138 | import java.util.Scanner; 2139 | 2140 | public class MyClass { 2141 | public static void main(String[] args) { 2142 | int x; 2143 | System.out.print("\nEnter a number: "); 2144 | x = STDIN_SCANNER.nextInt(); 2145 | if(x % 3 == 0 || x % 5 == 0) { 2146 | System.out.print("True"); 2147 | } else { 2148 | System.out.print("False"); 2149 | } 2150 | } 2151 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2152 | } 2153 | 2154 | ``` 2155 | ---------------------------------------- 2156 | 2157 | 2158 | # Question 67 2159 | 2160 | ### **Question:** 2161 | 2162 | > ***Write a program to verify whether one of the two entered integers falls within the range of 100 to 200 included.*** 2163 | 2164 | ---------------------------------------- 2165 | 2166 | Solution: 2167 | 2168 | ```java language 2169 | import java.util.Scanner; 2170 | 2171 | public class MyClass { 2172 | public static void main(String[] args) { 2173 | int x, y; 2174 | System.out.print("\nEnter the value for x: "); 2175 | x = STDIN_SCANNER.nextInt(); 2176 | System.out.print("\nEnter the value for y: "); 2177 | y = STDIN_SCANNER.nextInt(); 2178 | if(x >= 100 && x <= 200 || y >= 100 && y <= 200) { 2179 | System.out.print("True"); 2180 | } else { 2181 | System.out.print("False"); 2182 | } 2183 | } 2184 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2185 | } 2186 | 2187 | ``` 2188 | ---------------------------------------- 2189 | 2190 | # Question 68 2191 | 2192 | ### **Question:** 2193 | 2194 | > ***Write a program to determine which of the two given integers is closest to the value 100. If the two numbers are equal, return 0.*** 2195 | 2196 | ---------------------------------------- 2197 | 2198 | Solution: 2199 | 2200 | ```java language 2201 | public class MyClass { 2202 | public static void main(String[] args) { 2203 | System.out.print(myfunc(86, 99)); 2204 | System.out.print("\n" + myfunc(55, 55)); 2205 | System.out.print("\n" + myfunc(65, 80)); 2206 | } 2207 | 2208 | public static int myfunc(int a, int b) { 2209 | int x = Math.abs(a - 100); 2210 | int y = Math.abs(b - 100); 2211 | return x == y ? 0 : (x < y ? a : b); 2212 | } 2213 | } 2214 | ``` 2215 | ---------------------------------------- 2216 | 2217 | # Question 69 2218 | 2219 | ### **Question:** 2220 | 2221 | > ***Write a program to determine whether a positive number entered by the user is a multiple of three or five, but not both.*** 2222 | 2223 | ---------------------------------------- 2224 | 2225 | Solution: 2226 | 2227 | ```java language 2228 | import java.util.Scanner; 2229 | public class MyClass { 2230 | public static void main(String[] args) { 2231 | int x; 2232 | System.out.print("\nEnter a number: "); 2233 | x = STDIN_SCANNER.nextInt(); 2234 | if(x % 3 == 0 ^ x % 5 == 0) { 2235 | System.out.print("True"); 2236 | } else { 2237 | System.out.print("False"); 2238 | } 2239 | } 2240 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2241 | } 2242 | 2243 | ``` 2244 | ---------------------------------------- 2245 | 2246 | 2247 | # Question 70 2248 | 2249 | ### **Question:** 2250 | 2251 | > ***Write a program to determine whether two entered non-negative numbers have the same last digit.*** 2252 | 2253 | ---------------------------------------- 2254 | 2255 | Solution: 2256 | 2257 | ```java language 2258 | import java.util.Scanner; 2259 | 2260 | public class MyClass { 2261 | public static void main(String[] args) { 2262 | int x, y; 2263 | System.out.print("\nEnter the value for x: "); 2264 | x = STDIN_SCANNER.nextInt(); 2265 | System.out.print("\nEnter the value for y: "); 2266 | y = STDIN_SCANNER.nextInt(); 2267 | if(Math.abs(x % 10) == Math.abs(y % 10)) { 2268 | System.out.print("True"); 2269 | } else { 2270 | System.out.print("False"); 2271 | } 2272 | } 2273 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2274 | } 2275 | 2276 | ``` 2277 | ---------------------------------------- 2278 | 2279 | # Question 71 2280 | 2281 | ### **Question:** 2282 | 2283 | > ***Write a program to determine whether a given non-negative number is a multiple of 12 or it is one more than a multiple of 12.*** 2284 | 2285 | ---------------------------------------- 2286 | 2287 | Solution: 2288 | 2289 | ```java language 2290 | public class MyClass { 2291 | public static void main(String[] args) { 2292 | int x = 43; 2293 | if(x % 12 == 0 || x % 12 == 1) { 2294 | System.out.print("True"); 2295 | } else { 2296 | System.out.print("False"); 2297 | } 2298 | } 2299 | } 2300 | ``` 2301 | ---------------------------------------- 2302 | 2303 | 2304 | # Question 72 2305 | 2306 | ### **Question:** 2307 | 2308 | > ***Write a program that accepts two integers and returns true when one of them equals 6, or when their sum or difference equals 6.*** 2309 | 2310 | ---------------------------------------- 2311 | 2312 | Solution: 2313 | 2314 | ```java language 2315 | import java.util.Scanner; 2316 | 2317 | public class MyClass { 2318 | public static void main(String[] args) { 2319 | int x, y; 2320 | System.out.print("\nEnter the value for x: "); 2321 | x = STDIN_SCANNER.nextInt(); 2322 | System.out.print("\nEnter the value for y: "); 2323 | y = STDIN_SCANNER.nextInt(); 2324 | if(x == 6 || y == 6 || x + y == 6 || Math.abs(x - y) == 6) { 2325 | System.out.print("True"); 2326 | } else { 2327 | System.out.print("False"); 2328 | } 2329 | } 2330 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2331 | } 2332 | ``` 2333 | ---------------------------------------- 2334 | 2335 | 2336 | # Question 73 2337 | 2338 | ### **Question:** 2339 | 2340 | > ***Write a program to check whether it is possible to add two integers to get the third integer from three entered integers.*** 2341 | 2342 | ---------------------------------------- 2343 | 2344 | Solution: 2345 | 2346 | ```java language 2347 | import java.util.Scanner; 2348 | 2349 | public class MyClass { 2350 | public static void main(String[] args) { 2351 | int x, y, z; 2352 | System.out.print("\nEnter the value for x: "); 2353 | x = STDIN_SCANNER.nextInt(); 2354 | System.out.print("\nEnter the value for y: "); 2355 | y = STDIN_SCANNER.nextInt(); 2356 | System.out.print("\nEnter the value for z: "); 2357 | z = STDIN_SCANNER.nextInt(); 2358 | if(x == y + z || y == x + z || z == x + y) { 2359 | System.out.print("True"); 2360 | } else { 2361 | System.out.print("False"); 2362 | } 2363 | } 2364 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2365 | } 2366 | 2367 | ``` 2368 | ---------------------------------------- 2369 | 2370 | # Question 74 2371 | 2372 | ### **Question:** 2373 | 2374 | > ***Write a program that converts kilometers per hour to miles per hour.*** 2375 | 2376 | ---------------------------------------- 2377 | 2378 | Solution: 2379 | 2380 | ```java language 2381 | import java.util.Scanner; 2382 | 2383 | public class MyClass { 2384 | public static void main(String[] args) { 2385 | float kmph; 2386 | System.out.print("Enter kilometers per hour: "); 2387 | kmph = STDIN_SCANNER.nextFloat(); 2388 | System.out.printf("\n%f miles per hour", kmph * 0.6213712); 2389 | } 2390 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2391 | } 2392 | ``` 2393 | ---------------------------------------- 2394 | 2395 | 2396 | # Question 75 2397 | 2398 | ### **Question:** 2399 | 2400 | > ***Write a program to calculate area of an ellipse.*** 2401 | 2402 | ---------------------------------------- 2403 | 2404 | Solution: 2405 | 2406 | ```java language 2407 | import java.util.Scanner; 2408 | 2409 | public class MyClass { 2410 | public final static double PI = 3.141592; 2411 | public static void main(String[] args) { 2412 | float major, minor; 2413 | System.out.print("\nEnter length of major axis: "); 2414 | major = STDIN_SCANNER.nextFloat(); 2415 | System.out.print("\nEnter length of minor axis: "); 2416 | minor = STDIN_SCANNER.nextFloat(); 2417 | System.out.printf("\nArea of an ellipse = %.4f", PI * major * minor); 2418 | } 2419 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2420 | } 2421 | ``` 2422 | ---------------------------------------- 2423 | 2424 | # Question 76 2425 | 2426 | ### **Question:** 2427 | 2428 | > ***Write a program to calculate the sum of three given integers. Return the third value if the first two values are equal.*** 2429 | 2430 | ---------------------------------------- 2431 | 2432 | Solution: 2433 | 2434 | ```java language 2435 | public class MyClass { 2436 | public static void main(String[] args) { 2437 | System.out.print("\n" + myfunc(11, 11, 11)); 2438 | System.out.print("\n" + myfunc(11, 11, 16)); 2439 | System.out.print("\n" + myfunc(18, 15, 10)); 2440 | } 2441 | 2442 | public static int myfunc(int a, int b, int c) { 2443 | if(a == b && b == c) { 2444 | return 0; 2445 | } 2446 | if(a == b) { 2447 | return c; 2448 | } 2449 | if(a == c) { 2450 | return b; 2451 | } 2452 | if(b == c) { 2453 | return a; 2454 | } else { 2455 | return a + b + c; 2456 | } 2457 | } 2458 | } 2459 | ``` 2460 | ---------------------------------------- 2461 | 2462 | 2463 | # Question 77 2464 | 2465 | ### **Question:** 2466 | 2467 | > ***Write a program to convert bytes to kilobytes.*** 2468 | 2469 | ---------------------------------------- 2470 | 2471 | Solution: 2472 | 2473 | ```java language 2474 | import java.util.Scanner; 2475 | 2476 | public class MyClass { 2477 | public static void main(String[] args) { 2478 | double bytes; 2479 | System.out.print("\nEnter number of bytes: "); 2480 | bytes = STDIN_SCANNER.nextDouble(); 2481 | System.out.printf("\nKilobytes: %.2f", bytes / 1024); 2482 | } 2483 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2484 | } 2485 | 2486 | ``` 2487 | ---------------------------------------- 2488 | 2489 | 2490 | # Question 78 2491 | 2492 | ### **Question:** 2493 | 2494 | > ***Write a program to convert megabytes to kilobytes.*** 2495 | 2496 | ---------------------------------------- 2497 | 2498 | Solution: 2499 | 2500 | ```java language 2501 | import java.util.Scanner; 2502 | 2503 | public class MyClass { 2504 | public static void main(String[] args) { 2505 | double megabytes, kilobytes; 2506 | System.out.print("\nInput the amount of megabytes to convert: "); 2507 | megabytes = STDIN_SCANNER.nextDouble(); 2508 | kilobytes = megabytes * 1_024; 2509 | System.out.printf("\nThere are %f kilobytes in %f megabytes.", kilobytes, megabytes); 2510 | } 2511 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2512 | } 2513 | ``` 2514 | ---------------------------------------- 2515 | 2516 | 2517 | # Question 79 2518 | 2519 | ### **Question:** 2520 | 2521 | > ***Write a program to count the number of even elements in an integer array.*** 2522 | 2523 | ---------------------------------------- 2524 | 2525 | Solution: 2526 | 2527 | ```java language 2528 | import java.util.Scanner; 2529 | 2530 | public class MyClass { 2531 | public static void main(String[] args) { 2532 | int[] array = new int[1000]; 2533 | int arrSize, even = 0; 2534 | System.out.print("Input the size of the array: "); 2535 | arrSize = STDIN_SCANNER.nextInt(); 2536 | System.out.println("Enter the elements in array: "); 2537 | for(int i = 0; i < arrSize; i++) { 2538 | array[i] = STDIN_SCANNER.nextInt(); 2539 | } 2540 | 2541 | for(int i = 0; i < arrSize; i++) { 2542 | if(array[i] % 2 == 0) { 2543 | even++; 2544 | } 2545 | } 2546 | System.out.print("Number of even elements: " + even); 2547 | } 2548 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2549 | } 2550 | 2551 | ``` 2552 | ---------------------------------------- 2553 | 2554 | 2555 | # Question 80 2556 | 2557 | ### **Question:** 2558 | 2559 | > ***Write a program to count the number of odd elements in an integer array.*** 2560 | 2561 | ---------------------------------------- 2562 | 2563 | Solution: 2564 | 2565 | ```java language 2566 | import java.util.Scanner; 2567 | 2568 | public class MyClass { 2569 | public static void main(String[] args) { 2570 | int[] array = new int[1000]; 2571 | int arrSize, odd = 0; 2572 | System.out.print("Input the size of the array: "); 2573 | arrSize = STDIN_SCANNER.nextInt(); 2574 | System.out.println("Enter the elements in array: "); 2575 | for(int i = 0; i < arrSize; i++) { 2576 | array[i] = STDIN_SCANNER.nextInt(); 2577 | } 2578 | 2579 | for(int i = 0; i < arrSize; i++) { 2580 | if(array[i] % 2 != 0) { 2581 | odd++; 2582 | } 2583 | } 2584 | System.out.print("Number of odd elements: " + odd); 2585 | } 2586 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2587 | } 2588 | ``` 2589 | ---------------------------------------- 2590 | 2591 | 2592 | # Question 81 2593 | 2594 | ### **Question:** 2595 | 2596 | > ***Write a program that will accept two integers and determine whether or not they are equal.*** 2597 | 2598 | ---------------------------------------- 2599 | 2600 | Solution: 2601 | 2602 | ```java language 2603 | import java.util.Scanner; 2604 | 2605 | public class MyClass { 2606 | public static void main(String[] args) { 2607 | int x, y; 2608 | System.out.println("Input the values for x and y: "); 2609 | x = STDIN_SCANNER.nextInt(); 2610 | y = STDIN_SCANNER.nextInt(); 2611 | if(x == y) { 2612 | System.out.println("x and y are equal"); 2613 | } else { 2614 | System.out.println("x and y are not equal"); 2615 | } 2616 | } 2617 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2618 | } 2619 | ``` 2620 | ---------------------------------------- 2621 | 2622 | # Question 82 2623 | 2624 | ### **Question:** 2625 | 2626 | > ***Write a program to find the third angle of a triangle if two angles are given.*** 2627 | 2628 | --------------------------------------- 2629 | 2630 | Solution: 2631 | 2632 | ```java language 2633 | import java.util.Scanner; 2634 | 2635 | public class MyClass { 2636 | public static void main(String[] args) { 2637 | int angle1, angle2; 2638 | System.out.print("\nEnter the first angle of the triangle: "); 2639 | angle1 = STDIN_SCANNER.nextInt(); 2640 | System.out.print("\nEnter the second angle of the triangle: "); 2641 | angle2 = STDIN_SCANNER.nextInt(); 2642 | System.out.print("\nThird angle of the triangle is: " + (180 - (angle1 + angle2))); 2643 | } 2644 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2645 | } 2646 | 2647 | ``` 2648 | ---------------------------------------- 2649 | 2650 | 2651 | # Question 83 2652 | 2653 | ### **Question:** 2654 | 2655 | > ***Write a program to determine whether a particular year is a leap year or not.*** 2656 | 2657 | --------------------------------------- 2658 | 2659 | Solution: 2660 | 2661 | ```java language 2662 | import java.util.Scanner; 2663 | 2664 | public class MyClass { 2665 | public static void main(String[] args) { 2666 | int year; 2667 | System.out.print("Enter the year: "); 2668 | year = STDIN_SCANNER.nextInt(); 2669 | if(year % 400 == 0) { 2670 | System.out.print("\n" + year + " is a leap year."); 2671 | } else if(year % 100 == 0) { 2672 | System.out.print("\n" + year + " is a not leap year."); 2673 | } else if(year % 4 == 0) { 2674 | System.out.print("\n" + year + " is a leap year."); 2675 | } else { 2676 | System.out.print("\n" + year + " is not a leap year."); 2677 | } 2678 | } 2679 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2680 | } 2681 | ``` 2682 | ---------------------------------------- 2683 | 2684 | # Question 84 2685 | 2686 | ### **Question:** 2687 | 2688 | > ***Write a program that reads the candidate's age and determine a candidate's eligibility to cast his own vote.*** 2689 | 2690 | --------------------------------------- 2691 | 2692 | Solution: 2693 | 2694 | ```java language 2695 | import java.util.Scanner; 2696 | 2697 | public class MyClass { 2698 | public static void main(String[] args) { 2699 | int age; 2700 | System.out.print("\nEnter the age of the candidate: "); 2701 | age = STDIN_SCANNER.nextInt(); 2702 | if(age < 18) { 2703 | System.out.print("\nWe apologize, but the candidate is not able to cast his vote."); 2704 | System.out.print("\nAfter " + (18 - age) + " year, the candidate would be able to cast his vote."); 2705 | } else { 2706 | System.out.println("Congratulation! the candidate is qualified to cast his vote."); 2707 | } 2708 | } 2709 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2710 | } 2711 | ``` 2712 | ---------------------------------------- 2713 | 2714 | # Question 85 2715 | 2716 | ### **Question:** 2717 | 2718 | > ***Write a program to Convert Yard to Foot.*** 2719 | 2720 | --------------------------------------- 2721 | 2722 | Solution: 2723 | 2724 | ```java language 2725 | import java.util.Scanner; 2726 | 2727 | public class MyClass { 2728 | public static void main(String[] args) { 2729 | float yard; 2730 | System.out.print("\nEnter the Length in Yard : "); 2731 | yard = STDIN_SCANNER.nextFloat(); 2732 | System.out.printf("\n%f Yard in Foot is: %f", yard, 3 * yard); 2733 | } 2734 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2735 | } 2736 | ``` 2737 | ---------------------------------------- 2738 | 2739 | 2740 | # Question 86 2741 | 2742 | ### **Question:** 2743 | 2744 | > ***Write a program to convert gigabytes to megabytes.*** 2745 | 2746 | --------------------------------------- 2747 | 2748 | Solution: 2749 | 2750 | ```java language 2751 | import java.util.Scanner; 2752 | 2753 | public class MyClass { 2754 | public static void main(String[] args) { 2755 | double gigabytes, megabytes; 2756 | System.out.print("\nInput the amount of gigabytes to convert: "); 2757 | gigabytes = STDIN_SCANNER.nextDouble(); 2758 | megabytes = gigabytes * 1_024; 2759 | System.out.printf("\nThere are %f megabytes in %f gigabytes.", megabytes, gigabytes); 2760 | } 2761 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2762 | } 2763 | 2764 | ``` 2765 | ---------------------------------------- 2766 | 2767 | # Question 87 2768 | 2769 | ### **Question:** 2770 | 2771 | > ***Write a program to Convert Kilogram to Pounds.*** 2772 | 2773 | --------------------------------------- 2774 | 2775 | Solution: 2776 | 2777 | ```java language 2778 | import java.util.Scanner; 2779 | 2780 | public class MyClass { 2781 | public static void main(String[] args) { 2782 | float kg, lbs; 2783 | System.out.print("\nEnter Weight in Kilogram: "); 2784 | kg = STDIN_SCANNER.nextFloat(); 2785 | lbs = (float)(kg * 2.20462); 2786 | System.out.printf("\n%f Kg = %f Pounds", kg, lbs); 2787 | } 2788 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2789 | } 2790 | ``` 2791 | ---------------------------------------- 2792 | 2793 | 2794 | # Question 88 2795 | 2796 | ### **Question:** 2797 | 2798 | > ***Write a program to Convert Kilogram to Ounce.*** 2799 | 2800 | --------------------------------------- 2801 | 2802 | Solution: 2803 | 2804 | ```java language 2805 | import java.util.Scanner; 2806 | 2807 | public class MyClass { 2808 | public static void main(String[] args) { 2809 | float kg, ounce; 2810 | System.out.print("\nEnter Weight in Kilogram: "); 2811 | kg = STDIN_SCANNER.nextFloat(); 2812 | ounce = (float)(kg * 35.274); 2813 | System.out.printf("\n%f Kg = %f Ounce", kg, ounce); 2814 | } 2815 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2816 | } 2817 | 2818 | ``` 2819 | ---------------------------------------- 2820 | 2821 | # Question 89 2822 | 2823 | ### **Question:** 2824 | 2825 | > ***Write a program to Convert Pounds to Grams.*** 2826 | 2827 | --------------------------------------- 2828 | 2829 | Solution: 2830 | 2831 | ```java language 2832 | import java.util.Scanner; 2833 | 2834 | public class MyClass { 2835 | public static void main(String[] args) { 2836 | float pound, gram; 2837 | System.out.print("\nEnter Weight in Pounds: "); 2838 | pound = STDIN_SCANNER.nextFloat(); 2839 | gram = (float)(pound * 453.592); 2840 | System.out.printf("\n%f Pound = %f Grams", pound, gram); 2841 | } 2842 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2843 | } 2844 | ``` 2845 | ---------------------------------------- 2846 | 2847 | 2848 | # Question 90 2849 | 2850 | ### **Question:** 2851 | 2852 | > ***Write a program to verify whether a triangle is valid or not using angles.*** 2853 | 2854 | --------------------------------------- 2855 | 2856 | Solution: 2857 | 2858 | ```java language 2859 | import java.util.Scanner; 2860 | 2861 | public class MyClass { 2862 | public static void main(String[] args) { 2863 | int angle1, angle2, angle3, sum; 2864 | System.out.print("\nEnter the first angle of the triangle: "); 2865 | angle1 = STDIN_SCANNER.nextInt(); 2866 | System.out.print("\nEnter the second angle of the triangle: "); 2867 | angle2 = STDIN_SCANNER.nextInt(); 2868 | System.out.print("\nEnter the third angle of the triangle: "); 2869 | angle3 = STDIN_SCANNER.nextInt(); 2870 | sum = angle1 + angle2 + angle3; 2871 | if(sum == 180) { 2872 | System.out.print("\nThe triangle is valid."); 2873 | } else { 2874 | System.out.print("\nThe triangle is not valid."); 2875 | } 2876 | } 2877 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2878 | } 2879 | ``` 2880 | ---------------------------------------- 2881 | 2882 | # Question 91 2883 | 2884 | ### **Question:** 2885 | 2886 | > ***Write a program to add the digits of a two-digit number that is entered by the user.*** 2887 | 2888 | --------------------------------------- 2889 | 2890 | Solution: 2891 | 2892 | ```java language 2893 | import java.util.Scanner; 2894 | 2895 | public class MyClass { 2896 | public static void main(String[] args) { 2897 | int x, y, sum = 0; 2898 | System.out.print("\nEnter a two-digit number: "); 2899 | x = STDIN_SCANNER.nextInt(); 2900 | y = x; 2901 | while(y != 0) { 2902 | sum = sum + y % 10; 2903 | y = y / 10; 2904 | } 2905 | System.out.print("\nSum of digits of " + x + " is: " + sum); 2906 | } 2907 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2908 | } 2909 | 2910 | ``` 2911 | ---------------------------------------- 2912 | 2913 | 2914 | # Question 92 2915 | 2916 | ### **Question:** 2917 | 2918 | > ***Write a program to verify if a character you entered is a vowel or a consonant.*** 2919 | 2920 | --------------------------------------- 2921 | 2922 | Solution: 2923 | 2924 | ```java language 2925 | import java.util.Scanner; 2926 | 2927 | public class MyClass { 2928 | public static void main(String[] args) { 2929 | Scanner scanner = new Scanner(System.in); 2930 | System.out.println("Enter a alphabet: "); 2931 | char ch = scanner.next().charAt(0); 2932 | if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || 2933 | ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' ) { 2934 | System.out.println(ch + " is vowel"); 2935 | } 2936 | else { 2937 | System.out.println(ch + " is consonant"); 2938 | } 2939 | } 2940 | } 2941 | 2942 | 2943 | ``` 2944 | ---------------------------------------- 2945 | 2946 | 2947 | # Question 93 2948 | 2949 | ### **Question:** 2950 | 2951 | > ***Write a program to find factorial of a number.*** 2952 | 2953 | --------------------------------------- 2954 | 2955 | Solution: 2956 | 2957 | ```java language 2958 | import java.util.Scanner; 2959 | 2960 | public class MyClass { 2961 | public static void main(String[] args) { 2962 | int fact = 1, num; 2963 | System.out.print("\nEnter a number: "); 2964 | num = STDIN_SCANNER.nextInt(); 2965 | for(int i = 1; i <= num; i++) { 2966 | fact = fact * i; 2967 | } 2968 | System.out.print("\nFactorial of " + num + " is: " + fact); 2969 | } 2970 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 2971 | } 2972 | ``` 2973 | ---------------------------------------- 2974 | 2975 | # Question 94 2976 | 2977 | ### **Question:** 2978 | 2979 | > ***Write a program to print number of days in a month.*** 2980 | 2981 | --------------------------------------- 2982 | 2983 | Solution: 2984 | 2985 | ```java language 2986 | import java.util.Scanner; 2987 | 2988 | public class MyClass { 2989 | public static void main(String[] args) { 2990 | int[] x = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 2991 | int m; 2992 | System.out.print("\nEnter the month number: "); 2993 | m = STDIN_SCANNER.nextInt(); 2994 | if(m > 12 || m < 1) { 2995 | System.out.print("Invalid input"); 2996 | } else if(m == 2) { 2997 | System.out.print("\nNumber of days in month 2 is either 29 or 28"); 2998 | } else { 2999 | System.out.print("\nNumber of days in month " + m + " is " + x[m - 1]); 3000 | } 3001 | } 3002 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3003 | } 3004 | ``` 3005 | ---------------------------------------- 3006 | 3007 | 3008 | # Question 95 3009 | 3010 | ### **Question:** 3011 | 3012 | > ***Write a program to concatenate multiple strings.*** 3013 | 3014 | --------------------------------------- 3015 | 3016 | Solution: 3017 | 3018 | ```java language 3019 | public class MyClass { 3020 | public static void main(String[] args) { 3021 | String x = "Stephen"; 3022 | String y = "-William"; 3023 | String z = "-Hawking"; 3024 | String c = x.concat(y).concat(z); 3025 | System.out.println(c); 3026 | } 3027 | } 3028 | ``` 3029 | ---------------------------------------- 3030 | 3031 | 3032 | # Question 96 3033 | 3034 | ### **Question:** 3035 | 3036 | > ***Write a program to find maximum between two numbers.*** 3037 | 3038 | --------------------------------------- 3039 | 3040 | Solution: 3041 | 3042 | ```java language 3043 | import java.util.Scanner; 3044 | 3045 | public class MyClass { 3046 | public static void main(String[] args) { 3047 | int a, b; 3048 | System.out.println("Enter two numbers: "); 3049 | a = STDIN_SCANNER.nextInt(); 3050 | b = STDIN_SCANNER.nextInt(); 3051 | if(a > b) { 3052 | System.out.print("\n" + a + " is a maximum number"); 3053 | } else { 3054 | System.out.print("\n" + b + " is a maximum number"); 3055 | } 3056 | } 3057 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3058 | } 3059 | ``` 3060 | 3061 | 3062 | ```java language 3063 | public class MyClass { 3064 | public static void main(String args[]) { 3065 | double a = 15.143; 3066 | double b = 15.656; 3067 | System.out.println(Math.max(a, b)); 3068 | } 3069 | } 3070 | 3071 | ``` 3072 | 3073 | ---------------------------------------- 3074 | 3075 | 3076 | 3077 | # Question 97 3078 | 3079 | ### **Question:** 3080 | 3081 | > ***Write a program to compare two strings.*** 3082 | 3083 | --------------------------------------- 3084 | 3085 | Solution: 3086 | 3087 | ```java language 3088 | 3089 | public class MyClass { 3090 | public static void main(String[] args) { 3091 | String x = "Albert"; 3092 | String y = "Albert"; 3093 | if(x == y) { 3094 | System.out.println("The 2 strings are equal."); 3095 | } 3096 | else { 3097 | System.out.println("The 2 strings are not equal."); 3098 | } 3099 | } 3100 | } 3101 | ``` 3102 | ---------------------------------------- 3103 | 3104 | 3105 | ```java language 3106 | 3107 | public class MyClass { 3108 | public static void main(String[] args) { 3109 | String x = "Albert"; 3110 | String y = "Albert"; 3111 | if(x.equals(y)) { 3112 | System.out.println("The 2 strings are equal."); 3113 | } 3114 | else { 3115 | System.out.println("The 2 strings are not equal."); 3116 | } 3117 | } 3118 | } 3119 | ``` 3120 | ---------------------------------------- 3121 | 3122 | # Question 98 3123 | 3124 | ### **Question:** 3125 | 3126 | > ***Write a program to convert the upper case string to lower case string.*** 3127 | 3128 | --------------------------------------- 3129 | 3130 | Solution: 3131 | 3132 | ```java language 3133 | public class MyClass { 3134 | public static void main(String args[]) { 3135 | String x = new String("ALBERT EINSTEIN"); 3136 | System.out.println(x.toLowerCase()); 3137 | } 3138 | } 3139 | 3140 | ``` 3141 | ---------------------------------------- 3142 | 3143 | 3144 | # Question 99 3145 | 3146 | ### **Question:** 3147 | 3148 | > ***Write a program to find the quotient and remainder of a entered dividend and divisor.*** 3149 | 3150 | --------------------------------------- 3151 | 3152 | Solution: 3153 | 3154 | ```java language 3155 | import java.util.Scanner; 3156 | 3157 | public class MyClass { 3158 | public static void main(String[] args) { 3159 | int dividend, divisor; 3160 | System.out.print("\nEnter dividend: "); 3161 | dividend = STDIN_SCANNER.nextInt(); 3162 | System.out.print("\nEnter divisor: "); 3163 | divisor = STDIN_SCANNER.nextInt(); 3164 | System.out.println("\nQuotient = " + (dividend / divisor)); 3165 | System.out.print("\nRemainder = " + (dividend % divisor)); 3166 | } 3167 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3168 | } 3169 | ``` 3170 | ---------------------------------------- 3171 | 3172 | # Question 100 3173 | 3174 | ### **Question:** 3175 | 3176 | > ***Write a program to determine the Size of int, float, double and char.*** 3177 | 3178 | --------------------------------------- 3179 | 3180 | Solution: 3181 | 3182 | ```java language 3183 | public class MyClass { 3184 | public static void main (String[] args) { 3185 | System.out.println("Size of int is: " + (Integer.SIZE/8) + " bytes."); 3186 | System.out.println("Size of char is: " + (Character.SIZE/8) + " bytes."); 3187 | System.out.println("Size of float is: " + (Float.SIZE/8) + " bytes."); 3188 | System.out.println("Size of double is: " + (Double.SIZE/8) + " bytes."); 3189 | } 3190 | } 3191 | ``` 3192 | ---------------------------------------- 3193 | 3194 | 3195 | # Question 101 3196 | 3197 | ### **Question:** 3198 | 3199 | > ***Write a program to promt user for 4 times password check.*** 3200 | 3201 | --------------------------------------- 3202 | 3203 | Solution: 3204 | 3205 | ```java language 3206 | import java.util.Scanner; 3207 | 3208 | public class MyClass { 3209 | public static void main(String[] args) { 3210 | String password = "123"; 3211 | String inputPass; 3212 | Scanner input = new Scanner(System.in); 3213 | System.out.println("Enter Your Password: "); 3214 | inputPass = input.nextLine(); 3215 | if (inputPass.equals(password)) { 3216 | System.out.println("Welcome User!"); 3217 | } 3218 | else { 3219 | for(int i = 0; i < 3; i++) { 3220 | System.out.println("Enter Your Password:"); 3221 | inputPass = input.nextLine(); 3222 | } 3223 | System.out.println("Access Denied! Try again"); 3224 | } 3225 | } 3226 | } 3227 | 3228 | ``` 3229 | ---------------------------------------- 3230 | 3231 | 3232 | # Question 102 3233 | 3234 | ### **Question:** 3235 | 3236 | > ***Write a program to find absolute value of a number.*** 3237 | 3238 | --------------------------------------- 3239 | 3240 | Solution: 3241 | 3242 | ```java language 3243 | import java.util.Scanner; 3244 | 3245 | public class MyClass { 3246 | public static void main(String[] args) { 3247 | int num; 3248 | System.out.println("Input a positive or negative number: "); 3249 | num = STDIN_SCANNER.nextInt(); 3250 | System.out.println("\nAbsolute value of |" + num + "| is " + Math.abs(num)); 3251 | } 3252 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3253 | } 3254 | ``` 3255 | 3256 | 3257 | ```java language 3258 | public class MyClass { 3259 | public static void main(String args[]) { 3260 | int x = 820; 3261 | int y = -985; 3262 | float z = -8.1f; 3263 | System.out.printf( "Absolute Value of x: %d \n", Math.abs(x) ); 3264 | System.out.printf( "Absolute Value of y: %d \n", Math.abs(y) ); 3265 | System.out.printf( "Absolute Value of z: %f \n", Math.abs(z) ); 3266 | } 3267 | } 3268 | ``` 3269 | ---------------------------------------- 3270 | 3271 | 3272 | # Question 103 3273 | 3274 | ### **Question:** 3275 | 3276 | > ***Write a program that will accept a person's height in cm and classify the person based on it.*** 3277 | 3278 | --------------------------------------- 3279 | 3280 | Solution: 3281 | 3282 | ```java language 3283 | import java.util.Scanner; 3284 | 3285 | public class MyClass { 3286 | public static void main(String[] args) { 3287 | float ht; 3288 | System.out.print("\nEnter the height (in cm): "); 3289 | ht = STDIN_SCANNER.nextFloat(); 3290 | if(ht < 150.0) { 3291 | System.out.println("Dwarf."); 3292 | } else if(ht >= 150.0 && ht < 165.0) { 3293 | System.out.println("Average Height."); 3294 | } else if(ht >= 165.0 && ht <= 195.0) { 3295 | System.out.println("Taller."); 3296 | } else { 3297 | System.out.println("Abnormal height."); 3298 | } 3299 | } 3300 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3301 | } 3302 | ``` 3303 | ---------------------------------------- 3304 | 3305 | 3306 | # Question 104 3307 | 3308 | ### **Question:** 3309 | 3310 | > ***Write a program to calculate the area of different geometric shapes using switch statements.*** 3311 | 3312 | --------------------------------------- 3313 | 3314 | Solution: 3315 | 3316 | ```java language 3317 | import java.util.Scanner; 3318 | 3319 | public class MyClass { 3320 | public static void main(String[] args) { 3321 | int choice; 3322 | float r, l, w, b, h; 3323 | System.out.print("\nEnter 1 for area of circle: "); 3324 | System.out.print("\nEnter 2 for area of rectangle: "); 3325 | System.out.print("\nEnter 3 for area of triangle: "); 3326 | System.out.print("\nEnter your choice : "); 3327 | choice = STDIN_SCANNER.nextInt(); 3328 | 3329 | switch(choice) { 3330 | case 1: 3331 | System.out.print("Enter the radius of the circle: "); 3332 | r = STDIN_SCANNER.nextFloat(); 3333 | System.out.printf("\nArea of a circle is: %f", 3.14 * r * r); 3334 | break; 3335 | case 2: 3336 | System.out.println("Enter the length and width of the rectangle: "); 3337 | l = STDIN_SCANNER.nextFloat(); 3338 | w = STDIN_SCANNER.nextFloat(); 3339 | System.out.printf("\nArea of a rectangle is: %f", l * w); 3340 | break; 3341 | case 3: 3342 | System.out.println("Enter the base and height of the triangle: "); 3343 | b = STDIN_SCANNER.nextFloat(); 3344 | h = STDIN_SCANNER.nextFloat(); 3345 | System.out.printf("\nArea of a triangle is: %f", 0.5 * b * h); 3346 | break; 3347 | default: 3348 | System.out.print("\nPlease enter a number from 1 to 3."); 3349 | break; 3350 | } 3351 | } 3352 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3353 | } 3354 | ``` 3355 | ---------------------------------------- 3356 | 3357 | # Question 105 3358 | 3359 | ### **Question:** 3360 | 3361 | > ***Write a program to accept a character from the keyboard and print "Yes" if it is equal to y. Otherwise print "No".*** 3362 | 3363 | --------------------------------------- 3364 | 3365 | Solution: 3366 | 3367 | ```java language 3368 | public class MyClass { 3369 | public static void main(String[] args) throws Exception { 3370 | char ch; 3371 | System.out.println("Enter a character: "); 3372 | ch = (char)System.in.read(); 3373 | if(ch == 'y' || ch == 'Y') { 3374 | System.out.println("Yes\n"); 3375 | } 3376 | else { 3377 | System.out.println("No\n"); 3378 | } 3379 | } 3380 | } 3381 | 3382 | ``` 3383 | ---------------------------------------- 3384 | 3385 | # Question 106 3386 | 3387 | ### **Question:** 3388 | 3389 | > ***Write a program that uses bitwise operators to multiply an entered value by four.*** 3390 | 3391 | --------------------------------------- 3392 | 3393 | Solution: 3394 | 3395 | ```java language 3396 | import java.util.Scanner; 3397 | 3398 | public class MyClass { 3399 | public static void main(String[] args) { 3400 | long x, y; 3401 | System.out.print("Enter a integer: "); 3402 | x = STDIN_SCANNER.nextLong(); 3403 | y = x; 3404 | x = x << 2; 3405 | System.out.println(y + " x 4 = " + x); 3406 | } 3407 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3408 | } 3409 | ``` 3410 | ---------------------------------------- 3411 | 3412 | # Question 107 3413 | 3414 | ### **Question:** 3415 | 3416 | > ***Write a program to check whether a number entered by the user is power of 2 or not.*** 3417 | 3418 | --------------------------------------- 3419 | 3420 | Solution: 3421 | 3422 | ```java language 3423 | import java.util.Scanner; 3424 | 3425 | public class MyClass { 3426 | public static void main(String[] args) { 3427 | int x; 3428 | System.out.print("Enter a number: "); 3429 | x = STDIN_SCANNER.nextInt(); 3430 | if(x != 0 && (x & x - 1) == 0) { 3431 | System.out.print("\n" + x + " is a power of 2"); 3432 | } else { 3433 | System.out.print("\n" + x + " is not a power of 2"); 3434 | } 3435 | } 3436 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3437 | } 3438 | 3439 | ``` 3440 | ---------------------------------------- 3441 | 3442 | 3443 | # Question 108 3444 | 3445 | ### **Question:** 3446 | 3447 | > ***Write a program to determine whether a triangle is scalene, isosceles, or equilateral.*** 3448 | 3449 | --------------------------------------- 3450 | 3451 | Solution: 3452 | 3453 | ```java language 3454 | 3455 | import java.util.Scanner; 3456 | 3457 | public class MyClass { 3458 | public static void main(String[] args) { 3459 | int side1, side2, side3; 3460 | System.out.print("\nEnter the first side of the triangle: "); 3461 | side1 = STDIN_SCANNER.nextInt(); 3462 | System.out.print("\nEnter the second side of the triangle: "); 3463 | side2 = STDIN_SCANNER.nextInt(); 3464 | System.out.print("\nEnter the third side of the triangle: "); 3465 | side3 = STDIN_SCANNER.nextInt(); 3466 | if(side1 == side2 && side2 == side3) { 3467 | System.out.print("\nThe given Triangle is equilateral."); 3468 | } else if(side1 == side2 || side2 == side3 || side3 == side1) { 3469 | System.out.print("\nThe given Triangle is isosceles."); 3470 | } else { 3471 | System.out.print("\nThe given Triangle is scalene."); 3472 | } 3473 | } 3474 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3475 | } 3476 | 3477 | ``` 3478 | ---------------------------------------- 3479 | 3480 | 3481 | # Question 109 3482 | 3483 | ### **Question:** 3484 | 3485 | > ***Write a program to print ASCII values of all the letters of the English alphabet from A to Z.*** 3486 | 3487 | --------------------------------------- 3488 | 3489 | Solution: 3490 | 3491 | ```java language 3492 | public class MyClass { 3493 | public static void main(String[] args) { 3494 | for(int i = 'A'; i <= 'Z'; i++) { 3495 | System.out.println("ASCII value of " + ((char)Byte.toUnsignedInt((byte)i)) + " = " + i); 3496 | } 3497 | } 3498 | } 3499 | ``` 3500 | ---------------------------------------- 3501 | 3502 | 3503 | # Question 110 3504 | 3505 | ### **Question:** 3506 | 3507 | > ***Write a program to find sum of even numbers between 1 to n.*** 3508 | 3509 | --------------------------------------- 3510 | 3511 | Solution: 3512 | 3513 | ```java language 3514 | import java.util.Scanner; 3515 | 3516 | public class MyClass { 3517 | public static void main(String[] args) { 3518 | int num, sum = 0; 3519 | System.out.print("Enter a number: "); 3520 | num = STDIN_SCANNER.nextInt(); 3521 | for(int i = 2; i <= num; i = i + 2) { 3522 | sum = sum + i; 3523 | } 3524 | System.out.print("\nSum of all even number between 1 to " + num + " is: " + sum); 3525 | } 3526 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3527 | } 3528 | ``` 3529 | ---------------------------------------- 3530 | 3531 | # Question 111 3532 | 3533 | ### **Question:** 3534 | 3535 | > ***Write a program to find sum of odd numbers between 1 to n.*** 3536 | 3537 | --------------------------------------- 3538 | 3539 | Solution: 3540 | 3541 | ```java language 3542 | import java.util.Scanner; 3543 | 3544 | public class MyClass { 3545 | public static void main(String[] args) { 3546 | int num, sum = 0; 3547 | System.out.print("Enter a number: "); 3548 | num = STDIN_SCANNER.nextInt(); 3549 | for(int i = 1; i <= num; i = i + 2) { 3550 | sum = sum + i; 3551 | } 3552 | System.out.print("\nSum of all odd number between 1 to " + num + " is: " + sum); 3553 | } 3554 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3555 | } 3556 | ``` 3557 | ---------------------------------------- 3558 | 3559 | # Question 112 3560 | 3561 | ### **Question:** 3562 | 3563 | > ***Write a program that accepts an integer (x) and computes the value of x+xx+xxx.*** 3564 | 3565 | --------------------------------------- 3566 | 3567 | Solution: 3568 | 3569 | ```java language 3570 | import java.util.Scanner; 3571 | public class MyClass { 3572 | public static void main(String[] args) { 3573 | int x; 3574 | Scanner in = new Scanner(System.in); 3575 | System.out.print("Enter a number: "); 3576 | x = in .nextInt(); 3577 | System.out.printf("%d + %d%d + %d%d%d\n", x, x, x, x, x, x); 3578 | } 3579 | } 3580 | 3581 | ``` 3582 | ---------------------------------------- 3583 | 3584 | # Question 113 3585 | 3586 | ### **Question:** 3587 | 3588 | > ***Write a program that allows you to enter the cost price and the selling price of a product and calculate profit or loss.*** 3589 | 3590 | --------------------------------------- 3591 | 3592 | Solution: 3593 | 3594 | ```java language 3595 | import java.util.Scanner; 3596 | 3597 | public class MyClass { 3598 | public static void main(String[] args) { 3599 | int cp, sp; 3600 | System.out.print("\nInput Cost Price: "); 3601 | cp = STDIN_SCANNER.nextInt(); 3602 | System.out.print("\nInput Selling Price: "); 3603 | sp = STDIN_SCANNER.nextInt(); 3604 | if(sp > cp) { 3605 | System.out.print("Profit = " + (sp - cp)); 3606 | } else if(cp > sp) { 3607 | System.out.print("Loss = " + (cp - sp)); 3608 | } else { 3609 | System.out.print("No Profit No Loss."); 3610 | } 3611 | } 3612 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3613 | } 3614 | 3615 | ``` 3616 | ---------------------------------------- 3617 | 3618 | 3619 | # Question 114 3620 | 3621 | ### **Question:** 3622 | 3623 | > ***Write a program that display the pattern like a right angle triangle using an asterisk.*** 3624 | 3625 | --------------------------------------- 3626 | 3627 | Solution: 3628 | 3629 | ```java language 3630 | import java.util.Scanner; 3631 | 3632 | public class MyClass { 3633 | public static void main(String[] args) { 3634 | int rows; 3635 | System.out.print("Input the number of rows: "); 3636 | rows = STDIN_SCANNER.nextInt(); 3637 | for(int x = 1; x <= rows; x++) { 3638 | for(int y = 1; y <= x; y++) { 3639 | System.out.print("*"); 3640 | } 3641 | System.out.println(); 3642 | } 3643 | } 3644 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3645 | } 3646 | ``` 3647 | ---------------------------------------- 3648 | 3649 | 3650 | # Question 115 3651 | 3652 | ### **Question:** 3653 | 3654 | > ***Write a program that display the pattern like a right angle triangle using a number.*** 3655 | 3656 | --------------------------------------- 3657 | 3658 | Solution: 3659 | 3660 | ```java language 3661 | import java.util.Scanner; 3662 | 3663 | public class MyClass { 3664 | public static void main(String[] args) { 3665 | int rows; 3666 | System.out.print("Input the number of rows: "); 3667 | rows = STDIN_SCANNER.nextInt(); 3668 | for(int x = 1; x <= rows; x++) { 3669 | for(int y = 1; y <= x; y++) { 3670 | System.out.print(y); 3671 | } 3672 | System.out.println(); 3673 | } 3674 | } 3675 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3676 | } 3677 | 3678 | ``` 3679 | ---------------------------------------- 3680 | 3681 | 3682 | # Question 116 3683 | 3684 | ### **Question:** 3685 | 3686 | > ***Write a program to determine the number and sum of all integers between 50 and 100 which are divisible by 2.*** 3687 | 3688 | --------------------------------------- 3689 | 3690 | Solution: 3691 | 3692 | ```java language 3693 | public class MyClass { 3694 | public static void main(String[] args) { 3695 | int sum = 0; 3696 | System.out.println("Numbers between 50 and 100, divisible by 2: "); 3697 | for(int x = 51; x < 100; x++) { 3698 | if(x % 2 == 0) { 3699 | System.out.printf("%5d", x); 3700 | sum += x; 3701 | } 3702 | } 3703 | System.out.print("\nThe sum: " + sum); 3704 | } 3705 | } 3706 | ``` 3707 | ---------------------------------------- 3708 | 3709 | # Question 117 3710 | 3711 | ### **Question:** 3712 | 3713 | > ***Write a program that uses the function to determine whether a entered number is even or odd.*** 3714 | 3715 | --------------------------------------- 3716 | 3717 | Solution: 3718 | 3719 | ```java language 3720 | import java.util.Scanner; 3721 | 3722 | public class MyClass { 3723 | public static int myfunc(int x) { 3724 | return x & 1; 3725 | } 3726 | 3727 | public static void main(String[] args) { 3728 | int x; 3729 | System.out.print("Enter any number: "); 3730 | x = STDIN_SCANNER.nextInt(); 3731 | if(myfunc(x) != 0) { 3732 | System.out.print("\nThe number you entered is odd."); 3733 | } else { 3734 | System.out.print("\nThe number you entered is even."); 3735 | } 3736 | } 3737 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3738 | } 3739 | 3740 | ``` 3741 | ---------------------------------------- 3742 | 3743 | 3744 | # Question 118 3745 | 3746 | ### **Question:** 3747 | 3748 | > ***Write a program to find square root of a entered number.*** 3749 | 3750 | --------------------------------------- 3751 | 3752 | Solution: 3753 | 3754 | ```java language 3755 | import java.util.Scanner; 3756 | 3757 | public class MyClass { 3758 | public static void main(String[] args) { 3759 | int x; 3760 | System.out.print("Enter any number: "); 3761 | x = STDIN_SCANNER.nextInt(); 3762 | System.out.printf("Square root of %d is %.2f", x, Math.sqrt(x)); 3763 | } 3764 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3765 | } 3766 | ``` 3767 | ---------------------------------------- 3768 | 3769 | # Question 119 3770 | 3771 | ### **Question:** 3772 | 3773 | > ***Write a program to find power of a entered number using library function.*** 3774 | 3775 | --------------------------------------- 3776 | 3777 | Solution: 3778 | 3779 | ```java language 3780 | import java.util.Scanner; 3781 | 3782 | public class MyClass { 3783 | public static void main(String[] args) { 3784 | int x, y; 3785 | System.out.print("\nEnter the value for x: "); 3786 | x = STDIN_SCANNER.nextInt(); 3787 | System.out.print("\nEnter the value for y: "); 3788 | y = STDIN_SCANNER.nextInt(); 3789 | System.out.print("\n" + x + "^" + y + " = " + ((long)Math.pow(x, y))); 3790 | } 3791 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 3792 | } 3793 | ``` 3794 | ---------------------------------------- 3795 | 3796 | 3797 | # Question 120 3798 | 3799 | ### **Question:** 3800 | 3801 | > ***Write a program to read 10 numbers from the keyboard and find their sum and average.*** 3802 | 3803 | --------------------------------------- 3804 | 3805 | Solution: 3806 | 3807 | ```java language 3808 | import java.util.Scanner; 3809 | public class MyClass { 3810 | public static void main(String [] args) { 3811 | int N1, N2, N3, N4, N5, N6, N7, N8, N9, N10, sum; 3812 | float X; 3813 | Scanner scan = new Scanner(System.in); 3814 | System.out.println("Enter any ten Numbers: "); 3815 | N1 = scan.nextInt(); 3816 | N2 = scan.nextInt(); 3817 | N3 = scan.nextInt(); 3818 | N4 = scan.nextInt(); 3819 | N5 = scan.nextInt(); 3820 | N6 = scan.nextInt(); 3821 | N7 = scan.nextInt(); 3822 | N8 = scan.nextInt(); 3823 | N9 = scan.nextInt(); 3824 | N10 = scan.nextInt(); 3825 | sum = N1 + N2 + N3 + N4 + N5 + N6 + N7 + N8 + N9 + N10; 3826 | X = sum /10; 3827 | System.out.println("The sum of 10 numbers = " + sum); 3828 | System.out.println("The average of 10 numbers = " + X); 3829 | } 3830 | } 3831 | 3832 | ``` 3833 | ---------------------------------------- 3834 | 3835 | 3836 | # Question 121 3837 | 3838 | ### **Question:** 3839 | 3840 | > ***Write a program to determine whether the given character is an alphanumeric character or not.*** 3841 | 3842 | --------------------------------------- 3843 | 3844 | Solution: 3845 | 3846 | ```java language 3847 | public class MyClass { 3848 | public static void main(String[] args) { 3849 | String x="abc123", y="abc.com"; 3850 | System.out.println(x.matches("[a-zA-Z0-9]+")); 3851 | System.out.println(y.matches("[a-zA-Z0-9]+")); 3852 | } 3853 | } 3854 | 3855 | 3856 | ``` 3857 | ---------------------------------------- 3858 | 3859 | # Question 122 3860 | 3861 | ### **Question:** 3862 | 3863 | > ***Write a program to illustrate try-catch statement.*** 3864 | 3865 | --------------------------------------- 3866 | 3867 | Solution: 3868 | 3869 | ```java language 3870 | public class MyClass { 3871 | public static void main(String[] args) { 3872 | try { 3873 | int[] num = {1, 2, 3}; 3874 | System.out.println(num[3]); 3875 | } catch (Exception e) { 3876 | System.out.println("Something went wrong."); 3877 | } 3878 | } 3879 | } 3880 | ``` 3881 | ---------------------------------------- 3882 | 3883 | 3884 | # Question 123 3885 | 3886 | ### **Question:** 3887 | 3888 | > ***Write a program to remove all whitespaces from a given string.*** 3889 | 3890 | --------------------------------------- 3891 | 3892 | Solution: 3893 | 3894 | ```java language 3895 | public class MyClass { 3896 | public static void main(String[] args) { 3897 | String x = "T his is b ett er."; 3898 | x = x.replaceAll("\\s", ""); 3899 | System.out.println(x); 3900 | } 3901 | } 3902 | ``` 3903 | ---------------------------------------- 3904 | 3905 | 3906 | # Question 124 3907 | 3908 | ### **Question:** 3909 | 3910 | > ***Write a program to get current working directory.*** 3911 | 3912 | --------------------------------------- 3913 | 3914 | Solution: 3915 | 3916 | ```java language 3917 | public class MyClass { 3918 | public static void main(String[] args) { 3919 | String path = System.getProperty("user.dir"); 3920 | System.out.println("Current Working Directory: " + path); 3921 | } 3922 | } 3923 | 3924 | ``` 3925 | ---------------------------------------- 3926 | 3927 | 3928 | # Question 125 3929 | 3930 | ### **Question:** 3931 | 3932 | > ***Write a program to split a sentence into words.*** 3933 | 3934 | --------------------------------------- 3935 | 3936 | Solution: 3937 | 3938 | ```java language 3939 | public class MyClass { 3940 | public static void main(String[] args) { 3941 | String x = "Hai this is Alan"; 3942 | String [] y = x. split(" ", 3); 3943 | for(String i : y) 3944 | System. out. println(i); 3945 | } 3946 | } 3947 | 3948 | ``` 3949 | ---------------------------------------- 3950 | 3951 | # Question 126 3952 | 3953 | ### **Question:** 3954 | 3955 | > ***Write a program to replace all occurrences of 'a' to 'e' in a string.*** 3956 | 3957 | --------------------------------------- 3958 | 3959 | Solution: 3960 | 3961 | ```java language 3962 | public class MyClass { 3963 | public static void main(String args[]){ 3964 | String x="Java is a powerful general-purpose programming language."; 3965 | String replaceString=x.replace('a','e'); 3966 | System.out.println(replaceString); 3967 | } 3968 | } 3969 | 3970 | ``` 3971 | ---------------------------------------- 3972 | 3973 | # Question 127 3974 | 3975 | ### **Question:** 3976 | 3977 | > ***Write a program to check if the given string is empty or not.*** 3978 | 3979 | --------------------------------------- 3980 | 3981 | Solution: 3982 | 3983 | ```java language 3984 | public class MyClass { 3985 | public static void main(String[] args) { 3986 | String a=""; 3987 | String b="Java"; 3988 | System.out.println(a.isEmpty()); 3989 | System.out.println(b.isEmpty()); 3990 | } 3991 | } 3992 | ``` 3993 | ---------------------------------------- 3994 | 3995 | 3996 | # Question 128 3997 | 3998 | ### **Question:** 3999 | 4000 | > ***Write a program to illustrate .join() method.*** 4001 | 4002 | --------------------------------------- 4003 | 4004 | Solution: 4005 | 4006 | ```java language 4007 | public class MyClass { 4008 | public static void main(String[] args) { 4009 | String a=String.join("-","Java","Programming"); 4010 | System.out.println(a); 4011 | } 4012 | } 4013 | ``` 4014 | ---------------------------------------- 4015 | 4016 | 4017 | 4018 | # Question 129 4019 | 4020 | ### **Question:** 4021 | 4022 | > ***Write a program to calculate surface area of cube.*** 4023 | 4024 | --------------------------------------- 4025 | 4026 | Solution: 4027 | 4028 | ```java language 4029 | import java.util.Scanner; 4030 | 4031 | public class MyClass { 4032 | public static void main(String[] args) { 4033 | int side; 4034 | long area; 4035 | System.out.print("\nEnter the side of cube: "); 4036 | side = STDIN_SCANNER.nextInt(); 4037 | area = 6 * side * side; 4038 | System.out.print("\nThe surface area of cube is: " + area); 4039 | } 4040 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 4041 | } 4042 | ``` 4043 | ---------------------------------------- 4044 | 4045 | 4046 | # Question 130 4047 | 4048 | ### **Question:** 4049 | 4050 | > ***Write a program to subtract 2 numbers without using subtraction operator.*** 4051 | 4052 | --------------------------------------- 4053 | 4054 | Solution: 4055 | 4056 | ```java language 4057 | public class MyClass { 4058 | public static void main(String[] args) { 4059 | int x = 6, y = 3; 4060 | System.out.print(x + ~y + 1); 4061 | } 4062 | } 4063 | ``` 4064 | ---------------------------------------- 4065 | 4066 | 4067 | # Question 131 4068 | 4069 | ### **Question:** 4070 | 4071 | > ***Write a program to add 2 numbers without using addition operator.*** 4072 | 4073 | --------------------------------------- 4074 | 4075 | Solution: 4076 | 4077 | ```java language 4078 | public class MyClass { 4079 | public static void main(String[] args) { 4080 | int x = 6, y = 3; 4081 | System.out.print(x - ~y - 1); 4082 | } 4083 | } 4084 | ``` 4085 | ---------------------------------------- 4086 | 4087 | # Question 132 4088 | 4089 | ### **Question:** 4090 | 4091 | > ***Write a program to multiply a number by 2 without using multiplication operator.*** 4092 | 4093 | --------------------------------------- 4094 | 4095 | Solution: 4096 | 4097 | ```java language 4098 | public class MyClass { 4099 | public static void main(String[] args) { 4100 | int x = 2; 4101 | System.out.print(x << 1); 4102 | } 4103 | } 4104 | ``` 4105 | ---------------------------------------- 4106 | 4107 | # Question 134 4108 | 4109 | ### **Question:** 4110 | 4111 | > ***Write a program to divide a number by 2 without using division operator.*** 4112 | 4113 | --------------------------------------- 4114 | 4115 | Solution: 4116 | 4117 | ```java language 4118 | public class MyClass { 4119 | public static void main(String[] args) { 4120 | int x = 12; 4121 | System.out.print(x >> 1); 4122 | } 4123 | } 4124 | ``` 4125 | ---------------------------------------- 4126 | 4127 | # Question 135 4128 | 4129 | ### **Question:** 4130 | 4131 | > ***Write a program to calculate volume of sphere.*** 4132 | 4133 | --------------------------------------- 4134 | 4135 | Solution: 4136 | 4137 | ```java language 4138 | import java.util.Scanner; 4139 | 4140 | public class MyClass { 4141 | public static void main(String[] args) { 4142 | int radius; 4143 | float PI = 3.141592f; 4144 | System.out.print("\nEnter the radius of sphere: "); 4145 | radius = STDIN_SCANNER.nextInt(); 4146 | float volume = (4 / 3) * (PI * radius * radius * radius); 4147 | System.out.printf("\nThe volume of sphere is: %f", volume); 4148 | } 4149 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 4150 | } 4151 | ``` 4152 | ---------------------------------------- 4153 | 4154 | 4155 | # Question 136 4156 | 4157 | ### **Question:** 4158 | 4159 | > ***Write a program to calculate volume of ellipsoid.*** 4160 | 4161 | --------------------------------------- 4162 | 4163 | Solution: 4164 | 4165 | ```java language 4166 | import java.util.Scanner; 4167 | 4168 | public class MyClass { 4169 | public static void main(String[] args) { 4170 | int r1, r2, r3; 4171 | float PI = 3.141592f; 4172 | System.out.print("\nEnter the radius of the ellipsoid of axis 1: "); 4173 | r1 = STDIN_SCANNER.nextInt(); 4174 | System.out.print("\nEnter the radius of the ellipsoid of axis 2: "); 4175 | r2 = STDIN_SCANNER.nextInt(); 4176 | System.out.print("\nEnter the radius of the ellipsoid of axis 3: "); 4177 | r3 = STDIN_SCANNER.nextInt(); 4178 | float volume = (4 / 3) * (PI * r1 * r2 * r3); 4179 | System.out.printf("\nThe volume of ellipsoid is: %f", volume); 4180 | } 4181 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 4182 | } 4183 | ``` 4184 | ---------------------------------------- 4185 | 4186 | # Question 137 4187 | 4188 | ### **Question:** 4189 | 4190 | > ***Write a program that uses a for loop to determine power of a number entered by the user.*** 4191 | 4192 | --------------------------------------- 4193 | 4194 | Solution: 4195 | 4196 | ```java language 4197 | import java.util.Scanner; 4198 | 4199 | public class MyClass { 4200 | public static void main(String[] args) { 4201 | int x, y; 4202 | long power = 1; 4203 | System.out.print("\nEnter the value for x: "); 4204 | x = STDIN_SCANNER.nextInt(); 4205 | System.out.print("\nEnter the value for y: "); 4206 | y = STDIN_SCANNER.nextInt(); 4207 | for(int i = 1; i <= y; i++) { 4208 | power = power * x; 4209 | } 4210 | System.out.print(x + " ^ " + y + " = " + power); 4211 | } 4212 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 4213 | } 4214 | ``` 4215 | ---------------------------------------- 4216 | 4217 | 4218 | # Question 138 4219 | 4220 | ### **Question:** 4221 | 4222 | > ***Write a program to read three numbers and find average of numbers.*** 4223 | 4224 | --------------------------------------- 4225 | 4226 | Solution: 4227 | 4228 | ```java language 4229 | import java.util.Scanner; 4230 | 4231 | public class MyClass { 4232 | public static void main(String[] args) { 4233 | int a, b, c; 4234 | float avg; 4235 | System.out.print("\nEnter the first number: "); 4236 | a = STDIN_SCANNER.nextInt(); 4237 | System.out.print("\nEnter the second number: "); 4238 | b = STDIN_SCANNER.nextInt(); 4239 | System.out.print("\nEnter the third number: "); 4240 | c = STDIN_SCANNER.nextInt(); 4241 | avg = (float)(a + b + c / 3.0); 4242 | System.out.printf("\nAverage of three numbers is: %f", avg); 4243 | } 4244 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 4245 | } 4246 | ``` 4247 | ---------------------------------------- 4248 | 4249 | 4250 | # Question 139 4251 | 4252 | ### **Question:** 4253 | 4254 | > ***Write a program to read integer "n" and print first three powers (n1, n2, n3).*** 4255 | 4256 | --------------------------------------- 4257 | 4258 | Solution: 4259 | 4260 | ```java language 4261 | import java.util.Scanner; 4262 | 4263 | public class MyClass { 4264 | public static void main(String[] args) { 4265 | int n; 4266 | System.out.print("\nEnter a number: "); 4267 | n = STDIN_SCANNER.nextInt(); 4268 | System.out.printf("%f, %f, %f", Math.pow(n, 1), Math.pow(n, 2), Math.pow(n, 3)); 4269 | } 4270 | public final static Scanner STDIN_SCANNER = new Scanner(System.in); 4271 | } 4272 | ``` 4273 | ---------------------------------------- 4274 | 4275 | 4276 | 4277 | # Question 140 4278 | 4279 | ### **Question:** 4280 | 4281 | > ***Write a program to search the substring in a given string.*** 4282 | 4283 | --------------------------------------- 4284 | 4285 | Solution: 4286 | 4287 | ```java language 4288 | public class MyClass { 4289 | public static void main(String[] args) { 4290 | String name="Java is a powerful general-purpose programming language"; 4291 | System.out.println(name.contains("Java")); 4292 | System.out.println(name.contains("programming")); 4293 | System.out.println(name.contains("language")); 4294 | } 4295 | } 4296 | ``` 4297 | ---------------------------------------- 4298 | 4299 | 4300 | # Question 141 4301 | 4302 | ### **Question:** 4303 | 4304 | > ***Write a program to check if the string ends with a given suffix.*** 4305 | 4306 | --------------------------------------- 4307 | 4308 | Solution: 4309 | 4310 | ```java language 4311 | public class MyClass { 4312 | public static void main(String[] args) { 4313 | String a="Java Programming"; 4314 | System.out.println(a.endsWith("g")); 4315 | } 4316 | } 4317 | ``` 4318 | ---------------------------------------- 4319 | 4320 | 4321 | # Question 142 4322 | 4323 | ### **Question:** 4324 | 4325 | > ***Write a program to check if the string starts with the given prefix.*** 4326 | 4327 | --------------------------------------- 4328 | 4329 | Solution: 4330 | 4331 | ```java language 4332 | public class MyClass { 4333 | public static void main(String[] args) { 4334 | String a="Java Programming"; 4335 | System.out.println(a.startsWith("j")); 4336 | System.out.println(a.startsWith("J")); 4337 | } 4338 | } 4339 | ``` 4340 | ---------------------------------------- 4341 | 4342 | 4343 | # Question 143 4344 | 4345 | ### **Question:** 4346 | 4347 | > ***Write a program to check whether a character is alphabet, digit or special character.*** 4348 | 4349 | --------------------------------------- 4350 | 4351 | Solution: 4352 | 4353 | ```java language 4354 | import java.util.Scanner; 4355 | public class MyClass { 4356 | public static void main(String[] args) { 4357 | char ch; 4358 | Scanner x=new Scanner(System.in); 4359 | System.out.print("Enter a character: "); 4360 | ch=x.next().charAt(0); 4361 | if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) { 4362 | System.out.println(ch+" is Alphabet."); 4363 | } 4364 | else if(ch>='0'&&ch<='9') { 4365 | System.out.println(ch+" is Digit."); 4366 | } 4367 | else { 4368 | System.out.println(ch+" is Special Character."); 4369 | } 4370 | } 4371 | } 4372 | ``` 4373 | ---------------------------------------- 4374 | 4375 | 4376 | # Question 144 4377 | 4378 | ### **Question:** 4379 | 4380 | > ***Write a program to Check whether Java is installed on your computer.*** 4381 | 4382 | --------------------------------------- 4383 | 4384 | Solution: 4385 | 4386 | ```java language 4387 | public class MyClass { 4388 | public static void main(String[] args) { 4389 | System.out.println("\nJava Version: "+System.getProperty("java.version")); 4390 | System.out.println("Java Runtime Version: "+System.getProperty("java.runtime.version")); 4391 | System.out.println("Java Home: "+System.getProperty("java.home")); 4392 | System.out.println("Java Vendor: "+System.getProperty("java.vendor")); 4393 | System.out.println("Java Vendor URL: "+System.getProperty("java.vendor.url")); 4394 | System.out.println("Java Class Path: "+System.getProperty("java.class.path")+"\n"); 4395 | } 4396 | } 4397 | ``` 4398 | ---------------------------------------- 4399 | 4400 | 4401 | 4402 | # Question 145 4403 | 4404 | ### **Question:** 4405 | 4406 | > ***Write a program to Check whether Java is installed on your computer.*** 4407 | 4408 | --------------------------------------- 4409 | 4410 | Solution: 4411 | 4412 | ```java language 4413 | public class MyClass { 4414 | public static void main(String[] args) { 4415 | System.out.println("\nJava Version: "+System.getProperty("java.version")); 4416 | System.out.println("Java Runtime Version: "+System.getProperty("java.runtime.version")); 4417 | System.out.println("Java Home: "+System.getProperty("java.home")); 4418 | System.out.println("Java Vendor: "+System.getProperty("java.vendor")); 4419 | System.out.println("Java Vendor URL: "+System.getProperty("java.vendor.url")); 4420 | System.out.println("Java Class Path: "+System.getProperty("java.class.path")+"\n"); 4421 | } 4422 | } 4423 | ``` 4424 | ---------------------------------------- 4425 | 4426 | 4427 | # Question 146 4428 | 4429 | ### **Question:** 4430 | 4431 | > ***Write a program to get the current system environment and system properties.*** 4432 | 4433 | --------------------------------------- 4434 | 4435 | Solution: 4436 | 4437 | ```java language 4438 | import java.lang.*; 4439 | public class Main { 4440 | public static void main(String[] args) { 4441 | System.out.println("\nCurrent system environment:"); 4442 | System.out.println(System.getenv()); 4443 | System.out.println("\n\nCurrent system properties:"); 4444 | System.out.println(System.getProperties()); 4445 | } 4446 | } 4447 | ``` 4448 | ---------------------------------------- 4449 | 4450 | 4451 | # Question 147 4452 | 4453 | ### **Question:** 4454 | 4455 | > ***Write a program to measure how long code takes to execute in nanoseconds.*** 4456 | 4457 | --------------------------------------- 4458 | 4459 | Solution: 4460 | 4461 | ```java language 4462 | import java.lang.*; 4463 | public class Main { 4464 | public static void main(String[] args) { 4465 | long startTime = System.nanoTime(); 4466 | int i; 4467 | System.out.println ("The first 5 natural numbers are:\n"); 4468 | for (i=1;i<=5;i++) { 4469 | System.out.println(i); 4470 | } 4471 | long estimatedTime = System.nanoTime() - startTime; 4472 | System.out.println("Estimated time (in nanoseconds) to get the first 5 natural numbers: "+estimatedTime); 4473 | } 4474 | } 4475 | ``` 4476 | ---------------------------------------- 4477 | 4478 | 4479 | # Question 148 4480 | 4481 | ### **Question:** 4482 | 4483 | > ***Write a program to replace the spaces of a string with a specific character.*** 4484 | 4485 | --------------------------------------- 4486 | 4487 | Solution: 4488 | 4489 | ```java language 4490 | public class MyClass { 4491 | public static void main(String[] args) { 4492 | String a = "Java Programming"; 4493 | char ch = '-'; 4494 | a = a.replace(' ', ch); 4495 | System.out.println("String after replacing spaces with the character '-': "); 4496 | System.out.println(a); 4497 | } 4498 | } 4499 | ``` 4500 | ---------------------------------------- 4501 | 4502 | 4503 | 4504 | # Question 149 4505 | 4506 | ### **Question:** 4507 | 4508 | > ***Write a program to count the total number of punctuations in a given string.*** 4509 | 4510 | --------------------------------------- 4511 | 4512 | Solution: 4513 | 4514 | ```java language 4515 | public class Main { 4516 | public static void main (String args[]) { 4517 | int count = 0; 4518 | String str = "Logic will get you from A to Z; imagination will get you everywhere."; 4519 | for(int i = 0; i < str.length(); i++) { 4520 | if(str.charAt(i) == '!' || str.charAt(i) == ',' || str.charAt(i) == ';' 4521 | || str.charAt(i) == '.' || str.charAt(i) == '?' || str.charAt(i) == '-' 4522 | || str.charAt(i) == '\'' || str.charAt(i) == '\"' || str.charAt(i) == ':') { 4523 | count++; 4524 | } 4525 | } 4526 | System.out.println("The total number of punctuations in a given string is: " +count); 4527 | } 4528 | } 4529 | ``` 4530 | ---------------------------------------- 4531 | 4532 | 4533 | # Question 150 4534 | 4535 | ### **Question:** 4536 | 4537 | > ***Write a program to convert Decimal to Hexadecimal.*** 4538 | 4539 | --------------------------------------- 4540 | 4541 | Solution: 4542 | 4543 | ```Java 4544 | public class MyClass { 4545 | public static void main(String args[]){ 4546 | System.out.println(Integer.toHexString(10)); 4547 | System.out.println(Integer.toHexString(15)); 4548 | System.out.println(Integer.toHexString(289)); 4549 | } 4550 | } 4551 | ``` 4552 | ---------------------------------------- 4553 | 4554 | # Question 151 4555 | 4556 | ### **Question:** 4557 | 4558 | > ***Write a program to convert Decimal to Octal.*** 4559 | 4560 | --------------------------------------- 4561 | 4562 | Solution: 4563 | 4564 | ```Java 4565 | public class MyClass { 4566 | public static void main(String args[]){ 4567 | 4568 | System.out.println(Integer.toOctalString(8)); 4569 | System.out.println(Integer.toOctalString(19)); 4570 | System.out.println(Integer.toOctalString(81)); 4571 | 4572 | } 4573 | 4574 | } 4575 | ``` 4576 | ---------------------------------------- 4577 | 4578 | # Question 152 4579 | 4580 | ### **Question:** 4581 | 4582 | > ***Write a program to convert Decimal to Binary.*** 4583 | 4584 | --------------------------------------- 4585 | 4586 | Solution: 4587 | 4588 | ```Java 4589 | 4590 | public class MyClass { 4591 | public static void main(String args[]){ 4592 | System.out.println(Integer.toBinaryString(10)); 4593 | System.out.println(Integer.toBinaryString(21)); 4594 | System.out.println(Integer.toBinaryString(31)); 4595 | } 4596 | 4597 | } 4598 | ``` 4599 | ---------------------------------------- 4600 | 4601 | # Question 153 4602 | 4603 | ### **Question:** 4604 | 4605 | > ***Write a program to convert Binary to Decimal.*** 4606 | 4607 | --------------------------------------- 4608 | 4609 | Solution: 4610 | 4611 | ```Java 4612 | 4613 | public class MyClass { 4614 | public static void main(String args[]){ 4615 | String a="1010"; 4616 | int decimal=Integer.parseInt(a,2); 4617 | System.out.println(decimal); 4618 | } 4619 | } 4620 | ``` 4621 | ---------------------------------------- 4622 | # Question 154 4623 | 4624 | ### **Question:** 4625 | 4626 | > ***Write a program to convert Hexadecimal to Decimal.*** 4627 | 4628 | --------------------------------------- 4629 | 4630 | Solution: 4631 | 4632 | ```Java 4633 | 4634 | public class MyClass { 4635 | public static void main(String args[]){ 4636 | String hex="a"; 4637 | int decimal=Integer.parseInt(hex,16); 4638 | System.out.println(decimal); 4639 | } 4640 | 4641 | } 4642 | ``` 4643 | ---------------------------------------- 4644 | 4645 | 4646 | # Question 155 4647 | 4648 | ### **Question:** 4649 | 4650 | > ***Write a program to determine whether one string is a rotation of another.*** 4651 | 4652 | --------------------------------------- 4653 | 4654 | Solution: 4655 | 4656 | ```Java 4657 | 4658 | public class MyClass { 4659 | public static void main(String[] args) { 4660 | String x = "abcde", y = "deabc"; 4661 | if(x.length() != y.length()){ 4662 | System.out.println("Second string is not a rotation of first string"); 4663 | } 4664 | else { 4665 | x = x.concat(x); 4666 | 4667 | if(x.indexOf(y) != -1) 4668 | System.out.println("Second string is a rotation of first string"); 4669 | else 4670 | System.out.println("Second string is not a rotation of first string"); 4671 | } 4672 | } 4673 | } 4674 | ``` 4675 | ---------------------------------------- 4676 | 4677 | 4678 | # Question 156 4679 | 4680 | ### **Question:** 4681 | 4682 | > ***Write a program to illustrate the isNaN method.*** 4683 | 4684 | --------------------------------------- 4685 | 4686 | Solution: 4687 | 4688 | ```Java 4689 | 4690 | public class MyClass { 4691 | public static void main(String args[]) { 4692 | 4693 | /* The isNaN method returns true if the value is NaN. */ 4694 | 4695 | Float a = Float.NaN; 4696 | Float b = 6.0f; 4697 | System.out.println(a +" - " + a.isNaN()); 4698 | System.out.println(a +" - " + Float.isNaN(a)); 4699 | System.out.println(b +" - " + Float.isNaN(b)); 4700 | } 4701 | } 4702 | ``` 4703 | ---------------------------------------- 4704 | 4705 | # Question 157 4706 | 4707 | ### **Question:** 4708 | 4709 | > ***Write a program to illustrate the isNaN method.*** 4710 | 4711 | --------------------------------------- 4712 | 4713 | Solution: 4714 | 4715 | ```Java 4716 | 4717 | public class MyClass { 4718 | public static void main(String args[]) { 4719 | 4720 | /* The isNaN method returns true if the value is NaN. */ 4721 | 4722 | Float a = Float.NaN; 4723 | Float b = 6.0f; 4724 | System.out.println(a +" - " + a.isNaN()); 4725 | System.out.println(a +" - " + Float.isNaN(a)); 4726 | System.out.println(b +" - " + Float.isNaN(b)); 4727 | } 4728 | } 4729 | ``` 4730 | ---------------------------------------- 4731 | 4732 | # Question 158 4733 | 4734 | ### **Question:** 4735 | 4736 | > ***Write a program to Design Simple Calculator.*** 4737 | 4738 | --------------------------------------- 4739 | 4740 | Solution: 4741 | 4742 | ```Java 4743 | 4744 | import java.util.Scanner; 4745 | 4746 | public class MyClass { 4747 | public static void main(String[] args) { 4748 | 4749 | char operator; 4750 | Double number1, number2, result; 4751 | Scanner input = new Scanner(System.in); 4752 | System.out.println("Choose an operator: +, -, *, or /"); 4753 | operator = input.next().charAt(0); 4754 | 4755 | System.out.println("Enter first number:"); 4756 | number1 = input.nextDouble(); 4757 | 4758 | System.out.println("Enter second number:"); 4759 | number2 = input.nextDouble(); 4760 | 4761 | switch (operator) { 4762 | 4763 | case '+': 4764 | result = number1 + number2; 4765 | System.out.println(number1 + " + " + number2 + " = " + result); 4766 | break; 4767 | 4768 | case '-': 4769 | result = number1 - number2; 4770 | System.out.println(number1 + " - " + number2 + " = " + result); 4771 | break; 4772 | 4773 | case '*': 4774 | result = number1 * number2; 4775 | System.out.println(number1 + " * " + number2 + " = " + result); 4776 | break; 4777 | 4778 | case '/': 4779 | result = number1 / number2; 4780 | System.out.println(number1 + " / " + number2 + " = " + result); 4781 | break; 4782 | 4783 | default: 4784 | System.out.println("Invalid operator!"); 4785 | break; 4786 | } 4787 | 4788 | input.close(); 4789 | } 4790 | } 4791 | 4792 | 4793 | ``` 4794 | ---------------------------------------- 4795 | 4796 | 4797 | # Question 159 4798 | 4799 | ### **Question:** 4800 | 4801 | > ***Write a program to print Invert Triangle.*** 4802 | 4803 | --------------------------------------- 4804 | 4805 | Solution: 4806 | 4807 | ```Java 4808 | public class MyClass { 4809 | public static void main(String args[]) { 4810 | int x = 9; 4811 | while(x > 0) { 4812 | for(int i=1; i<=x; i++) { 4813 | System.out.print(" "+x+" "); 4814 | } 4815 | System.out.print("\n"); 4816 | x--; 4817 | } 4818 | } 4819 | } 4820 | 4821 | ``` 4822 | ---------------------------------------- 4823 | 4824 | 4825 | 4826 | 4827 | 4828 | 4829 | 4830 | 4831 | 4832 | 4833 | --------------------------------------------------------------------------------