Lab 3

General instruction:- Make a directory named esc101-lab03 before starting. You should put all the programs in this directory.

Monday[20 aug]


  1. Write a Java class with a main method that declares a double variable and initializes it to a positive real number. The program prints the integer part, fractional part, and the nearest integer of this number. Assume that the nearest integer of X.5 is X+1 where X is a non-negative integer. For example, if the number is 4.678 the program should produce the following output.

    The integer part of 4.678 is 4.
    The fractional part of 4.678 is 0.678.
    The nearest integer to 4.678 is 5.

    Note that the second line may not come out exactly as 0.678.
  2. Write a Java class with a main method that declares an integer and initializes it to a positive number. The program prints the sum of the digits of the number.


  3. Write a Java class with a main method that declares an integer and initializes it to a four-digit number. The program checks if it is a palindrome. A palindrome is a number which when read in the reverse direction is the same as the original number. For example, 1771 is a palindrome. Sample output in this case: The input is 1771. It is a palindrome.

    However, 1234 is not a palindrome. Sample output in this case: The input is 1234. It is not a palindrome.

    Can you extend this program to check if an integer with any number of digits is a palindrome?


  4. Write a Java class with a main method to print all primes within 10000. How many primes are there between 0 and 1000, and 1000 and 10000?



Tuesday[21 aug]


  1. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

    Write a program to find all Armstrong number in the range of 0 and 999
  2. Write a Java class with a main method that declares an integer and initializes it to a positive number (> 1000). The program prints the reverse of the digits of the number.



  3. Fibonacci series is a series of numbers starting from 0,1 such that each number is the sum of previous two numbers.eg
    0   1  1  2  3  5   8    13
    Write a Java class with a main method that declares an integer n(number of terms in fibonacci series) and prints the first n terms of Fibonacci series.



  4. Write a Java class with a main method to print the factorial of a number n.



Thu[23 aug]


  1. Write a java program that declares and initializes an integer. The program should print "1" , if the integer is divisible by only 2 print "2" , if the integer is divisible by 2 and 7 print "3" , if the integer is divisible by 2 and 7 and 11 (Use nested if-else).
  2. Write a java program that declares and initializes an integer 'n'. The program should calculate and print the integer 2^n, using the for loop construct. Re-write the program using the while loop and the do-while loop.


  3. Write a java program to calculate and print the sum of all prime numbers between 2 and 50.


  4. Write a java program to find and print all 3 digit integers which are palindromes. (A 3-digit integer qualifies for a palindrome if its symmetric, ie, having the same first and last digit eg. 131, 595...).



Fri[24 aug]


  1. Write a java program to print a pyramid for numbers upto 20. Example of a pyramid for numbers upto 4 :
    1
    2   1  2
    3   2  1  2  3
    4   3  2  1  2  3  4
  2. Write a java program to print the first 20 terms and the sum of a arithmetic progression given its first term, increment and the number of terms.


  3. Write a java program to print the prime factors of a given integer.


  4. Write a java program to convert a number into it's binary equivalent and find its 2's complement without converting it back into decimal format. Do not use library function for conversion.


Wed[29 aug]


  1. Kaprekar number for a given base is a non-negative integer, the representation of whose square in that base can be split into two parts that add up to the original number again.

    Consider an n-digit number k. Square it and add the right n digits to the left n or n-1 digits. If the resultant sum is k, then k is called a Kaprekar number.
    For example,
    9 is a Kaprekar number since
    9^2==81 and 8+1==9,

    and 297 is a Kaprekar number since
    297^2==88209 and 88+209==297.

    Write a program to print all Kaprekar numbers from 1 to 1000. Extend this program to generate Kaprekar numbers . between 1 to M where M is initialized and declared in the program.
  2. Write a program to find the hexadecimal representation of a non-negative integer. For ex., the output for 121 should be 79, and for 122 it should be 7A (make no assumption on the number of digits).


  3. Write a program to find the binary representation of a positive decimal fraction (in [0, 1), i.e. the integer part is 0). For ex., the output for 0.75 should be 0.11 (make no assumption on the number of places after the point).

    Multiply the number by 2. If the integer part becomes 1, it has a component of 1/2. Ignore the integer part. Again multiply the new fraction by 2. If the integer part becomes 1, it has a component of 1/4, and so on. This is just the inverse process of converting an integer to binary representation. The process terminates when the fraction becomes exactly 0 (or 1.0, depending on how you interpret it).


  4. Write a program to print out "victory!" (V sign) (with any number of rows):

    *          *
     *       * 
      *     *  
       *   *   
        * *    
         *