Go to Top

Monday, 19 February 2018

Readers request - 7

Hello Keshab !

You had asked for the program to print the following pattern :

1 2 3 4 5
2 3 4 5
3 4 5
4 5
5


Java Program :
class keshabs_query
{
    public static void main(String args[])
    {
        int i,j;
        for(i=1;i<=5;i++)
        {
            for(j=i;j<=5;j++)
            {
                System.out.print(j);
            }
            System.out.print("\n");
        }
    }
}


P.S. - I honestly want to thank all the young programmers out here who have been helping the others in doubt, and contributing to keep this blog lively enough. I am back from my hiatus and will try to revamp this forum with new and good content, as always (*wink).

Tuesday, 21 February 2012

Visitor's Request - 6

Hello Harish !

You had asked for the program to print the Pascal's Triangle, which is :

1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 

Here's your program.

Java Program :

class pattern_44
{
 static void print(int b)
 {
     int[] a=new int[b];
     a[0]=1;
     int i,j,k;
     for(i=0;i<b;i++)
     {
         for(j=i-1;j>=0;j--)
         a[j+1]=a[j+1]+a[j];
         for(k=0;k<=i;k++)
         System.out.print(a[k]+" ");
         System.out.print("\n");
        }
    }
}

I would be a bit busy for this month, owing to my ICSE exams, but in case you have any more queries, be sure to communicate them to me. I would certainly try to solve it in my free time.

Tuesday, 14 February 2012

Visitors Request - 5

This was the program requested from me.

Question : To print the following pattern.

55555
54444
54333
54322
54321

Java Program :

class pattern_41
{
 static void pattern(int a)
 {
     int i,j,k;
     for(i=a;i>=1;i--)
     {
         for(j=a;j>i;j--)
         System.out.print(j);
         for(k=1;k<=i;k++)
         System.out.print(i);
         System.out.print("\n");
        }
    }
}

This is the program for it and as usual, I have included the option to extend the pattern to any length you enter.

Friday, 3 February 2012

Visitors Request - 4

Hello once again Aman ! You have the following problems :

"tell me is there any way that i could low the graphics of a particular game so that i could play it on my computer because every tome i install a game it gave an error that pixel shader 2.0 not found."  

Here's a nice software that could really help you out to play your game. Just download it. The link is provided below :


The software is called "3D - Analyze". This software is used to emulate your hardware, or in other words, fool your game to believe that your computer really has whatever it wants. Install the software in a particular folder which you can easily search for. Then open it and click on the .exe file which says "3DAnalyze". Then in the top left, click on the "Select .exe" and click on the .exe file of the game in the browse window.
Then look for the boxes "VendorID" and "DeviceID" below. On the right of it will be a list of some graphics cards and their ID's. Enter the corresponding ID's in the boxes provided. You may even use some of the check boxes above. After this is done, click on "Run" at the top right part of the window and see if the game runs.

And be sure to leave a comment or two or some feedbacks or even a rating of your choice.

Visitors Request - 4

Hello once again Aman ! You have the following problems : 

"tell me is there any way that i could low the graphics of a particular game so that i could play it on my computer because every tome i install a game it gave an error that pixel shader 2.0 not found."  

Here's a nice software that could really help you out to play your game. Just download it. The link is provided below :


The software is called "3D - Analyze". This software is used to emulate your hardware, or in other words, fool your game to believe that your computer really has whatever it wants. Install the software in a particular folder which you can easily search for. Then open it and click on the .exe file which says "3DAnalyze".
Then int the top left, click on the "Select .exe" and click on the .exe file of the game in the browse window.
Then look for the boxes "VendorID" and "DeviceID" below. On the right of it will be a list of some graphics cards and their ID's. Enter the corresponding ID's in the boxes provided. You may even use some of the check boxes above. After this is done, click on "Run" at the top right part of the window and see if the game runs.

And be sure to leave a comment or two or some feedbacks or even a rating of your choice.

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);
    }
}

Saturday, 31 December 2011

Happy New Year !

Sunday, 25 December 2011

Merry Christmas !



Friday, 23 December 2011

To manipulate vowels in a string

Question 72 : Write a program in Java to accept a word and do the following : 

