Thursday, July 27, 2017

Hailstone Sequence

/* * This program generates hailstone sequence 
 * Use internet to find out how hailstone sequence works.

public class HailstoneSequence {

    public static void sequenceCalculation(int num) {

        if(num == 0) {
            System.out.println("Please enter a natural number > 0:");
        while(num!=1) {
            if(num % 2 == 0) {
                num = num/2;
            } else {
                num = 3*num + 1;
            System.out.printf(", %d ", num);

    //main method    
public static void main(String [] args) {


   /*This program is not complete. It doesn't interact with the users.
  Use Scanner class to take user input and display the hailstone sequence.
 After doing some research you knew that the Hailstone sequence is a 
 sequence of numbers where the the nextnumber is division of 2 if the
 preceding num is even or if its odd then the following number will
 be 3 times the preceeding number plus 1.Mathematically:                 
                N(t+1) = N(t)/2 or 3N(t)+1; 
                       Where t = 1,2,3,4,...... ;             N = number;              

Monday, May 15, 2017

Sum of the numbers which are multiple of 3.

//Problem:Find the sum of all natural numbers which are less or equal to n and are 
//multiple of 3. 

/* This program calculates the sum of the multiples of 3  from 1 to the specified number. 
 * @author blankbug * 

public class Lesson1 {
    private static int num = 12;
    public static void main(String[]args){


    //define a method to calculate the sum of multiples of 3    
public static int multiple(int num){
        int total_sum = 0;
        for(int i=1;i<=num;i++){
            if(i%3 == 0){
                total_sum += i;

        return total_sum;
    }//end of a method multiple
}//end of a class Lesson1

Task 1: Implement this program to take user input and prints the sum of multiple of 3  
of all the number less or equal to the given number.

Task 2: Change this program so that it asks users a number and prints the sum of all 
the numbers less or equal to this number and are multiple of 7.

Thursday, May 11, 2017

Square Root and Cube Root

Problem: Write a java program to print the following multiplication table.

Note: You can use several print and println statements to do this but try something 
different.What about a loop?

/** * @author blankbug * */
public class Square{
    int i;
    public static void main(String[]args){
        Square sq = new Square();

    public void calculation(){
        for(int i=1;i<=10;i++){