Welcome to Program Diary

Program Diary emphasis on executable,readable and understandable program

Lets move to C

Programming is similar to a game of golf. The point is not getting the ball in the hole but how many strokes it takes.

Cplusplus

Fifty years of programming language research, and we end up with

Java World

If Java had true garbage collection, most programs would delete themselves upon execution

.NET

........Processing

Wednesday, October 17, 2012

stack example First of all,what is stack? A stack is a data structure in which addition of new element or deletion of existing element always takes place at the same end. The end is often known as Top of Stack. For example a no. of books kept one above another, if we have to keep a new book there, we will kept it above all the books and suppose we have to take out a book at first we will pick the book which is at the t
1) Hello world program.
codepublic class HelloWorld
{
    public static void main(String[] args)
    {    

//System.out.println is used to print on console.


        System.out.println("Hello Welcome to the World of JAVA");
    }
}

Output:

Hello Welcome to the World of JAVA


open
2) Program to print multiplication table of a number.
codeimport java.util.*;
public class MultiplicationTable
{
    public static void main(String[] args)
    {
        int number;
        Scanner in=new Scanner(System.in);
        System.out.print("Enter a number : ");
        number=in.nextInt();        

//Let the number is "11".


        System.out.println("Table of "+number+" is:");
        for(int i=1;i<=10;i++)      

//"i" will have values 1 to 10.


        {
            System.out.println(number+" * "+i+" = "+number*i);  
        }
    }
}

Output:

Enter a number : 11
Table of 11 is:
11 * 1 = 11
11 * 2 = 22
11 * 3 = 33
11 * 4 = 44
11 * 5 = 55
11 * 6 = 66
11 * 7 = 77
11 * 8 = 88
11 * 9 = 99
11 * 10 = 110


open
3)Program to find out whether a number is prime or not.
codeimport java.util.*;
public class PrimeNumber
{
   

//A number is prime if it is divisible only by '1' and itself.


    public static void main(String[] args)
    {
        int number,div=2;
        Scanner in=new Scanner(System.in);
        System.out.print("Enter a number : ");
        number=in.nextInt();        

/*Let the number is "11"
 This loop divides the given number by 2 to number(in this case 2,3,4,5..11)
 and stops when any of these numbers(2 to 11) completely divides the given number.*/


        while(number%div!=0)          
        {                            
            div++;                  
        }

/*if (11==11) that means the number was only divisible by itself and ,obviously, '1'.
 that means it is a prime number otherwise it is composite number(non-prime).*/


        if(number==div)              
        {
            System.out.println(number+" is a prime number");
        }
        else        
        {
            System.out.println(number+" is not a prime number");
        }
    }
}

Output:

Enter a number : 191
191 is a prime number


open
4) Program to find out whether a number is even or odd.
codeimport java.util.*;
public class EvenOrOdd
{
   

//A number is an even number if it leaves '0' remainder when divided by '2'.


    public static void main(String[] args)
    {
        int number;
        Scanner in=new Scanner(System.in);
        System.out.print("Enter a number : ");
        number=in.nextInt();        

/*Let the number is '223'
 '%' operator(called modulus operator) returns the remainder value
 i.e. 11%2 will return '1',21%3 will return 0.*/


        if(number%2==0)              
        {                            
            System.out.println(number+" is an even number");
        }
        else                        
        {
            System.out.println(number+" is a odd number");
        }
    }
}

Output:

Enter a number : 223
223 is a odd number


open
5) Program to find factorial of a number.
codeimport java.util.*;
public class Factorial
{
   

//Factorial of a number say 5 is 5*4*3*2*1.


    public static void main(String[] args)
    {
        factorial f=new factorial();      

//Object of factorial class


        int number;
        Scanner in=new Scanner(System.in);
        System.out.print("Enter a number : ");
        number=in.nextInt();              

//Let the number is '6'.


        System.out.println("Factorial of "+number+" is :(6*5*4*3*2*1) = "+f.fact(number));

//factorial object used to call 'fact(int n)'.


    }
}
class factorial
{
   

/*Recursive method=it is executed again and again until a particular situation is met
(in our case,until n is greater than equal to '1').*/


    int fact(int n)        
    {
        if(n>=1)            

//This is executed if number is greater than '1'.

 
        {                  

//First Iteration: 6 * fact(5)


/*                    

//Second Iteration:30 * fact(4)


             return n*fact(n-1);  

////Third Iteration:120 * fact(3)


               

//Fourth Iteration:360 * fact(2)


        }                  

//Fifth Iteration:720 * fact(1)


        else                

//if the number is '0',its factorial is '0'.


        {
             return 0;
        }
    }
}

Output:

Enter a number : 6
Factorial of 6 is :(6*5*4*3*2*1) = 720


open
6) Program to swap 2 numbers using a third variable.
codeimport java.util.*;
public class Swap
{
    public static void main(String[] args)
    {
        int a,b,temp;
        Scanner in=new Scanner(System.in);
        System.out.print("Enter two numbers \n a is : ");
        a=in.nextInt();                            

//Let a=35;


        System.out.print(" b is : ");
        b=in.nextInt();                            

//Let b=23


        System.out.println("Before Swapping the numbers are : \n a is "+a+"\n b is "+b);
        temp=a;                                    

//temp=35


        a=b;                                        

//a=23


        b=temp;                                    

//b=35


        System.out.println("After Swapping the numbers are : \n a is "+a+"\n b is "+b);
    }
}

Output:

Enter two numbers
 a is : 235
 b is : 1992
Before Swapping the numbers are :
 a is 235
 b is 1992
After Swapping the numbers are :
 a is 1992
 b is 235


open
7) Program to swap 2 numbers without using third variable.
codeimport java.util.*;
public class SwapUsingTwoVariables
{
    public static void main(String[] args)
    {
        int a,b,temp;
        Scanner in=new Scanner(System.in);
        System.out.print("Enter two numbers \n a is : ");  
        a=in.nextInt();                                

//let a=10


        System.out.print(" b is : ");
        b=in.nextInt();                                

//let b=20


        System.out.println("Before Swapping the numbers are : \n a is "+a+"\n b is "+b);
        a=a+b;              

//a=10+20=30


        b=a-b;              

//b=30-20=10


        a=a-b;              

//a=30-10=20


        System.out.println("After Swapping the numbers are : \n a is "+a+"\n b is "+b);
    }
}

Output:

Enter two numbers
 a is : 876
 b is : 123
Before Swapping the numbers are :
 a is 876
 b is 123
After Swapping the numbers are :
 a is 123
 b is 876


open
8) Program to find out whether a string is palindrome or not.
codeimport java.util.*;
public class Palindrome
{
    public static void main(String[] args)
    {
        String OriginalString,ReversedString="";
        int length;
        Scanner in=new Scanner(System.in);
        System.out.print("Enter a string to find out whether it is palindrome or not : ");
        OriginalString=in.next();                
        length=OriginalString.length();          

/*Let string is "nitin"
length=5 i.e. the length of "nitin"
values for "i" will be 4,3,2,1,0*/


        for(int i=length-1;i>=0;i--)            
        {              
     ReversedString=ReversedString+OriginalString.charAt(i);                      
 

/*when i=4:ReversedString= "" + "n"(charAt position 4 in OriginalString)
 when i=3:ReversedString= "n" + "i"(charAt position 3 in OriginalString)
 when i=2:ReversedString= "ni" + "t"(charAt position 2 in OriginalString)
 when i=1:ReversedString= "nit" + "i"(charAt position 1 in OriginalString)
 when i=0:ReversedString= "niti" + "n"(charAt position 0 in OriginalString) */


        }
        if(OriginalString.equals(ReversedString))  

//if("nitin"=="nitin")


        {
             System.out.println(OriginalString+" is a Palindrome");
        }
        else
        {
            System.out.println(OriginalString+" is not a Palindrome");
        }
    }
}

Output:

Enter a string to find out whether it is palindrome or not : java
java is not a Palindrome


open
9) Program to find reverse of a string.
codeimport java.util.*;
public class ReverseString
{
    public static void main(String[] args)
    {
        String OriginalString,ReversedString="";
        int length;
        Scanner in=new Scanner(System.in);
        System.out.print("Enter a string : ");
        OriginalString=in.next();                
        length=OriginalString.length();          

/*Let string is "abcde"
length=5 i.e. the length of "abcde"
values for "i" will be 4,3,2,1,0 */


        for(int i=length-1;i>=0;i--)            
        {        
      ReversedString=ReversedString+OriginalString.charAt(i);                            
 

/*when i=4:ReversedString= "" + "e"(charAt position 4 in OriginalString)
 when i=3:ReversedString= "e" + "d"(charAt position 3 in OriginalString)
 when i=2:ReversedString= "ed" + "c"(charAt position 2 in OriginalString)
 when i=1:ReversedString= "edc" + "b"(charAt position 1 in OriginalString)
 when i=0:ReversedString= "edcb" + "a"(charAt position 0 in OriginalString)*/


        }
        System.out.println("Original String was "+OriginalString);
        System.out.println("Reversed String is "+ReversedString);
    }
}

Output:

Enter a string : umesh
Original String was umesh
Reversed String is hsemu


open
10) Program to find reverse of a number.
codeimport java.util.*;
public class ReverseNumber
{
    public static void main(String[] args)
    {
        int OriginalNumber,ReversedNumber=0;
        Scanner in=new Scanner(System.in);
        System.out.print("Enter a number : ");
        OriginalNumber=in.nextInt();      

//Let the number be "546"


/*This loop will execute until original number is not equal to zero.
 When original number is zero,loop will exit.*/


        while(OriginalNumber!=0)      
        {
               int r=OriginalNumber%10;
               OriginalNumber=OriginalNumber/10;
               ReversedNumber=ReversedNumber*10+r;
 

/*First iteration: r=546%10=6,OriginalNumber=546/10=54,ReversedNumber=(0*10)+6=6
    Second iteration: r=54%10=4,OriginalNumber=54/10=5,ReversedNumber=(6*10)+4=64
    Third iteration: r=5%10=5,OriginalNumber=5/10=0,ReversedNumber=64*10 + 5=645
    In short,loop will 3 times i.e. the number of digits in number.*/


        }                          
        System.out.print("Reversed Number is "+ReversedNumber);
    }
}

Output:

Enter a number : 12345
Reversed Number is 54321


open

Tuesday, October 16, 2012

Stack implementation using C++

stack example


First of all,what is stack? A stack is a data structure in which addition of new element or deletion of existing element always takes place at the same end. The end is often known as Top of Stack. For example a no. of books kept one above another, if we have to keep a new book there, we will kept it above all the books and suppose we have to take out a book at first we will pick the book which is at the top.