Friday, May 20, 2016

Differences between String and StringBuffer and StringBuilder in Java

String :

Consider the bellow program first,

                                                 String s = new String(" Manoj");
                                                 s.concat("Patra");
                                                 System.out.println(s);  //Manoj
  1. String objects are immutable i.e once we created a String object we can't perform any changes in the existing object. 
  2. If we try to perform any change with those change a new object will be created.
  3. In the first line of the above program we are creating a string object 's' which contains 'Manoj'
  4. In the second line we are trying to do some change so, another new object will be created with content 'ManojPatra'
  5. In 3rd line we are printing 's'. Since 's' is still pointing to old object only. It will print 'Manoj'. 
Summery : Once we create a string object we can't perform any changes in the existing object. If we are trying to perform any changes with those changes a new object will be created. This non changeable nature is nothing but immutability of string objects.

StringBuffer :


Consider the bellow program first,

                                               StringBuffer sb = new StringBuffer(" Manoj");
                                               sb.append("Patra");
                                               System.out.println(s);  //ManojPatra
  1. StringBuffer objects are mutable i.e after creating a StringBuffer object we can perform any changes in the existing object. 
  2. If we perform any changes , all those changes will be reflected in the same object.
  3. In the first line of the above program we are creating a StringBuffer object 'sb' which contains 'Manoj'
  4. In the second line we are trying to do some change so, all changes will be performed on same object.
  5. In 3rd line we are printing 'sb'. It will print 'ManojPatra'. 
Summery : Once we create a string object we can perform any type of changes in the existing object. This changeable nature is nothing but mutability of StringBuffer object.

Difference Between StringBuffer and StringBuilder?

StringBuffer is exactly same as StringBuilder ( including Methods and Construvtor) except the following difference.

StringBuffer :

  1. Every method present in StringBuffer is synchronized.
  2. At a time only one thread is allowed to operate on StringBuffer object. Hence StringBuffer object is thread safe.
  3. It increases waiting time of threads and hence relatively performance is low.
  4. Introduced in 1.0 version.

StringBuilder :

  1.  No method present in StringBuilder is synchronized.
  2. At a time multiple threads are allowed to operate on StringBuilder object. Hence StringBuilder object is not thread safe.
  3. Threads are not required to wait to operate on StringBuilder object and hence performance is relatively high.
  4. Introduced in 1.5 version.

When to use String, StringBuffer and StringBuilder?

  1. If the content is fixed and won't change frequently then we should go for String.
  2. If the content is not fixed and keep on changing but Thread safety is required then we should go for StringBuffer.
  3. If the content is not fixed and keep on changing but Thread safety is NOT required then we should go for StringBuffer.

Difference between final, finally, finalize() in Java ?

final :

  1. final is a modifier applicable for classes, methods and variables. If a class declared as final then we cant extend that class. i.e we cant create child class for that class.
  2. If a method declared as final then we can't override that method in the child class.
  3. If a variable declared as final then it will become constant and we can't perform re-assignment for that variables.

finally :

  1. finally is a block always associated with try-catch to maintain cleanup code.
                                                                   try
                                                                   {
                                                                            //risky code
                                                                   }
                                                                   catch( Exception e)
                                                                   {
                                                                            //handling code
                                                                   }
                                                                   finally
                                                                   {
                                                                              //cleanup code
                                                                   }

finalize() :

  1. finalize() is a method which is always invoked by garbage collector just before destroying an object to perform cleanup activities.
Note : Both finally and finalize() are meant for cleanup activity only but, 
  1. the finally block is responsible for cleanup activity related to try block i.e whatever resources are opened in try block will be closed by finally block.
  2. The finalize() method is associated with the object i.e all the resources related to object will be closed by finalize() method. The garbage collector is responsible for calling finalize() method just before destroying an object.



Tuesday, April 5, 2016

life

= LIFE =

Sometimes 'life' closes doors because it’s time to move forward.  And that’s a good thing because we often won’t move unless circumstances force us to.  When times are tough, remind yourself that no pain comes without a purpose.  Move on from what hurt you, but never forget what it taught you.  Just because you’re struggling doesn’t mean you’re failing.  Every great success requires some type of worthy struggle to get there.  Good things take time.  Stay patient and stay positive.  Everything is going to come together; maybe not immediately, but eventually.

Every time it rains, it stops raining.  Every time you get hurt, you heal.  After darkness there is always light – you are reminded of this every morning, but still you often forget, and instead choose to believe that the night will last forever.  It won’t.  Nothing lasts forever.

So if things are good right now, enjoy it.  It won’t last forever.  If things are bad, don’t worry because it won’t last forever either.  Just because life isn’t easy at the moment, doesn’t mean you can’t laugh.  Just because something is bothering you, doesn’t mean you can’t smile.  Every moment gives you a new beginning and a new ending.  You get a second chance, every second.  You just have to take it and make the best of it.

Those who complain the most, accomplish the least.  It’s always better to attempt to do something great and fail than to attempt to do nothing and succeed.  It’s not over if you’ve lost; it’s over when you do nothing but complain about it.  If you believe in something, keep trying.  Don’t let the shadows of the past darken the doorstep of your future.  Spending today complaining about yesterday won’t make tomorrow any brighter.  Take action instead.  Let what you’ve learned improve how you live.  Make a change and never look back.

And regardless of what happens in the long run, remember that true happiness begins to arrive only when you stop complaining about your problems and you start being grateful for all the problems you don’t have.

In life, patience is not about waiting; it’s the ability to keep a good attitude while working hard on your dreams, knowing that the work is worth it.  So if you’re going to try, put in the time and go all the way.  Otherwise, there’s no point in starting.  This could mean losing stability and comfort for a while, and maybe even your mind on occasion.  It could mean not eating what, or sleeping where, you’re used to, for weeks on end.  It could mean stretching your comfort zone so thin it gives you a nonstop case of the chills.  It could mean sacrificing relationships and all that’s familiar.  It could mean accepting ridicule from your peers.  It could mean lots of time alone in solitude.  Solitude, though, is the gift that makes great things possible.  It gives you the space you need.  Everything else is a test of your determination, of how much you really want it.

