Full Range

Call me (USA Free)

Monday 14 April 2014

String programs.....

1 comment
Q.Write a java program to count the number of words in the entered sentence

 import java.io.*;
public class CountWords
{
  public static void main(String args[])throws Exception
    {
       BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));
       System.out.print("Enter the sentence: ");
       String s=stdin.readLine();
       s=' '+s;
       char ch;
       int w=0;
       for(int i=0;i<s.length();i++) //loop is used to extract each character in the sentence
        {
           ch=s.charAt(i);
           if(ch==' ')
            {
             w++;
            }
        }
     System.out.println("Number of word in the sentence:"+w);
    } //method
} //class


Q.Write a program to accept a word and  arrange the letters of words in alphabetical order
    Sample input: computer
                output:cemoprtu

import java.io.*;
public class WordAlbha
 {
     public static void main(String args[])throws Exception
     {
         InputStreamReader isr =new InputStreamReader(System.in);
         BufferedReader stdin=new BufferedReader(isr);
         System.out.print("Enter the word:");
         String word=stdin.readLine();
         char ch;
         for(int i=65;i<=90;i++)
          {
              for(int j=0;j<word.length();j++)
               {
                   ch=word.charAt(j);
                   ch=Character.toLowerCase(ch);
                   if(ch==(char)i||ch==(char)(i+32))
                   System.out.print(ch);
                }
           }
        }
    }    

Read More

Printing Patterns....

Leave a Comment
Q.Write a program to print the following pattern
  
    *
     * *
     * * *
     * * * *
     * * * * *       

public class HalfTriangle
 {
     public void pattern(int n)
      {
          for(int i=1;i<=n;i++) //to define the number of rows
            {
              for(int j=1;j<=i;j++)
               {
                  System.out.print("* ");
                }
             System.out.println();
            }
      } //method
  } //class
Read More

Simple programs for beginner's

Leave a Comment

Q.Write a java program to add two numbers.

     public class  addNum
    {
        public void sum(int x,int y) // creates the instance of a class
       {
   
           int z=x+y;
           System.out.print(z);
       }
 
   }
 
  Q.Write a java program to subtract two numbers.

      public class Difference
       {
           public void diff(int x,int y)
            {
              int z=x-y;
              System.out.println("The difference between "+x+" and "+y+" is "+z);
            }
      }
 Q.Write a java program to check whether the entered number is prime or composite...
     
      import java.io.*;
     public class Prime
    {
         public static void main(String args[])throws Exception
          {
             BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));
             System.out.print("Enter the number: ");
             int n=Integer.parseInt(stdin.readLine());
             int flag=0;
              for(int i=2;i<n;i++) //used to get the vales less than the entered number in loop
               {
                   if(n%i==0) //checks if the number is divisble by i
                    {
                       flag=1;
                       break; //exits from the loop
                    }
       
               }
               if(flag==0)
               {
                  System.out.println(n+" is a prime number");
               }
             else
             {
                System.out.println(n+" is a composite number");
            }
    } //method
} //class

Q.Write a java program to reverse the entered number

    import java.io.*;
   public class Reverse
    {
        public static void main(String args[])throws Exception
        {
          BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));
          System.out.print("Enter the number: ");
          int n=Integer.parseInt(stdin.readLine());
          int nr=0;
         while(n>0) //loop used to reverse the number
         {
          nr=(nr*10)+(n%10); //(n%10):extracts the last digit of the number
          n=n/10;
         }
         System.out.print(nr);
      } //method
 } //class

Read More
Leave a Comment
This blog is mainly a guide for isc computer science students...Some solved questions are given in this blog and also guidance to java progaram....icse students can also refer to this site for learning programs.

Read More