1) If the word starts with a vowel, then move the vowel to the end of the word and add "ay" to it. 
Example : owl -> wloay.
2) If there are no vowels in the word, then just add "ay" to it.
Example : sky -> skyay.
3) If the vowel lies in between, then remove all the letters from the beginning till the vowel and add them at the end of the word. Finally, attach "ay" to it.
Example : stop -> opstay.

Java Program :

import java.io.*;
class vowel_changer
{
    static String s,s1;
   static void main()throws IOException
   {
       input();
       manipulate();
       display();
    }
  static void input()throws IOException
  {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter a word : ");
      s=br.readLine();
    }
    static void manipulate()
    {
        int i,c=0;
        char k;
        for(i=0;i<s.length();i++)
        {
            k=s.charAt(i);
            if(k=='a' || k=='A' || k=='e' || k=='E' || k=='i' || k=='I' || k=='o' || k=='O' || k=='u' || k=='U')
            {
                if(i==0)
                s1=s.substring(1,s.length())+k+"ay";
                else
                s1=s.substring(i,s.length())+s.substring(0,i)+"ay";
                c=1;
                break;
            }
        }
        if(c==0)
        s1=s+"ay";
    }
    static void display()
    {
        System.out.print("\nManipulated String : "+s1);
    }
}
                 

Wednesday, 21 December 2011

Utilities 26 : The nice old cups game

Recently, I had made a nice cups game. Most of you would be familiar with the old cups game, where a ball is kept inside one of the three cups, and while the cups are interchanged amongst themselves, you have to look out for the position of the ball inside that particular cup.

I had posted that game within a package, if you don't remember it, click here. I am posting the game individually now. Enjoy.

Java Program :

/*
 *      _____
 *     |     |              
 *     |     |
 *     |     |
 */

