Go to Top

Thursday 11 August 2011

Leave letter : I'll be back folks

                          Unfortunately, my examinations are commencing from the 19th of August, 2011. So I'll be a bit busy and would be returning to more blogging only after the 1st of September, 2011. Good Luck to you all (and myself too) till then.

To find out whether a triangle is right angled or not

Question 43 : Write a program in Java to find out whether a triangle is right angled or not by taking the sides of the triangle as inputs by the user.


Java Program :



import java.io.*;
class right_angled
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int a,b,c;
     System.out.println("Enter the value of the sides\n");
     System.out.print("Side 1 : ");
     a=Integer.parseInt(br.readLine());
     System.out.print("Side 2 : ");
     b=Integer.parseInt(br.readLine());
     System.out.print("Side 3 : ");
     c=Integer.parseInt(br.readLine());
     System.out.print("\n");
     if(a>b&&a>c)
     {
         if((a*a)==(b*b)+(c*c))
         System.out.println("It is a right-angled triangle");
         else 
         System.out.println("It is not a right-angled triangle");
        }
        if(b>c&&b>a)
        {
            if((b*b)==(c*c)+(a*a))
            System.out.println("It is a right-angled triangle");
            else
            System.out.println("It is not a right-angled triangle");
        }
        if(c>a&&c>b)
        {
            if((c*c)==(a*a)+(b*b))
            System.out.println("It is a right-angled triangle");
            else
            System.out.println("It is not a right-angled triangle");
        }
    }
}

To analyse a sentence

Question 42 : Write a program in Java to break a sentence entered by the user into its words and find the no. of vowels and consonants in each.


Java Program : 



import java.io.*;
class vowels
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int i,j,b=0,v=0,c=0,space=0;
     char k,d='a';
     String a,s="";
     System.out.print("Enter the sentence : ");
     a=br.readLine();
     System.out.print("\n");
     for(i=0;i<=a.length()-1;i++)
     {
         k=a.charAt(i);
         if(k==' ')
         {
              check(i-1,b,a);
              b=i+1;
            }
         if(k!=' ')
         space=space+1;
        }
        if(space!=a.length())
        check(a.length()-1,b,a);
        if(space==a.length())
        check(a.length()-1,0,a);
        System.out.println("Thank you for using this short program");
        }
        static void check(int z,int y,String x)
        {
            int j,v=0,c=0;
            char d;
            String s="";
            for(j=y;j<=z;j++)
            {
                 d=x.charAt(j);
                 s=s+d;
                 if(d=='a' || d=='A' || d=='e' || d=='E' || d=='i' || d=='I' || d=='o' || d== 'O' || d=='u' || d=='U')
                 v=v+1;
                 else
                 c=c+1;
              }
              System.out.println(s+" = No. of vowels = "+v+" & No. of consonants = "+c);
              System.out.print("\n");
    }
}

To find out the factors of a number

Question 41 : Write a program in Java to find the factors of a number given by the user.


Java Program :



import java.io.*;
class factors
{
 static void main()throws IOException
 {
     BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
     int a,i;
     System.out.print("Enter the number : ");
     a=Integer.parseInt(buf.readLine());
     System.out.print("\n");
     System.out.print("The factors are : ");
     for(i=1;i<=a/2;i++)
     {
         if(a%i==0)
         System.out.print(i+",");
        }
        System.out.print(a);
    }
}

To make a square root function

Question 40 : Write a program in Java to make a function that calculates the square root of a number given by the user.

Java Program : 

import java.io.*;
class sqrt_function
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int i;
     double a,b=2,c=0;
     System.out.print("Enter a number : ");
     a=Double.parseDouble(br.readLine());
     for(i=1;i<=10;i++)
     {
        c=(a/b);
        b=(c+b)/2;
     }
     System.out.print("\nSquare Root of "+a+" = "+b);     
    }
}

Utilities - 2 : How to jumble up words ?

                         Hi folks! Recently I had created a Java Program to jumble up words. And I am here to share it with you all. Here's the program. Just copy and paste and compile this program :

Java Program : 


import java.io.*;
import java.util.*;
class jumbled_updated
{
  static String s,s1="";
  static char[] a;
  static String[] b;
  static void main()throws IOException
  {
      input();
      separate();
      jumble();
      display();
    }
    static void input()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the word/s : ");
        s=br.readLine();
        if(s.length()<3)
        {
            System.out.print("\nPlease enter atleast three characters\n\n");
            input();
        }
        a=new char[s.length()];
    }
    static void separate()
    {
        int i,u=0,l=0,c1=0;
        char k,c=1;
        for(i=0;i<s.length();i++)
        {
            k=s.charAt(i);
            if(k==' ')
            c++;
        }
        b=new String[c];
        for(i=0;i<s.length();i++)
        {
            k=s.charAt(i);
            if(k==' ')
            {
                u=i;
                b[c1]=s.substring(l,u);
                l=u+1;
                c1++;
            }
        }
        b[c1]=s.substring(l,s.length());
    }
    static void jumble()
    {
        int c1,c2,i,j,k,c=0,c3=2;
        char k1;
        Random r1=new Random();
        Random r2=new Random();
        for(i=1;i<=b.length;i++)
        {
            a=new char[b[c].length()];
            for(j=0;j<b[c].length();j++)
            a[j]=b[c].charAt(j);
            for(k=0;k<=s.length()*3;k++)
            {
                c1=r1.nextInt(b[c].length()-1);
                c2=r2.nextInt(b[c].length()-1);
                k1=a[c1];
                a[c1]=a[c2];
                a[c2]=k1;
            }
            for(k=0;k<a.length;k++)
            s1=s1+a[k];
            if(c3<=b.length)
            s1=s1+" ";
            c++;
            c3++;
        }
    }
    static void display()
    {
        System.out.print("\nThe jumbled word is : "+s1);
        s1="";
    }
}

Please comment if you like this post.

Tuesday 9 August 2011

To find the absolute value of a given integer

Question 39 : Write a program in Java to find the absolute value of an integer given by the user.


Java Program : 


class absolute
{
    static void cal(int a)
    {
        int b=a>0?a:-a;
        System.out.print("The absolute value of '"+a+"' : "+b);
    }
}

Tips & Tricks - 2 - How to disable start up applications

                                  Some of the start up applications like Bit torrent, Windows Messenger etc. are a real nuisance when they are seen to run automatically when the system starts up. Not only are they a nuisance, but they also slow down the system because windows has to run it every time it starts up even if it unnecessary. Also when they appear to be closed, they are still running in the background, so you can't perceive them normally, but only when it slows down your computer. Here are the steps to get rid of them without uninstalling them.


Tips & Tricks : 


Step 1 : First click on Start -> Run... a small dialog box will open up like this : 




Step 2 : In the text box type exactly this "msconfig.exe" or simply "msconfig" .


Step 3 : A window like this will open : 




Step 4 : Select the tab "Startup" .


Step 5 : You'll see many check boxes. Uncheck those boxes that are related to the application you want to get rid of and click on "Apply" and then "Close" .


Step 6 : Then a box like this will appear : 




Step 7 : Then click on "Restart" to restart then only or "Exit Without Restart" if you want to restart later on.


Step 8 : Finally, a dialog box like this will open after you restart your system : 




Step 9 : Select the check box and click on "Ok" and you are done.


And please comment if you like this post.

Thursday 4 August 2011

To say whether a number is even or odd

Question 38 : Write a program in Java to to find whether a number is odd or even :

Java Program :



public class even_or_odd
{
 public static void main(int a)
   {
     int b=a%2;
     if (b==0)
     System.out.println("The number "+a+" is an even one");
     else
     System.out.println("The number "+a+" is an odd one");
    }
} 

To find the LCM of two numbers

Question 37 : Write a program in Java to find the Lowest Common Multiple (L.C.D.) of two numbers entered by the user :

Java Program : 

import java.io.*;
class lcm
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int a,b,i;
     int c=0;
     System.out.print("Enter the first number : ");
     a=Integer.parseInt(br.readLine());
     System.out.print("\n");
     System.out.print("Enter the second number : ");
     b=Integer.parseInt(br.readLine());
     System.out.print("\n");
     c=a*b;
     int d=c;
     for(i=1;i<=c;i++)
     {
         if(i%a==0 && i%b==0 && i<d)
         d=i;
        }
        System.out.println("The L.C.M : "+d);
    }
}

To find the GCD or HCF of two numbers

Question 36 : Write a program in Java to find the Greatest Common Divisor (G.C.D.) of two numbers entered by the user : 


Java Program : 


import java.io.*;
class hcf
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int a,b,i;
     int d=1;
     int c=0;
     System.out.print("Enter the first number : ");
     a=Integer.parseInt(br.readLine());
     System.out.print("Enter the second number : ");
     b=Integer.parseInt(br.readLine());
     if (a>b)
     c=a;
     else
     c=b;
     for(i=1;i<=c;i++)
     {
        if (a%i==0 && b%i==0 && i>d)
        d=i;
    }
    System.out.println("The H.C.F. = "+d);
}
}

