Go to Top

Thursday 26 January 2012

Visitors Request - 3

Hi Aman !

               Thanks for posting comments ! As far as I remember, you have asked for this pattern :

# # # #
#@@@#
#@@@#
#@@@#
# # # #

(Sorry, for the bad indentation, Blogger indents are different from those in Java Terminal Window)

Here's your program : 

class pattern_40
{
 public static void print(int a)
 {
     int i,j;
     for(i=1;i<a;i++)
     System.out.print("#");
     System.out.print("\n");
     for(i=1;i<a-1;i++)
     {
         System.out.print("#");
         for(j=1;j<a-1;j++)
         System.out.print("@");
         System.out.print("#");
         System.out.print("\n");
     }
     for(i=1;i<a;i++)
     System.out.print("#");
    }
}

I have made it such that you can form a pattern for any range that you pass as a parameter into the variable "a". If you find any mistakes, or if this program is not the way you meant it to be, then please comment the same way.  Thanks for contributing a nice Java question to this blog.

Regards,
ICSE Java.

Tuesday 17 January 2012

To convert binary number into decimal notation

Question 74 : Write a program in Java to convert a binary number into decimal notation.

Java Program : 

class bintodec
{
 static void main(String m)
 {
     String n="";
     int i=0,d=m.length()-1;
     double a=0;
     for(d=m.length()-1;d>=0;d--)
     {
         a=a+(Integer.parseInt(n+m.charAt(d))*Math.pow(2,i));
         i++;
        }
        System.out.print("The decimal value of "+m+" is : "+a);
    }
}

To convert decimal number into binary notation

Question 73 : Write a program in Java to convert a decimal number into binary notation.

Java Program : 

class dectobin
{
 static void main(int m)
 {
     String s="";
     int n=m;
     while(n>0)
     {
         s=String.valueOf(n%2)+s;
         n=n/2;
        }
        System.out.print("The binary value of "+m+" is : "+s);
    }
}

ShareThis