import java.io.*;
import java.util.Random;
class cups
{
 static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 static String feed="",err="",garb,s1,s2,s="      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |     |     |     |\n     |     |     |     |     |     |";
 static Random r=new Random();
 static int c,pos,time=900,score=0,lvl=1,iter=2,ded=0,ff=0,choice;
 static void main()throws IOException
 {
    control();
}    
 static void control()throws IOException
 {
     String choice=null;
    System.out.print("\fCups : The Game");
        System.out.print("\n\n1: New Game\n2: View score\n3: Visual demo\n4: Help\n5: Feedback\n6: Report an error\n7: Exit");
        System.out.print("\n\nEnter your choice : ");
        choice=br.readLine();
        if(choice.equals(""))
        choice=" ";
        switch(choice.charAt(0))
        {
            case '1':
            cont();
            break;
            case '2':
            details();
            control();
            break;
            case '3':
            viddem();
            break;
            case '4':
            help();
            break;
            case '5':
            feedback();
            break;
            case '6':
            error();
            break;
            case '7':
            exit();
            break;
            case 'q':
            case 'Q':
            viewer();
            break;
            default:
            System.out.print("\nInput error (Invalid input). Press enter to continue : ");
            garb=br.readLine();
            control();
}
}
    static void cont()throws IOException
    {
        String choice=" ";
        if(lvl!=1)
        {
        System.out.print("\fPress enter to continue or 'm' to initiate in-game menu : ");
        choice=choice+br.readLine();
    }
        if(choice.equals(" "))
        {
            gamerand();
            randmove();
            input();
        }
        else
        {
             System.out.print("\fIn-Game Menu\n\n1: Continue\n2: View score\n3: Restart\n4: Exit");
            System.out.print("\n\nEnter your choice : ");
            choice=br.readLine();
            switch(choice.charAt(0))
            {
                case '1':
                gamerand();
                randmove();
                input();
                break;
                case '2':
                details();
                cont();
                break;
                case '3':
                reset();
                break;
                case '4':
                exit();
                break;
                default:
                System.out.print("\nInput error (Invalid input). Press enter to continue : ");
                garb=br.readLine();
                cont();
            }
        }
    }
 static void gamerand()throws IOException
 {
     System.out.print("\fStage : "+lvl+"\n\n");
     c=r.nextInt(4);
     if(c==0)
     gamerand();
     else
     balldisp();
    }
    static void balldisp()throws IOException
    {
        if(c==1)
        {
            System.out.print("      _____       _____       _____\n     |     |     |     |     |     |\n     |  O  |     |     |     |     |\n     |     |     |     |     |     |");
            pos=1;
         }
        if(c==2)
        {
            System.out.print("      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |  O  |     |     |\n     |     |     |     |     |     |");
            pos=2;
         }
        if(c==3)
        {
            System.out.print("      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |     |     |  O  |\n     |     |     |     |     |     |");
            pos=3;
         }
        sleep(2000);
        System.out.print("\f");
    }
    static void randmove()throws IOException
        {      
        System.out.print(s);
        for(int i=1;i<iter;i++)
        {
            int c=r.nextInt(7);
            switch(c)
            {
                case 1:
                if(pos==1)
                pos=2;
                else if(pos==2)
                pos=1;
                move12();
                break;
                case 2:
                if(pos==2)
                pos=3;
                else  if(pos==3)
                pos=2;
                move23();
                break;
                case 3:
                if(pos==1)
                pos=3;
                else  if(pos==3)
                pos=1;
                move13();
                break;
                case 4:
                if(pos==1)
                pos=2;
                else  if(pos==2)
                pos=1;
                move21();
                break;
                case 5:
                if(pos==2)
                pos=3;
                else  if(pos==3)
                pos=2;
                move32();
                break;
                case 6:
                if(pos==1)
                pos=3;
                else  if(pos==3)
                pos=1;
                move31();
                break;
                default:
                i=i-1;
            }  
        }
    }
    static void input()throws IOException
    {
        int c=0;
        System.out.print("\n\n\nWhere is the ball ? : ");
        try
        {
            c=Integer.parseInt(br.readLine());
       }
        catch(Exception e)
        {
            ff=ff+1;
            System.out.print("\nLevel forfeited (Forfeits "+ff+" of 3) . Press enter to restart : ");
            garb=br.readLine();
            if(ff==3)
            {
                System.out.print("\nForfeit limit reached. Game over. Press enter to view score : ");
                garb=br.readLine();
                details();
                control();
            }
            cont();
        }
        if(c!=1235)
        {
        if(c==pos)
        {
        System.out.print("\nCorrect Answer");
        score=score+10;
    }
    if(c!=pos)
    System.out.print("\nWrong answer. The ball is in cup "+pos);
    System.out.print("\n\nPress enter to continue : ");
    garb=br.readLine();}
    level();
}  
static void level()throws IOException
{
  lvl=lvl+1;
  if(lvl<5)
  time=time-200;
  if(lvl==5)
  time=time-120;
  iter=iter+1;
  cont();
 }
 static void move12()
 {
     sleep(time);
     s1="\f                  _____       _____\n                 |     |     |     |\n                 |     |     |     |\n                 |     |     |     |\n      _____\n     |     |\n     |     |\n     |     |";
     System.out.print(s1);  
     sleep(time);
     s1="\f                  _____       _____\n                 |     |     |     |\n                 |     |     |     |\n                 |     |     |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
     System.out.print(s1);
     sleep(time);
     s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
     System.out.print(s1);
     sleep(time);
     s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
  System.out.print(s1);
    }
    static void move23()
 {
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____\n     |     |     |     |\n     |     |     |     |\n     |     |     |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
    static void move13()
    {
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n      _____\n     |     |\n     |     |\n     |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |\n      _____\n     |     |\n     |     |\n     |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
    static void move21()
    {
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n      _____\n     |     |\n     |     |\n     |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____       _____\n                 |     |     |     |\n                 |     |     |     |\n                 |     |     |     |\n      _____\n     |     |\n     |     |\n     |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
    static void move32()
    {
        sleep(time);
         s1="\f      _____       _____\n     |     |     |     |\n     |     |     |     |\n     |     |     |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |\n";
        System.out.print(s1);
        sleep(time);
         s1="\f      _____       _____\n     |     |     |     |\n     |     |     |     |\n     |     |     |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |\n";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
    static void move31()
    {
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |\n      _____\n     |     |\n     |     |\n     |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n      _____\n     |     |\n     |     |\n     |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
        static void sleep(int c)
  {
      try{Thread.sleep(c);}
      catch(Exception e){}
    }
    static void details()throws IOException
    {
        double fps=1/time;
        System.out.print("\fYour are in level : "+lvl+"\nYour current score is : "+score+"\nFPS : "+fps+"\nNo. of shuffles : "+(iter-1));
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
    }
    static void exit()
    {
        System.out.print("\nThanks for trying out Cups : The Game");
        System.exit(0);
    }
    static void reset()throws IOException
    {
        time=950;
        lvl=1;
        ded=0;
        iter=2;
        score=0;
        System.out.print("\fGame reset. Press enter to continue : ");
        garb=br.readLine();
        control();
    }
    static void error()throws IOException
    {
        System.out.print("\fPlease type the error : ");
        err=err+br.readLine();
        System.out.print("\nThank you. Your report has been sent for rectification.");
        err=err+" | ";
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
        control();
    }
    static void feedback()throws IOException
    {
        System.out.print("\fPlease enter your feedback : ");
        feed=feed+br.readLine();
        System.out.print("\nThank you for your feedback.");
        feed=feed+" | ";
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
        control();
    }
    static void viewer()throws IOException
    {
        System.out.print("\fReports : ");
        if(err!="")
        System.out.print(err);
        else
        System.out.print("No reports submitted yet");
        System.out.print("\n\nFeedbacks : ");
        if(feed!="")
        System.out.print(feed);
        else
        System.out.print("No feedbacks submitted yet");
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
        if(garb.equalsIgnoreCase("r"))
        {    
            err="";
            viewer();
        }
        if(garb.equalsIgnoreCase("f"))
        {    
            feed="";
            viewer();
        }
        control();
    }
    static void viddem()throws IOException
    {
        System.out.print("\fWelcome to the video demo\n\nThis demo will teach you how to master the game step-by-step\n\nPress enter to continue : ");
        garb=br.readLine();
        System.out.print("\fStep 1 : Look at the initial arrangement carefully\n\n");
        System.out.print("      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |  O  |     |     |\n     |     |     |     |     |     |\n");
        sleep(4000);
        for(int i=1;i<3;i++)
        {
            System.out.print("                    |\n");
            sleep(500);
        }
        sleep(500);
        System.out.print("                     -");
        sleep(500);
        for(int i=1;i<5;i++)
        {
           System.out.print("-");
           sleep(500);
        }
        sleep(50);
        System.out.print("> See, the ball is at position 'two'");
        System.out.print("\n\nNext step : ");
        garb=br.readLine();
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |  O  |     |     |\n     |     |     |     |     |     |";
        System.out.print(s1);
        sleep(4000);
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |  O  |\n                 |     |";
        System.out.print(s1);
        sleep(900);
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                              _____\n                             |     |\n                             |  O  |\n                             |     |";
        System.out.print(s1);
        sleep(900);
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____       _____\n     |     |     |     |\n     |     |     |     |\n     |     |     |     |\n                              _____\n                             |     |\n                             |  O  |\n                             |     |";
        System.out.print(s1);
        sleep(900);
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |     |     |  O  |\n     |     |     |     |     |     |";
        System.out.print(s1);
        System.out.print("\n\nNext step : ");
        garb=br.readLine();
        System.out.print("\fStep 3 : Now you know where the ball is hiding ? Hit the right key and press enter\n\n");
        System.out.print(s);
        System.out.print("\n\n\nWhere is the ball ? : ");
        sleep(5000);
        System.out.print("3");
        sleep(900);
        System.out.print("  >CLICK< ");
        sleep(900);
        System.out.print("\n\nCorrect Answer");
        sleep(900);
        System.out.print("\n\nYipeee !");
        sleep(900);
        System.out.print("\n\nNext step : ");
        garb=br.readLine();
        System.out.print("\fNow you are all ready to begin. Press enter to return to main menu : ");
        garb=br.readLine();
        control();
    }
    static void help()throws IOException
    {
        System.out.print("\fWelcome to the help guide for Cups : The Game");
        System.out.print("\n\nThe game has virtually unlimited levels and its up to you when you forfeit.");
        System.out.print("\n\nA 2 second clip will show you the position of the ball hidden in the cups.\nThen the cups will be randomly shuffled.\n");
        System.out.print("\nYou'll have to keep track of the ball and when the shuffling is over,\nenter your response by entering the cups numbers 1,2,3 respectively for each cup.");
        System.out.print("\nTry to enter the correct answer, and get rewarded with points.");
        System.out.print("\n\nAs you cross levels, the game become tougher, with more speed of shuffling the cups,\nand more number of shuffles.");
        System.out.print("\n\nGood luck and try to beat them all");
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
        control();
    }
}
         

Friday, 16 December 2011

Libraries 3 : Mod function

Here's the replica of the widely used Mod Function (%). You may even use it in your program as :

// Rest part of the program is omitted
int m=39,n=7,result;
result=mod.cal(m,n);
System.out.print(result);

Java Program :

class mod
{
 static int cal(int number,int mod)
 {
     for(;;)
     {
         if(number>=mod)
         number=number-mod;
         else
         return number;
        }
    }
}

Thursday, 15 December 2011

Utilities 25 : A nice maths trick

Hello everyone ! This nice program will play a nice maths trick on you, leaving you bewildered for a short time and curious for the rest period, to know its formula. Instructions will be provided by the program itself. Enjoy another nice program by ICSE Java.

Java Program :

import java.io.*;
class trick
{
  static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  static String garb;
  static int s;
  static void main()throws IOException
  {
      help();
      input();
      cal();
    }
    static void help()throws IOException
    {
        System.out.print("Welcome to Maths Magic.\n\nFor this trick, you need to think of a three digit number.");
        System.out.print("\n\nEnter when you are ready : ");
        garb=br.readLine();
        System.out.print("\fNow subtract the sum of digits from the original number.\n\nEx.: If you select 943, then subtract (9 + 4 + 3) from 943");
        System.out.print("\ni.e. 943 - (9 + 4 + 3) = 943 - 16 = 927.\n\nEnter when you are ready : ");
        garb=br.readLine();
    }
    static void input()throws IOException
    {
        s=0;
        System.out.print("\fNow enter any two of its digits, and I'll tell the third digit.\n\nEnter when you are ready : ");
        garb=br.readLine();
        try{
            System.out.print("\nEnter 1st digit : ");
            s=s+Integer.parseInt(br.readLine());
            System.out.print("\nEnter 2nd digit : ");
            s=s+Integer.parseInt(br.readLine());
        }
        catch(Exception e){
            System.out.print("\nError. Please try again : ");
            garb=br.readLine();
            System.out.print("\f");
            input();
        }
    }
    static void cal()
    {
        int i;
        for(i=0;i<=9;i++)
        if((s+i)%9==0)
        System.out.print("\nThe third digit is : "+i);
    }
}
        

Wednesday, 14 December 2011

To calculate the Compound Interest (C.I.)

Question 71 : Write a program in Java to find out Compound Interest (C. I.) on the basis of inputs accepted from the user.

Java Program :

import java.io.*;
class compound_interest
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     double p,r,t,a=0,n;
     System.out.println("\t\t*****Input Data*****\n");
     System.out.print("Enter the principal amount : Rs. ");
     p=Double.parseDouble(br.readLine());
     System.out.print("\nEnter the rate of interest (w/o % sign) : ");
     r=Double.parseDouble(br.readLine());
     System.out.print("\nEnter the time in yrs. : ");
     t=Double.parseDouble(br.readLine());
     System.out.print("\nHow many times a year is the interest calculated (Ex.: Half-yearly : 2 times) ? : ");
     n=Double.parseDouble(br.readLine());
     a=p*(Math.pow((1+(r/(n*100))),(t*n)));
     System.out.println("\n\t\t*****Results*****\n");
     System.out.println("Compound interest : Rs. "+(a-p));
     System.out.println("\nAmount : Rs. "+a);
    }
} 

Sunday, 11 December 2011

Utilities 24 : Percentage grapher

This program will represent any number as a percentage of another number in a graphical way. Just compile and see.

Java Program :

import java.io.*;
class per_graph
{
 static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 static int v1,v2,p;
    static void input()throws IOException
    {
        System.out.print("Enter the value : ");
        v1=Integer.parseInt(br.readLine());
        System.out.print("\n"+v1+" is out of : ");
        v2=Integer.parseInt(br.readLine());
        cal();
    }
    static void cal()
    {
        p=(v1*100)/v2;
        display();
    }
    static void display()
    {
        int i;
        System.out.print("\f|");
        for(i=1;i<=100;i++)
        System.out.print("-");
        System.out.print("|\n|");
        for(i=1;i<=p;i++)
        System.out.print("=");
        for(i=1;i<=(100-p);i++)
        System.out.print(" ");
        System.out.print("|\n|");
        for(i=1;i<=100;i++)
        System.out.print("-");
        System.out.print("|\n\n\t\t\t\t\t"+p+" % out of 100 %");
    }
}
    

Thursday, 8 December 2011

To check whether two numbers are amicable

Question 70 : Write a program in Java to check whether two numbers are amicable or not.

Amicable Number : If two numbers are such that the sum of the perfect divisors of one number is equal to the other number and the sum of the perfect divisors of  the other number is equal to the first number, then the numbers are called Amicable Numbers.

Example : 220 and 284.

Java Program :

class amicable
{
 static int a,b;
 static void input(int m,int n)
 {
     a=m;
     b=n;
     display();
    }
    static boolean check()
    {
        int s=0,i;
        for(i=1;i<a;i++)
        {
            if(a%i==0)
            {
                s=s+i;
            }
        }
        if(s==b)
        {
            s=0;
            for(i=1;i<b;i++)
            {
                if(b%i==0)
                {
                    s=s+i;
                }
            }
            if(s==a)
            return true;
            else
            return false;
        }
        return false;
    }
    static void display()
    {
        if(check())
        System.out.print("The numbers are amicable");
        else
        System.out.print("The numbers are not amicable");
}
}

Monday, 28 November 2011

Utilities 23 : Number to Word Converter

Hello friends ! Today I am going to present a unique Number to Word Converter, made in Java. This will allow you to convert any number, long, short, very long, negative, decimal etc. to words in the Indian notation. For example :

Sample input : 132433

Sample output : One lakh thirty two thousand four hundred thirty three only.

This project is still under development, but I assure you very less and minor errors. In case of any error, please report it to us in the comments section.

To view the program, simply click on the link below :

View Here

To download the program, simply click on the link below : 


Friday, 25 November 2011

To print the given pattern

Question 69 : Write a program in Java to print the given pattern : 

    *****
   *       *
  *         *
 *           *
*             *
 *           *
  *         *
   *       *
    *****

(Spacing in Blogger is not as in Java. Note that spacing inside the hexagon is in the order : 5, 7, 11, 13, 11, 7, 5)

Java Program :

class pattern_39
{
 static void print(int n)
 {
     int i,j,k,m=0;
     for(i=1;i<n;i++)
     System.out.print(" ");
     for(i=1;i<=n;i++)
     System.out.print("*");
     System.out.print("\n");
     for(i=1;i<n;i++)
     {
         for(j=1;j<(n-i);j++)
         System.out.print(" ");
         System.out.print("*");
         m=(n+(i*2-1));
         for(k=1;k<m;k++)
         System.out.print(" ");
         System.out.print("*");
         System.out.print("\n");
        }
        m=m-1;
        for(i=1;i<n-1;i++)
     {
         for(j=1;j<=i;j++)
         System.out.print(" ");
         System.out.print("*");
         for(k=1;k<(m-(i*2-1));k++)
         System.out.print(" ");
         System.out.print("*");
         System.out.print("\n");
        }
     for(i=1;i<n;i++)
     System.out.print(" ");
     for(i=1;i<=n;i++)
     System.out.print("*");
    }
}
         

Thursday, 24 November 2011

To print the given pattern

Question 68 : Write a program in Java to print the given pattern : 

ABCDEFEDCBA
ABCDE  EDCBA
ABCD       DCBA
ABC             CBA
AB                   BA
A                         A

(Spacing in Blogger is not as in Java. Note that spacing is in the order : nil, 1, 3, 5, 7...)

Java Program :

class pattern_37
{
 static void pattern(int len)
 {
     int n=65,i,j,k;
     String j1="";
     for(i=len;i>=1;i--)
     {
         n=65;
         for(j=0;j<i;j++)
         System.out.print((char)(n+j));
         System.out.print(j1);
         j1=j1+"  ";
         for(k=i-1;k>=0;k--)
         {
             if(i==len && k==i-1)
             k=k-1;
            System.out.print((char)(n+k));
        }
         System.out.print("\n");
         if(i==len)
         j1=" ";
        }
    }
}

ShareThis