And if you want it, you’ll do it, despite failure and rejection and the odds.  And every step will feel better than anything else you can imagine.  You will realize that the struggle is not found on the path, it is the path.  And it’s worth it.  So if you’re going to try, go all the way.  There’s no better feeling in the world… there’s no better feeling than knowing what it means to be ALIVE.

Be positive when negativity surrounds you.  Smile when others try to bring you down.  It’s an easy way to maintain your enthusiasm and focus.  When other people treat you poorly, keep being you.  Don’t ever let someone else’s bitterness change the person you are.  You can’t take things too personally, even if it seems personal. Rarely do people do things because of you.  They do things because of them.

Above all, don’t ever change just to impress someone who says you’re not good enough.  Change because it makes you a better person and leads you to a brighter future.  People are going to talk regardless of what you do or how well you do it.  So worry about yourself before you worry about what others think.  If you believe strongly in something, don’t be afraid to fight for it.  Great strength comes from overcoming what others think is impossible.

All jokes aside, your life only comes around once.  This is IT.  So do what makes you happy and be with whoever makes you smile, often.

True strength comes when you have so much to cry and complain about, but you prefer to smile and appreciate your life instead.  There are blessings hidden in every struggle you face, but you have to be willing to open your heart and mind to see them.  You can’t force things to happen.  You can only drive yourself crazy trying.  At some point you have to let go and let what’s meant to be, BE.


In the end, loving your life is about trusting your intuition, taking chances, losing and finding happiness, cherishing the memories, and learning through experience.  It’s a long-term journey.  You have to stop worrying, wondering, and doubting every step of the way.  Laugh at the confusion, live consciously in the moment, and enjoy your life as it unfolds.  You might not end up exactly where you intended to go, but you will eventually arrive precisely where you need to be.

Don’t be afraid to get back up – to try again, to love again, to live again, and to dream again.  Don’t let a hard lesson harden your heart.  Life’s best lessons are often learned at the worst times and from the worst mistakes.  There will be times when it seems like everything that could possibly go wrong is going wrong.  And you might feel like you will be stuck in this rut forever, but you won’t.  When you feel like quitting, remember that sometimes things have to go very wrong before they can be right.  Sometimes you have to go through the worst, to arrive at your best.

Yes, life is tough, but you are tougher.  Find the strength to laugh every day.  Find the courage to feel different, yet beautiful.  Find it in your heart to make others smile too.  Don’t stress over things you can’t change.  Live simply.  Love generously.  Speak truthfully.  Work diligently.  And even if you fall short, keep going.  Keep growing.

Awake every morning and do your best to follow this daily TO-DO list:    
    1.      Think positively.
    2.      Eat healthy.
    3.      Exercise today.
    4.      Worry less.
    5.      Work hard.
    6.      Laugh often.
    7.      Sleep well.
    Repeat all..............
                                                                              Source: Internet

Saturday, March 19, 2016

C Program to calculate x^y using recursive function.

/*=================================================================================
 **
 **  File Name     : powRecursion.c
 **  Creation Date : Sat 19 Mar 2016 06:02:15 AM IST
 **  Last Modified : Sat 19 Mar 2016 06:15:27 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
long int power(int x, int y);
int i=1;
long int sum=1;
int main()
{
    printf("\n= = = = = Calculate power using recursive function = = = = =\n");
    int no, pow;
    long int result;
    printf("\nEnter a Number : ");
    scanf("%d",&no);
    printf("Enter the Power : ");
    scanf("%d",&pow);
    result = power(no,pow);
    printf("\n%d to the power %d is: %ld\n",no,pow,result);
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}
long int power(int x, int pow)
{
    if(i<=pow)
    {
        sum=sum*x;
        power(x,pow-1);
    }
    else
        return sum;
}

OutPut:

max@ubuntu:~/cprog$ gcc powRecursion.c -o powRecursion
max@ubuntu:~/cprog$ ./powRecursion

= = = = = Calculate power using recursive function = = = = =

Enter a Number : 2
Enter the Power : 10

2 to the power 10 is: 1024


= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to perform Arithematic Operation using function

/*=================================================================================
**
**  File Name     : mathOpe.c
**  Creation Date : Sat 19 Mar 2016 05:25:39 AM IST
**  Last Modified : Sat 19 Mar 2016 05:43:16 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**================================================================================*/
#include<stdio.h>
int addition(int x, int y);
int subtraction(int x, int y);
int multiplication(int x, int y);
int division(int x, int y);
int main()
{
    printf("\n= = = = = Arithematic Operation using function = = = = =\n");
    int n1, n2, sum, sub, mul, div;
    printf("\nEnter the value of 'n1' : ");
    scanf("%d",&n1);
    printf("Enter the value of 'n2' : ");
    scanf("%d",&n2);
    sum = addition(n1,n2);
    printf("\nThe sum of %d and %d is : %d\n",n1,n2,sum);
    sub = subtraction(n1,n2);
    printf("The subtraction of %d from %d is : %d\n",n2,n1,sub);
    mul = multiplication(n1,n2);
    printf("The multiplication  of %d and %d is : %d\n",n1,n2,mul);
    div = division(n1,n2);
    printf("The division of %d by %d is : %d",n1,n2,div);
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");

}
int addition(int x, int y)
{
    int k;
    k = x+y;
    return k;
}
int subtraction(int x, int y)
{
    int k;
    k = x-y;
    return k;
}
int multiplication(int x, int y)
{
    int k;
    k = x*y;
    return k;
}
int division(int x, int y)
{
    int k;
    k = x/y;
    return k;
}

OutPut:

max@ubuntu:~/cprog$ gcc mathOpe.c -o mathOpe
max@ubuntu:~/cprog$ ./mathOpe

= = = = = Arithematic Operation using function = = = = =

Enter the value of 'n1' : 10
Enter the value of 'n2' : 4

The sum of 10 and 4 is : 14
The subtraction of 4 from 10 is : 6
The multiplication  of 10 and 4 is : 40
The division of 10 by 4 is : 2

= = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Friday, March 18, 2016

C Program to print all prime numbers between any two given numbers

/*=================================================================================
 **
 **  File Name     : primePrint.c
 **  Creation Date : Sat 19 Mar 2016 05:08:00 AM IST
 **  Last Modified : Sat 19 Mar 2016 05:14:34 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include <stdio.h>
int main()
{
    printf("\n= = = = = = = = = C Program to print Prime numbers = = = = = =\n");
    int n1, n2, i, j, stat;
    printf("\nEnter the first number : ");
    scanf("%d", &n1);
    printf("Enter the second number : ");
    scanf("%d", &n2);
    printf("\nThe Prime numbers between %d and %d are : \n", n1, n2);
    for(i=n1+1; i<n2; ++i)
    {
        stat=0;
        for(j=2; j<=i/2; ++j)
        {
            if(i%j==0)
            {
                stat=1;
                break;
            }
        }
        if(stat==0)
        printf("%d ",i);
    }
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}

OutPut:

max@ubuntu:~/cprog$ gcc primePrint.c -o primePrint
max@ubuntu:~/cprog$ ./primePrint

= = = = = = = = = C Program to print Prime numbers = = = = = =

Enter the first number : 2
Enter the second number : 50

The Prime numbers between 2 and 50 are :
3 5 7 11 13 17 19 23 29 31 37 41 43 47

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Write a C Program to check if a string is pallindrome or not

/*=================================================================================
**
**  File Name     : stringPall.c
**  Creation Date : Sat 19 Mar 2016 04:44:11 AM IST
**  Last Modified : Sat 19 Mar 2016 04:59:05 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**===============================================================================*/
#include<stdio.h>
#include<string.h>
int main()
{
    printf("\n= = = = = C Program to pallindrome check = = = = =\n");
    char string[100];
    int i, len, stat=0;
    printf("\nEnter the String : ");
    scanf("%s",string);
    len = strlen(string);
    for(i=0; i<len; i++)
    {
        if(string[i] != string[len-i-1])
        {
            stat = 1;
            break;
        }
    }
    if(stat == 1)
    {
        printf("\n'%s' is NOT a pallindrome\n",string);
    }
    else
    {
        printf("\n'%s' is a pallindrome\n",string);
    }
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}

OutPut:

max@ubuntu:~/cprog$ gcc stringPall.c -o stringPall
max@ubuntu:~/cprog$ ./stringPall

= = = = = C Program to pallindrome check = = = = =

Enter the String : manoj

'manoj' is NOT a pallindrome


= = = = = = = = = = = = = = = = = = = = = = = = = =
max@ubuntu:~/cprog$ ./stringPall

= = = = = C Program to pallindrome check = = = = =

Enter the String : abcdcba

'abcdcba' is a pallindrome


= = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to print the fibonacci series upto 'n' terms except first two

/*=================================================================================
 **
 **  File Name     : fibonacci.c
 **  Creation Date : Sat 19 Mar 2016 03:58:49 AM IST
 **  Last Modified : Sat 19 Mar 2016 04:14:17 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
int main()
{
    printf("\n= = = = = Fibonacci Series upto 'n' terms = = = = =\n");
    int t1, t2, n, sum, count = 0;
    printf("\nEnter the first term : ");
    scanf("%d",&t1);
    printf("Enter the second term : ");
    scanf("%d",&t2);
    printf("Enter the number of terms you want to print : ");
    scanf("%d",&n);
    printf("\nThe first two term of fibonacci series are : %d , %d\n", t1, t2);
    printf("Remaining %d terms are : ",n);
    while (count < n)
    {
        sum = t1 + t2;
        printf("%d  ", sum);
        t1 = t2;
        t2 = sum;
        count++;
    }
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}

OutPut:

max@ubuntu:~/cprog$ gcc fibonacci.c -o fibonacci
max@ubuntu:~/cprog$ ./fibonacci

= = = = = Fibonacci Series upto 'n' terms = = = = =

Enter the first term : 3
Enter the second term : 5
Enter the number of terms you want to print : 10

The first two term of fibonacci series are : 3 , 5
Remaining 10 terms are : 8  13  21  34  55  89  144  233  377  610 

= = = = = = = = = = = = = = = = = = = = = = = = = =

Write a C Program to find the sum of factorial of 'n' numbers

/*=================================================================================
**
**  File Name     : sumFact.c
**  Creation Date : Sat 19 Mar 2016 03:38:50 AM IST
**  Last Modified : Sat 19 Mar 2016 03:50:55 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**===============================================================================*/

#include<stdio.h>
int main()
{
        printf("\n= = = = = = = C Program to find sum of 'n' factorial = = = = = = =\n");
        int n, i, j, sum=0, fact=1;
        printf("\nEnter the value of 'n' : ");
        scanf("%d",&n);
        for(i=1; i<=n; i++)
        {
                for(j=1; j<=i; j++)
                {
                        fact=fact*j;
                }
                sum=sum+fact;
                fact=1;
        }
        printf("\nThe sum of factorial of 1 to %d is : %d\n",n,sum);
        printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}

OutPut:

max@ubuntu:~/cprog$ gcc sumFact.c -o sumFact
max@ubuntu:~/cprog$ ./sumFact

= = = = = = = C Program to find sum of 'n' factorial = = = = = = =

Enter the value of 'n' : 6

The sum of factorial of 1 to 6 is : 873

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
max@ubuntu:~/cprog$ ./sumFact

= = = = = = = C Program to find sum of 'n' factorial = = = = = = =

Enter the value of 'n' : 10

The sum of factorial of 1 to 10 is : 4037913

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Monday, March 14, 2016

C Program to Multiply two Matrix Using Function

/*=================================================================================
 **
 **  File Name     : matMul.c
 **  Creation Date : Mon 14 Mar 2016 03:41:26 AM IST
 **  Last Modified : Mon 14 Mar 2016 05:46:23 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
void insert_ele(int a[10][10],int row,int col)
{
    int i,j;
    for(i=1;i<=row;i++)
    {
        for(j=1;j<=col;j++)
        {
            printf("Enter Element of row=%d , column= %d : ",i,j);
            scanf("%d",&a[i][j]);
        }
    }
}

void mul_mat(int m1[10][10],int m2[10][10],int m3[10][10],int r1,int c1, int r2, int c2)
{
    int i,j,k,sum=0;
    for(i=0; i<r1; i++)
    {
        for(j=0; j<c2; j++)
        {
            m3[i][j]=0;
        }
    }
    for(i=1;i<=r1;i++)
    {
        for(j=1;j<=c2;j++)
        {
            for (k=1;k<=c1;k++)
            {
                sum = sum + (m1[i][k] * m2[k][j]);
            }
            m3[i][j]=sum;
            sum=0;
        }
    }
}

void print_mat(int m[10][10],int row,int col)
{
    int i,j;
    for(i=1;i<=row;i++)
    {
        for(j=1;j<=col;j++)
        {
            printf("%d ",m[i][j]);
        }
        printf("\n");
    }
}

main()
{
    int m1[10][10],m2[10][10],m3[10][10],row,col,r1,c1,r2,c2;
    printf("\n= = = = = = Matrix Multiplication = = = = = =\n\n");
    printf("Enter number of rows in first matrix: ");
    scanf("%d",&r1);
    printf("Enter number of colomns in second matrix: ");
    scanf("%d",&c1);
    insert_ele(m1,r1,c1);
    printf("\nThe elements of first matrix are:\n");
    print_mat(m1,r1,c1);
    printf("- - - - - - - - - - - - - - - - - - - - - - - - -\n");
    printf("\nEnter number of rows in second matrx: ");
    scanf("%d",&r2);
    while(r2 != c1)
    {
        printf("Error: Number of rows in 2nd matrix must be same as number of columns in 1st matrix\n");
        printf("\nEnter number of rows in second matrx: ");
        scanf("%d",&r2);
    }
    printf("Enter number of colomns in second matrix: ");
    scanf("%d",&c2);
    insert_ele(m2,r2,c2);
    printf("\nThe elements of the second matrix are:\n");
    print_mat(m2,r2,c2);
   
    /*----------------------------------------------------*/
    mul_mat(m1,m2,m3,r1,c1,r2,c2);
    printf("- - - - - - - - - - - - - - - - - - - - - - - - -\n");
    printf("\nThe resultant matrix is:\n");
    print_mat(m3,r1,c2);
    printf("- - - - - - - - - - - - - - - - - - - - - - - - -\n");
    printf("\n");
}

OutPut:

max@ubuntu:~$ gcc matMul.c -o matMul
max@ubuntu:~$ ./matMul

= = = = = = Matrix Multiplication = = = = = =

Enter number of rows in first matrix: 2
Enter number of colomns in second matrix: 4
Enter Element of row=1 , column= 1 : 1
Enter Element of row=1 , column= 2 : 2
Enter Element of row=1 , column= 3 : 3
Enter Element of row=1 , column= 4 : 4
Enter Element of row=2 , column= 1 : 4
Enter Element of row=2 , column= 2 : 3
Enter Element of row=2 , column= 3 : 2
Enter Element of row=2 , column= 4 : 1

The elements of first matrix are:
1 2 3 4
4 3 2 1
- - - - - - - - - - - - - - - - - - - - - - - - -

Enter number of rows in second matrx: 4
Enter number of colomns in second matrix: 3
Enter Element of row=1 , column= 1 : 1
Enter Element of row=1 , column= 2 : 2
Enter Element of row=1 , column= 3 : 3
Enter Element of row=2 , column= 1 : 4
Enter Element of row=2 , column= 2 : 5
Enter Element of row=2 , column= 3 : 6
Enter Element of row=3 , column= 1 : 3
Enter Element of row=3 , column= 2 : 2
Enter Element of row=3 , column= 3 : 1
Enter Element of row=4 , column= 1 : 4
Enter Element of row=4 , column= 2 : 5
Enter Element of row=4 , column= 3 : 6

The elements of the second matrix are:
1 2 3
4 5 6
3 2 1
4 5 6
- - - - - - - - - - - - - - - - - - - - - - - - -

The resultant matrix is:
34 38 42
26 32 38
- - - - - - - - - - - - - - - - - - - - - - - - -

Monday, February 15, 2016

C Program to find maximum and minimum element in a array of 5 elements.

/*=================================================================================
 **
 **  File Name     : maxMinArray.c
 **  Creation Date : Tue 16 Feb 2016 03:23:54 AM IST
 **  Last Modified : Tue 16 Feb 2016 03:41:23 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
int main ()
{
    printf("\n= = = = = Find bigest and Smallest elements in an array = = = =\n");
    int arr[5], i;
    printf("\nEnter the elements of the array : \n");
    for(i=0; i<5; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("\nThe elements of the array are : \n");
    for(i=0; i<5; i++)
    {
        printf("%d\t",arr[i]);
    }
    int max = arr[0];
    int min = arr[0];
    for (i = 0; i < 5; i++)
    {
        if (arr[i] > max)
        {
            max = arr[i];
        }
        else if (arr[i] < min)
        {
            min = arr[i];
        }
    }
    printf ("\nThe biggest element of the array : %d\n", max);
    printf ("\nThe smallest element of the array : %d\n", min);
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
    return 0;
}

Output :
max@ubuntu:~/cprog$ gcc maxMinArray.c -o maxMinArray
max@ubuntu:~/cprog$ ./maxMinArray

= = = = = Find bigest and Smallest elements in an array = = = =

Enter the elements of the array :
3
2
4
5
1

The elements of the array are :
3    2    4    5    1   
The biggest element of the array : 5

The smallest element of the array : 1

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to find the biggest and smallest of three numbers.

/*=================================================================================
**
**  File Name     : bigSmall.c
**  Creation Date : Tue 16 Feb 2016 03:03:35 AM IST
**  Last Modified : Tue 16 Feb 2016 03:20:17 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**===============================================================================*/

#include<stdio.h>
void main()
{
    printf("\n= = = = = = Big and Small among 3 number = = = = =\n");
    int n1, n2, n3, big, small;
    printf("\nEnter The first number : ");
    scanf("%d", &n1);
    printf("\nEnter The second number : ");
    scanf("%d", &n2);
    printf("\nEnter The third number : ");
    scanf("%d", &n3);

    printf("--------------------------------------------------\n");
    if(n1>n2 && n1>n3)
    {
        big=n1;
    }
    else if(n2 > n3)
    {
        big=n2;
    }
    else
    {
        big=n3;
    }
    printf("\nThe biggest number is : %d\n",big);
    if(n1<n2 && n1<n3)
    {
        small= n1;
    }
    else if(n2 < n3)
    {
        small = n2;
    }
    else
    {
        small=n3;
    }
    printf("\nThe smallest number is : %d\n",small);
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

Output :

max@ubuntu:~/cprog$ gcc bigSmall.c -o bigSmall
max@ubuntu:~/cprog$ ./bigSmall

= = = = = = Big and Small among 3 number = = = = =

Enter The first number : 34

Enter The second number : 23

Enter The third number : 67
--------------------------------------------------

The biggest number is : 67

The smallest number is : 23

= = = = = = = = = = = = = = = = = = = = = = = = =

C Program to calculate sum of all elements in an Array

/*=================================================================================
 **
 **  File Name     : sumArray.c
 **  Creation Date : Tue 16 Feb 2016 02:47:54 AM IST
 **  Last Modified : Tue 16 Feb 2016 02:57:53 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
int main()
{
    printf("\n= = = = = = Sum of Array elements = = = = =\n");
    int arr[100], n, i, sum=0;
    printf("\nHow many elements you wanna Store : ");
    scanf("%d", &n);
    printf("\nEnter the values into array :\n");
    for (i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }
    for (i=0; i<n; i++)
    {
        sum = sum + arr[i];
    }
    printf("\nSum of all the numbers in the array = %d\n", sum);
    printf("\n= = = = = = = = = = = = = = = = = = = = = = =\n\n");
    return (0);
}

Output :

max@ubuntu:~/cprog$ gcc sumArray.c -o sumArray
max@ubuntu:~/cprog$ ./sumArray

= = = = = = Sum of Array elements = = = = =

How many elements you wanna Store : 5

Enter the values into array :
1
2
3
4
5

Sum of all the numbers in the array = 15

= = = = = = = = = = = = = = = = = = = = = = =

C Program to find max and min between two numbers.

/*=================================================================================
 **
 **  File Name     : maxmin.c
 **  Creation Date : Tue 16 Feb 2016 01:09:26 AM IST
 **  Last Modified : Tue 16 Feb 2016 02:33:46 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
int main() 
{
    printf("\n= = = = = = Find max and min between two numbers = = = = = =\n\n");
    int num1, num2; 
    printf("Enter the first numbers : "); 
    scanf("%d", &num1); 
    printf("Enter the second numbers : "); 
    scanf("%d", &num2); 
    if(num1 > num2) 
    { 
        printf("\n%d is Maximum.\n", num1);
        printf("%d is Minimum.\n",num2);
    } 
    else 
    { 
        printf("\n%d is maximum.\n", num2); 
        printf("%d is Minimum.\n",num1);
    } 
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
    return 0; 
}

Output :

max@ubuntu:~/cprog$ gcc maxmin.c -o maxmin
max@ubuntu:~/cprog$ ./maxmin

= = = = = = Find max and min between two numbers = = = = = =

Enter the first numbers : 450
Enter the second numbers : 650

650 is maximum.
450 is Minimum.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to check Even or Odd

/*=================================================================================
 **
 **  File Name     : evenodd.c
 **  Creation Date : Tue 16 Feb 2016 12:55:54 AM IST
 **  Last Modified : Tue 16 Feb 2016 01:02:17 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include <stdio.h>
int main()
{
    printf("\n= = = = = Program to check Even or Odd = = = = = = =\n");
    int n;
    printf("\nEnter an integer : ");
    scanf("%d", &n);
    if(n%2 == 0)
    {
        printf("\nThe entered number %d is a Even number.\n",n);
    }
    else
    {
        printf("\nThe entered number %d is a Odd number.\n",n);
    }
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
    return 0;
}

OutPut :

max@ubuntu:~/cprog$ gcc evenodd.c -o evenodd
max@ubuntu:~/cprog$ ./evenodd

= = = = = Program to check Even or Odd = = = = = = =

Enter an integer : 23

The entered number 23 is a Odd number.

= = = = = = = = = = = = = = = = = = = = = = = = = = =

max@ubuntu:~/cprog$ ./evenodd

= = = = = Program to check Even or Odd = = = = = = =

Enter an integer : 46000

The entered number 46000 is a Even number.

= = = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to convert a decimal Number into binary

/*=================================================================================
 **
 **  File Name     : decToBin.c
 **  Creation Date : Mon 15 Feb 2016 10:10:36 PM IST
 **  Last Modified : Mon 15 Feb 2016 10:24:09 PM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include <stdio.h>
#include <math.h>
int main()
{
    printf("\n= = = = = = = Decimal to Binary = = = = = = = = =\n");
    int n, bin=0, rem, i=1, k;
    printf("\nEnter a decimal number : ");
    scanf("%d", &n);
    k=n;
    while (n!=0)
    {
        rem=n%2;
        n/=2;
        bin+=rem*i;
        i*=10;
    }
    printf("\nThe binary equivalent of %d is = %d\n",k,bin);
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

OutPut :

max@ubuntu:~/cprog$ gcc decToBin.c -o decToBin
max@ubuntu:~/cprog$ ./decToBin

= = = = = = = Decimal to Binary = = = = = = = = =

Enter a decimal number : 100

The binary equivalent of 100 is = 1100100

= = = = = = = = = = = = = = = = = = = = = = = = =

max@ubuntu:~/cprog$ ./decToBin

= = = = = = = Decimal to Binary = = = = = = = = =

Enter a decimal number : 15

The binary equivalent of 15 is = 1111

= = = = = = = = = = = = = = = = = = = = = = = = =

Program to count the number of digits in a integer Number.

/*=================================================================================
**
**  File Name     : countDigit.c
**  Creation Date : Mon 15 Feb 2016 09:18:11 PM IST
**  Last Modified : Mon 15 Feb 2016 09:33:57 PM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**===============================================================================*/

#include<stdio.h>
int main()
{
    printf("\n= = = = = = = = Count the Number of digits = = = = = = = = =");
    int no, count=0;
    printf("\nEnter the number to count the digits : ");
    scanf("%d", &no);
    int k=no;
    while(no)
    {
        no = no/10;
        count++;
    }
    printf("\nThe number of digits present in %d is : %d\n",k, count);
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

Output:

max@ubuntu:~/cprog$ gcc countDigit.c -o countDigit
max@ubuntu:~/cprog$ ./countDigit

= = = = = = = = Count the Number of digits = = = = = = = = =
Enter the number to count the digits : 467913

The number of digits present in 467913 is : 6

= = = = = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to print the digits of a number in reverse order.

/*=================================================================================
**
**  File Name     : revDigit.c
**  Creation Date : Mon 15 Feb 2016 09:18:11 PM IST
**  Last Modified : Mon 15 Feb 2016 09:42:45 PM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**===============================================================================*/

#include<stdio.h>
int main()
{
    printf("\n= = = = = = = = = Reverse the Number = = = = = = = = = = =");
    int no, rev, rem;
    printf("\nEnter the number to be reversed : ");
    scanf("%d", &no);
    while(no)
    {
        rem = no%10;
        no = no/10;
        //printf("%d",rem);
        rev = (rev*10)+rem;
    }
    printf("\nThe reverse number of %d is : %d\n",no, rev);
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

Output:

max@ubuntu:~/cprog$ gcc revDigit.c -o revDigit
max@ubuntu:~/cprog$ ./revDigit

= = = = = = = = = Reverse the Number = = = = = = = = = = =
Enter the number to be reversed : 12345

The reverse number of 0 is : 54321

= = = = = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to reverse the elements of an integer array.

/*=================================================================================
 **
 **  File Name     : reverseArray.c
 **  Creation Date : Mon 15 Feb 2016 07:48:58 AM IST
 **  Last Modified : Mon 15 Feb 2016 08:13:49 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.in@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include <stdio.h>
int main()
{
    printf("\n= = = = = = = = Reverse the Array Elements = = = = = = = = =");
    int arr[100], n, i, k, temp;
    printf("\nHow many elements you wanna insert in to array : ");
    scanf("%d", &n);
    printf("Enter the elements in to array : \n");
    for (i = 0; i < n ; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("\nThe original elements of the array are : \n");
    for (i = 0; i < n ; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n-----------------------------------------------------------\n");
    k = n-1;
    for(i=0; i<k; i++)
    {
        temp=arr[i];
        arr[i]=arr[k];
        arr[k]=temp;
        k--;
    }
    printf("\nThe Reversed array elements are : \n");
    for (i=0; i<n; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
    return 0;
}

Output:

max@ubuntu:~/cprog$ gcc reverseArray.c -o reverseArray
max@ubuntu:~/cprog$ ./reverseArray

= = = = = = = = Reverse the Array Elements = = = = = = = = =
How many elements you wanna insert in to array : 8
Enter the elements in to array :
1
2
3
4
5
6
7
8

The original elements of the array are :
1    2    3    4    5    6    7    8   
-----------------------------------------------------------

The Reversed array elements are :
8    7    6    5    4    3    2    1   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to remove duplicate elements from an integer array.

/*=================================================================================
**
**  File Name     : delDuplicate.c
**  Creation Date : Wed 14 Feb 2016 07:38:32 PM IST
**  Last Modified : Mon 15 Feb 2016 07:22:57 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**===============================================================================*/

#include<stdio.h>
int main()
{
    printf("\n= = = = = = = = Remove duplicate elements from Array = = = = = = = = =\n");
    int arr[100], i, j, n, k;
    printf("\nHow many elements you wanna insert : ");
    scanf("%d", &n);
    printf("\nInsert the elements in to the array:\n");
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("\nThe elements of the array before deleting duplicate elements:\n");
    for(i=0; i<n; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n-----------------------------------------------------------------------\n");
    for(i=1; i<n; i++)
    {
        //item= arr[i];
        int count=0;
        for(j=i-1; j>=0; j--)
        {
            if(arr[i]==arr[j])
            {
                for(k=i; k<n; k++)
                {
                    arr[k]=arr[k+1];
                }
                n=n-1;
            }
        }
    }
    printf("The elements of the array after deletion of duplicate elements:\n");
    for(i=0; i<n; i++)
    {
        printf("%d\t",arr[i]);
    }
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

Output:

max@ubuntu:~/cprog$ gcc delDuplicate.c -o delDuplicate
max@ubuntu:~/cprog$ ./delDuplicate

= = = = = = = = Remove duplicate elements from Array = = = = = = = = =

How many elements you wanna insert : 6

Insert the elements in to the array:
2
4
3
4
5
4

The elements of the array before deleting duplicate elements:
2    4    3    4    5    4   
-----------------------------------------------------------------------
The elements of the array after deletion of duplicate elements:
2    4    3    5   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to merge two array in ascending order.

/*=================================================================================
 **
 **  File Name     : merge2array.c
 **  Creation Date : Mon 15 Feb 2016 06:12:36 AM IST
 **  Last Modified : Mon 15 Feb 2016 06:42:13 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.in@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include <stdio.h>
int main()
{
    printf("\n= = = = = = = = = Merge the elements of two array = = = = = = = = =\n");
    int arr1[100], arr2[100], sorted[200], m, n, c;
    int i, j, k;
    j = k = 0;
    printf("\nHow many elements you wanna insert in first array : ");
    scanf("%d", &m);
    printf("Enter %d integers in to first array :\n", m);
    for (c = 0; c < m; c++)
    {   
        scanf("%d", &arr1[c]);      
    }
    printf("\nHow many elements you wanna insert in second array : ");
    scanf("%d", &n);
    printf("Enter %d integers in to second array :\n", n);
    for (c = 0; c < n; c++)
    {   
        scanf("%d", &arr2[c]);   
    }
    for (i = 0; i < m + n;)
    {
        if (j < m && k < n)
        {
            if (arr1[j] < arr2[k])
            {
                sorted[i] = arr1[j];
                j++;
            }
            else
            {
                sorted[i] = arr2[k];
                k++;
            }
            i++;
        }
        else if (j == m)
        {
            for (; i < m + n;)
            {
                sorted[i] = arr2[k];
                k++;
                i++;
            }
        }

        else
        {
            for (; i < m + n;)
            {
                sorted[i] = arr1[j];
                j++;
                i++;
            }
        }
    }
    printf("\nThe elements of merged array are :\n");
    for (c = 0; c < m + n; c++)
    {   
        printf("%d\t", sorted[c]);   
    }
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
    return 0;
}

Output:

= = = = = = = = = Merge the elements of two array = = = = = = = = =

How many elements you wanna insert in first array : 3
Enter 3 integers in to first array :
2
4
8

How many elements you wanna insert in second array : 4
Enter 4 integers in to second array :
1
4
6
9

The elements of merged array are :
1    2    4    4    6    8    9   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to sort an integer array in ascending order.

/*=================================================================================
 **
 **  File Name     : sortArray.c
 **  Creation Date : Mon 15 Feb 2016 04:43:08 AM IST
 **  Last Modified : Mon 15 Feb 2016 05:06:09 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.in@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/
#include <stdio.h>
int main()
{
    printf("\n= = = = = Sort the Array elements in ascending order = = = = =\n");
    int i, j, a, n, arr[30];
    printf("\nHow many number you want to sort : ");
    scanf("%d", &n);
    printf("Enter the numbers in to the array :\n");
    for (i = 0; i < n; ++i)
    {
        scanf("%d", &arr[i]);
    }
    printf("The elements of the array are : \n");
    for (i = 0; i < n; ++i)
    {
        printf("%d\t", arr[i]);
    }
    for (i = 0; i < n; ++i)
    {
        for (j = i + 1; j < n; ++j)
        {
            if (arr[i] > arr[j])
            {
                a =  arr[i];
                arr[i] = arr[j];
                arr[j] = a;
            }
        }
    }
    printf("\nThe elements of the array after sorting :\n");
    for (i = 0; i < n; ++i)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

Output:

= = = = = Sort the Array elements in ascending order = = = = =

How many number you want to sort : 10
Enter the numbers in to the array :
5
3
6
4
8
1
10
2
9
7
The elements of the array are :
5    3    6    4    8    1    10    2    9    7   
The elements of the array after sorting :
1    2    3    4    5    6    7    8    9    10   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

C program to print pattern-b

/*=================================================================================
**
**  File Name     : printPattern_b.c
**  Creation Date : Wed 10 Feb 2016 08:04:44 PM IST
**  Last Modified : Mon 15 Feb 2016 04:32:19 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**===============================================================================*/

#include<stdio.h>
int main()
{
    printf("\n= = = = = = = = = = Print Pattern = = = = = = = = = =\n");
    int n, i, j, count, item=1;
    printf("\nEnter the value of 'n' : ");
    scanf("%d",&n);
    count=n;
    j=n;
    printf("\n");
    for(i=0; i<j; i++)
    {
        while(count!=0)
        {
            printf("%d\t",item);
            count--;
        }
        printf("\n\n");
        n=n-1;
        count=n;
        item++;
    }
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("\n");
}

Output:

= = = = = = = = = = Print Pattern = = = = = = = = = =

Enter the value of 'n' : 5

1    1    1    1    1   

2    2    2    2   

3    3    3   

4    4   

5   

= = = = = = = = = = = = = = = = = = = = = = = = = = =

C program to print pattern-a

/*=================================================================================
**
**  File Name     : printPattern_a.c
**  Creation Date : Wed 10 Feb 2016 08:04:44 PM IST
**  Last Modified : Mon 15 Feb 2016 04:25:51 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**===============================================================================*/

#include<stdio.h>
int main()
{
    printf("\n= = = = = = = = = = Print Pattern = = = = = = = = = =\n");
    int n, i, count, item=1;
    printf("\nEnter the value of n : ");
    scanf("%d",&n);
    printf("\n");
    for(i=0; i<n; i++)
    {
        count=i+1;
        while(count!=0)
        {
            printf("%d\t",item);
            count--;
            item++;
        }
        printf("\n\n");
        item=1;
    }
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("\n");
}

Output:

= = = = = = = = = = Print Pattern = = = = = = = = = =

Enter the value of n : 5

1   

1    2   

1    2    3   

1    2    3    4   

1    2    3    4    5   

= = = = = = = = = = = = = = = = = = = = = = = = = = =

C Program to calculate gross salary

/*=================================================================================
**
**  File Name     : grossSalary.c
**  Creation Date : Mon 15 Feb 2016 03:33:41 AM IST
**  Last Modified : Mon 15 Feb 2016 04:02:41 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**===============================================================================*/

#include<stdio.h>
int main()
{
    printf("\n= = = = = = = = = = Calculation of Gross Salary = = = = = = = = = =\n");
    int bs;
    float hra, da, gs;
    printf("\nEnter the Basic Salary(must be a +ve integer): ");
    scanf("%d",&bs);
    if(bs >=1 && bs<=1000)
    {
        hra = (10*bs)/100;
        da = (50*bs)/100;
        printf("\nThe House Rent Allowance is 10 percent of %d = %f\n",bs,hra);
        printf("\nThe Dearness Allowance is 50 percent of %d = %f\n",bs,da);
    }
    else if(bs >=1001 && bs<=2000)
    {
        hra = (20*bs)/100;
        da = (60*bs)/100;
        printf("\nThe House Rent Allowance is 20 percent of %d = %f\n",bs,hra);
        printf("\nThe Dearness Allowance is 60 percent of %d = %f\n",bs,da);
    }
    else if(bs >=2001 && bs<=3000)
    {
        hra = (25*bs)/100;
        da = (70*bs)/100;
        printf("\nThe House Rent Allowance is 25 percent of %d = %f\n",bs,hra);
        printf("\nThe Dearness Allowance is 70 percent of %d = %f\n",bs,da);
    }
    else
    {
        hra = (30*bs)/100;
        da = (80*bs)/100;
        printf("\nThe House Rent Allowance is 30 percent of %d = %f\n",bs,hra);
        printf("\nThe Dearness Allowance is 80 percent of %d = %f\n",bs,da);
    }
    gs = bs + hra + da;
    printf("\nThe Gross Salary of %d is: %f\n",bs,gs);
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

Output:

= = = = = = = = = = Calculation of Gross Salary = = = = = = = = = =

Enter the Basic Salary(must be a +ve integer): 13400

The House Rent Allowance is 30 percent of 13400 = 4020.000000

The Dearness Allowance is 80 percent of 13400 = 10720.000000

The Gross Salary of 13400 is: 28140.000000

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Sunday, February 14, 2016

C Program to calculate the Simple Interest

/*==========================================================================================
**
**  File Name     : simpleInterest.c
**  Creation Date : Mon 15 Feb 2016 03:04:48 AM IST
**  Last Modified : Mon 15 Feb 2016 03:26:28 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**=========================================================================================*/

#include<stdio.h>
int main()
{
    float pa, rt, si, total;
    int t;
    printf("\n= = = = = = = = =  = Simple Interest Calculation = = = = = = = = = =");
    printf("\nEnter the principal amount: ");
    scanf("%f",&pa);
    printf("\nEnter the rate of interest: ");
    scanf("%f",&rt);
    printf("\nEnter the duration of time in Year: ");
    scanf("%d",&t);
    si = (pa * t * rt)/100;
    total = pa + si;
    printf("\nThe Interest Amount of Rs %f for %d years is: %f\n",pa,t,si);
    printf("\nThe Total Amount is %f + %f = %f",pa,si,total);
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

Output:

= = = = = = = = =  = Simple Interest Calculation = = = = = = = = = =
Enter the principal amount: 500

Enter the rate of interest: 13.5

Enter the duration of time in Year: 5

The Interest Amount of Rs 500.000000 for 5 years is: 337.500000

The Total Amount is 500.000000 + 337.500000 = 837.500000
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Saturday, December 12, 2015

How to Add Custom Header to Your .C File Automatically in vi Editor.

In this article I am going to tell you how to add default header text to your .c file, or how to create a header section in your C Programming code with File Name, Creation Date/Time, Last Modified Date/Time, Author Name, Organization Name automatically populated when you open a file in Vi. Whenever a new .c file is created. There is a feature vi editor called "autocommand", using this feature you can specify some vi commands to be executed automatically while reading or writing a file, or while entering/leaving a buffer/window, or while exiting vi editor.

Vi autocmd syntax: autocmd {event} {pattern} {cmd}

Events: Following are some autocmd events.

BufNewFile - Starting to edit a file that doesn't exist.
FileReadPre - Before reading a file with a ":read" command.
BufWritePre - Starting to write the whole buffer to a file.
FileWritePre - Starting to write part of a buffer to a file.
BufDelete - Before deleting a buffer from the buffer list.
BufWipeout - Before completely deleting a buffer.
BufNew - Just after creating a new buffer.
BufEnter - After entering a buffer.
BufLeave - Before leaving to another buffer.
SwapExists - Detected an existing swap file.

Suppose that I need the following template to be loaded automatically while opening a new “.c” file in Vi.

/*========================================================================================
**
**  File Name       :
**  Creation Date :
**  Last Modified  :
**  Compoler        : gcc
**  Author             : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization   : SCIS, University of Hyderabad, India.
**
**========================================================================================*/

You can achieve this in three steps as mentioned below.
Step-1: Create a template file
Save the above template in a text file with “:insert” in the first line, followed by the template and a “.”(dot) in the last line as shown below.

:insert
/*==========================================================================================
**
**  File Name       :
**  Creation Date :
**  Last Modified :
**  Compoler       : gcc
**  Author            : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**=========================================================================================*/
.


Step-2: Add autocmd commands to ~/.vimrc
Add the following lines in the ~/.vimrc file.

autocmd bufnewfile *.c so /home/manoj/c_header.txt
autocmd bufnewfile *.c exe "1," . 5 . "g/File Name     :.*/s//File Name     : " .expand("%")
autocmd bufnewfile *.c exe "1," . 5 . "g/Creation Date :.*/s//Creation Date : " .strftime("%c")
autocmd Bufwritepre,filewritepre *.c execute "normal ma"
autocmd Bufwritepre,filewritepre *.c exe "1," . 10 . "g/Last Modified :.*/s/Last Modified :.*/Last Modified : " .strftime("%c")
autocmd bufwritepost,filewritepost *.c execute "normal `a"

Step-3: Create a new *.c file with automatic header

Now, when you create a new *.c file using vim, this will automatically add the header defined in the Step1 and populate the File Name and Creation Date automatically as shown below.

/*==============================================================================================
**
**  File Name       : first.c
**  Creation Date : Sat 12 Dec 2015 10:06:12 PM IST
**  Last Modified :
**  Compoler       : gcc
**  Author            : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**============================================================================================*/ 

When you save the first.c file, it will automatically update the Last Modified field accordingly as shown below.

/*=======================================================================================
**
**  File Name       : first.c
**  Creation Date : Sat 12 Dec 2015 10:06:12 PM IST
**  Last Modified : Sat 12 Dec 2015 10:41:35 PM IST
**  Compoler       : gcc
**  Author            : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**========================================================================================*/ 

Explanation of the autocmd commands inside ~/.vimrc

1 autocmd bufnewfile *.c so /home/manoj/c_header.txt
2 autocmd bufnewfile *.c exe "1," . 5 . "g/File Name :.*/s//File Name : " .expand("%")
3 autocmd bufnewfile *.c exe "1," . 5 . "g/Creation Date :.*/s//Creation Date : " .strftime("%d-%m-%Y")
4 autocmd Bufwritepre,filewritepre *.c execute "normal ma"
5 autocmd Bufwritepre,filewritepre *.c exe "1," . 5 . "g/Last Modified :.*/s/Last Modified :.*/Last M
odified : " .strftime("%c")
6 autocmd bufwritepost,filewritepost *.c execute "normal `a"
 
Line 1 defines the template file. This indicates that for *.c file, /home/manoj/c_header.txt template file 
should be used.
Line 2 will search for the pattern “File Name :” from the 1st line to 5th line. If found, it will write the current 
filename in that line. 
Line 3 will update the Creation Date field. 
Line 5 will update the Last Modified field with the current date and time when you save the file.
Line 4 & 6: While saving the file, the cursor will move to the “Last modified :”(because of last write operation). 
If you want the cursor back to the previous position then, you need to add Line 4 and 6 
to the .vimrc file. 
Line 4 will mark the current cursor position before updating.
Line 6 will restore the cursor position back to its previous position.
 

Note:

1. Verify whether autocmd is enabled in Vi / Vim – Execute :version from vi / vim. If autocommand feature is enabled, it will display +autocmd.

2. Autocommand help: Execute :help au from vi / vim, to get quick help on vim autocmd features.

 

If you like what you show
Please like, share and comment in the comment box bellow.
Big thanks for reading..!