Search This Blog

Loading...

Monday, January 19, 2015

Disarium Numbers upto 2000 Using BlueJ Program




class Disarium
{
public void show()
{
int c,rev,sum,n,x;

for(int i=1;i<=2000;i++)
{
c=1;
 n=i;x=n;
sum=0;
rev=0;
while (n>0)
{
 rev=rev*10+n%10;
 n=n/10;
 }
 while(rev>0)
 {
  sum=sum+(int)Math.pow(rev%10,c);
  c++;
  rev=rev/10;
  }
  if(sum==x)
  System.out.println(x + " is DISARIUM");
 }
}
public static void main(String args[]) throws Exception
{
Disarium ob=new Disarium ();
ob.show();
}
}

How the Program is working


The numbers from 1 to 2000 are generated using the for loop for (int i=0;i<=2000;i++). The number is then reversed using a while loop and the reversed value is stored in variable 'rev'. The need of reversing the number is for getting the digits from left to right. (suppose the number is 124, 124%10 will give 4 but we need the left most digit '1' first so the number is reversed. 421%10 will give 1.). 'c' is a counter which is counting the position of each digit in the number - we are getting the value of 'c' as 1 for '1' of the number 124, 'c' as 2 for 2 of 124 and finally 'c' as 3 for 4 of 124 and value of 'c' is used as power of the respective digits of the number.

Friday, January 16, 2015

Words with vowel at end BlueJ Program


This is a BlueJ program to display the words of a sentence with vowel at the end. User will enter any  sentence and the words in the sentence which ends with a vowel will be displayed.

This program can be done with the help of indexOf() and substring () functions of String class as well as without using those two functions.

Here is the BlueJ program using indexOf () and substring () functions.


import java.io.*;
class String1
{
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   public void show() throws Exception
    {
       String str1,s1;
   int i;
   System.out.print("Enter the sentence:");
        str1=br.readLine();
         System.out.println("words in the sentence which ends with a vowel are");
        while(true)
        {
            i=str1.indexOf(" ");
            if(i<0 p="">            break;
            s1=str1.substring(0,i);
            str1=str1.substring(i+1);
            if(vowelChk(s1))
            System.out.println(s1);
        }
        if(vowelChk(str1))
            System.out.print(" "+str1);
       
        }
        private boolean vowelChk(String s)
        {
           boolean bool=false;
           s=s.toUpperCase();
           switch(s.charAt(s.length()-1))
           {
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                bool=true;
            }
            return bool;
        }
       
        public static void main(String args[])throws Exception
        {
             String1 ob=new String1();
             ob.show();
            }
        }
       
       
        You can also do the program with appending a space at the end of the sentence.
       
       
       import java.io.*;
class String1
{
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   public void show() throws Exception
    {
       String str1,s1;
   int i;
   System.out.print("Enter the sentence:::");
        str1=br.readLine().trim()+" ";
         System.out.println("words in the sentence which ends with a vowel are");
        while(true)
        {
            i=str1.indexOf(" ");
            if(i<0 p="">            break;
            s1=str1.substring(0,i);
            str1=str1.substring(i+1);
            if(vowelChk(s1))
            System.out.println(s1);
        }
           
        }
        private boolean vowelChk(String s)
        {
           boolean bool=false;
           s=s.toUpperCase();
           switch(s.charAt(s.length()-1))
           {
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                bool=true;
            }
            return bool;
        }
       
        public static void main(String args[])throws Exception
        {
             String1 ob=new String1();
             ob.show();
            }
        }

         
        Here is the program without using indexOf() and substring () function
       
           
       import java.io.*;
class String1
{
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   public void show() throws Exception
    {
       String str1,s1="";
   int i,len;
   char ch;
   System.out.print("Enter the sentence:");
        str1=br.readLine().trim()+" ";
         System.out.print("words in the sentence which ends with a vowel are");
         len=str1.length();
        for(i=0;i< len;i++)
        {
            ch=str1.charAt(i);
            if (ch!=' ')
            s1=s1+ch;
            else
            {
            if(vowelChk(s1))
            System.out.println(s1);
            s1="";
        }
    }
}
        private boolean vowelChk(String s)
        {
           boolean bool=false;
           s=s.toUpperCase();
           switch(s.charAt(s.length()-1))
           {
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                bool=true;
            }
            return bool;
        }
       
        public static void main(String args[])throws Exception
        {
             String1 ob=new String1();
             ob.show();
            }
        }


       
        All the three programs are perfect. Time complexity of the third program is higher than the first two programs as in the third program the loop executes more times than the first two programs. ICSE and ISC students can solve the program using any one of the techniques.
       
        I want feed back from students specially on using indexOf() and substring () functions. Computer teacher of a famous Burdwan school deducts marks if any student of his school uses indexOf() and substring () function. I came to know about the matter from one of the students of the school. I don't know
        whether this is due to lack of knowledge or some other reasson. The same teacher said earlier that 'public static void main (String args[])' is not the main function of java program.

CHECK HERE FOR PREVIOUS POSTS

Google+ Followers

Subscribe via email

Enter your email address:

Delivered by FeedBurner