Wednesday 3 August 2011

To print the multiplication table of a number up to n

Question 35 : Write a program in Java to print the multiplication table of a number up to n (entered by the user) :

Specimen (shown for 10 up to 20) : 



10x1=10
10x2=20
10x3=30
10x4=40
10x5=50
10x6=60
10x7=70
10x8=80
10x9=90
10x10=100
10x11=110
10x12=120
10x13=130
10x14=140
10x15=150
10x16=160
10x17=170
10x18=180
10x19=190
10x20=200

Java Program : 


public class multiplication_table_till_n_times
{
 public static void main(int a,int n)
 {
     int i,d;
     System.out.println("Your multiplication table is as follows : \n");
     for (i=1;i<=n;i++)
     {
         d=a*i;
         System.out.println(a+"x"+i+"="+d);
        }
    }
}

To print the multiplication table of a number

Question 34 : Write a program in Java to print the multiplication table of a number :


Specimen (shown for 10) :


10x1=10
10x2=20
10x3=30
10x4=40
10x5=50
10x6=60
10x7=70
10x8=80
10x9=90
10x10=100


Java Program :


public class multiplication_table
{
 public static void main(int a)
 {
     int i,d;
     System.out.println("Your multiplication table is as follows : \n");
     for (i=1;i<=10;i++)
     {
         d=a*i;
         System.out.println(a+"x"+i+"="+d);
        }
    }
}

Fibonacci Series : To print and find the sum of the fibonacci series

Question 33 : Write a program in Java to print and find the sum of the fibonacci series upto n terms (value of n to be given by the user) :

Fibonacci series : 


S = 0+1+1+2+3+5+8+......+n terms

Java Program :

public class fibonacci_series
{
 public static void main(int n)
 {
     int a=0;
     int b=1;
     int s=0;
     int c=0;
     int i;
     s=s+a+b;
     System.out.print("The fibonacci series = "+a+","+b+",");
     for(i=3;i<=n;i++)
     {
         c=a+b;
         System.out.print(c+",");         
         a=b;
         b=c;
         s=s+c;
        }
        System.out.println("upto "+n+" terms completed");
        System.out.println("\nThe sum of the fibonacci series upto "+n+" terms = "+s);
    }
}

To print the given pattern up to a user-defined input

Question 32 : Write a program in Java to print the following pattern (shown for 10) : 


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

Java Program : 

import java.io.*;
class pattern_20
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int n,i,j,k;
     System.out.print("Enter the length (in terms of *'s) : ");
     n=Integer.parseInt(br.readLine());
     System.out.print("\n");
     for(i=n-1;i>=0;i--)
     {
         for(k=1;k<=i;k++)
         {
             System.out.print(" ");
            }
         for(j=n;j>=i+1;j--)
         {
             System.out.print("*");
             System.out.print(" ");
            }
            System.out.print("\n");
        }
        for(i=1;i<=n-1;i++)
        {
            for(k=1;k<=i;k++)
            {
                System.out.print(" ");
            }
            for(j=n-1;j>=i;j--)
            {
                System.out.print("*");
                System.out.print(" ");
            }
            System.out.print("\n");
        }
    }
}

To print the given pattern up to user-defined input

Question 26 : Write a program in Java to print the following pattern (shown for 10) : 

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

Java Program : 

import java.io.*;
class pattern_20
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int n,i,j,k;
     System.out.print("Enter the length (in terms of *'s) : ");
     n=Integer.parseInt(br.readLine());
     System.out.print("\n");
     for(i=n-1;i>=0;i--)
     {
         for(k=1;k<=i;k++)
         {
             System.out.print(" ");
            }
         for(j=n;j>=i+1;j--)
         {
             System.out.print("*");
             System.out.print(" ");
            }
            System.out.print("\n");
        }
        for(i=1;i<=n-1;i++)
        {
            for(k=1;k<=i;k++)
            {
                System.out.print(" ");
            }
            for(j=n-1;j>=i;j--)
            {
                System.out.print("*");
                System.out.print(" ");
            }
            System.out.print("\n");
        }
    }
}

To print the given pattern

Question 31 : Write a program in Java to print the following pattern : 

1
11
121
1331
14641

Java Program : 

class pattern_19
{
 static void main()
 {
     int i,j;
     int s=1;
     System.out.println(s);
     for(i=1;i<=4;i++)
     {
         for(j=1;j<=i;j++)
         {
             s=s*11;
            }
            System.out.println(s);
            s=1;
        }
    }
}

ShareThis