Friday, November 11, 2016

Member Access Rules in JAVA

The member access rules determines whether a sub class can use a property of it's super class or it can only access or it can neither access nor access. There are two level of access control:

  • At the top level: public or package-private
  • At the member level: public, private, protected
A class may be declared with the 'public' modifier, in that case that class is visible to all classes everywhere.

At the member level, there are three different access modifiers are there: 'private', 'protected' and 'public'.
  • private : If private access modifier is applied to an instance variable, method or with a constructor in side a class then they will be accessed inside that class only not out side of the class.
          for example:  class A
                               {
                                        private int x=10;
                               }
                               class B extends A
                               {
                                        int y=20;
                                        System.out.println(x);//Illegal access to x ;
                                }
           If you make any class constructor private, you can't create the instance/object of that class from outside the class. for example:
                                class A
                                {
                                        int x;
                                        private A(int k) // private constructor
                                        {
                                                  x=k;
                                         }
                                }
                                class Test
                                {
                                         public static void main(String args[])
                                         {
                                                  A ob=new A(10); //Compile time error
                                          }
                                }

  • protected: If protected access modifier is applied to an instance variable, method or with a constructor in side a class then they will be accessed inside the package only in which class is present and, in addition, by a sub class in another package.

  • public: The class, variable, method or a constructor with public access modifier can be accessed from anywhere.

Thursday, October 20, 2016

Inheritance in Java

Defination :

Inheritance can be defined as the process by which a class can acquires the properties(instance variables and methods) of another class called super class. The class which is inheriting the properties is called child class or sub class or derived class and the class whose properties are being inherited is called parent class or super class or base class. The 'extends' keyword is used to inherit the properties of a class.

How to create inheritance?

class Person
{
      int age;
      String name;
}
class Student extends Person
{
      int rollNo;
      public void print()
      {
             System.out.println(age);
             System.out.println(name);
             System.out.println(rollNo);
      }
}

In sub class we need to specify only new variables and methods that are not a part of the super class.

How to Solve initramfs Problem in ubuntu?


mount: mounting /dev/loop0/ on /root failed : Invalid argument
mount: mounting /dev on /root/dev failed: No such file or directory
mount: mounting /sys on /root/sys failed: No such file or directory
mount: mounting /proc on /root/proc failed: No such file or directory
Target filesystem doesn' t have requested /sbin/init
No init found. Try passing init = bootarg.

BusyBox v1.21.1 (Ubuntu 1:1:21.0-1ubuntu1) built-in shell (ash)
Enter 'help' for a list of built-in commands

(initramfs) _

To solve this problem you must follow the following steps:

1. When you start your computer, it will ask you to select the operating system. Select Ubuntu.
2. When your computer exits from the OS choosing screen, press 'shift' key from your keyboard constantly.
3. Then one screen will appear from boot loader. Don't do anything with that, just press 'e' key from your key board.
4. Then a set of commands will be displayed which are used to boot ubuntu.
5. In those boot commands find the lines that contains 'ro quiet splash' and change the 'ro' word to 'rw' (read write). It will now looks like 'rw quiet splash'.
6. Press the key to boot at the bottom, or press F10, or press ctrl+X. Now boot loader will boot ubuntu with modified boot commands.
     
     However that 'rw' word wont be saved and at the next boot you will have the same problem again. So you need to edit the /etc/default/grub file.

7. Type 'sudo gedit /etc/default/grub' in terminal and hit enter.
8. Find the line which looks like 'GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" '.
9. Change it to 'GRUB_CMDLINE_LINUX_DEFAULT="rw quiet splash" '.
10. Now save the file and close the terminal. When you reboot your computer it will boot normally.

Sunday, June 12, 2016

go slow my life

Aahista chal zindagi, abhi kai karz chukana baaki hai
Kuch dard mitana baaki hai, kuch farz nibhana baaki hai


Raftaar mein tere chalne se kuchh rooth gaye, kuch chhoot gaye
Roothon ko manana baaki hai, roton ko hasana baki hai


Kuch hasraatein abhi adhuri hain, kuch kaam bhi aur zaruri hai
Khwahishen jo ghut gayi iss dil mein, unko dafnana baki hai


Kuch rishte ban kar toot gaye, kuch judte-judte chhoot gaye
Un toote-chhoote rishton ke zakhmon ko mitana baki hai


Tu aage chal main aata hoon, kya chhod tujhe ji paunga?
In saanson par haqq hai jinka, unko samjhaana baaki hai


Aahista chal zindagi, abhi kai karz chukana baki hai 

Kuch dard mitana baaki hai, kuch farz nibhana baaki hai !!



आहिस्ता चल जिंदगी, अभी कई कर्ज चुकाना बाकी है 
कुछ दर्द मिटाना बाकी है, कुछ फर्ज निभाना बाकी है


रफ़्तार में तेरे चलने से, कुछ रूठ गए कुछ छूट गए 
रूठों को मनाना बाकी है, रोतों को हँसाना बाकी है


कुछ रिश्ते बनकर टूट गए, कुछ जुड़ते -जुड़ते छूट गए 
उन टूटे -छूटे रिश्तों के, जख्मों को मिटाना बाकी है


कुछ हसरतें अभी अधूरी हैं, कुछ काम भी और जरूरी हैं 
जीवन की उलझ पहेली को, पूरा सुलझाना बाकी है


जब साँसों को थम जाना है, फिर क्या खोना ,क्या पाना है 
पर मन के जिद्दी बच्चे को, यह बात बताना बाकी है


आहिस्ता चल जिंदगी, अभी कई कर्ज चुकाना बाकी है 
कुछ दर्द मिटाना बाकी है, कुछ फर्ज निभाना बाकी है !!


-: University of Hyderabad :-

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   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =