Wednesday, March 28, 2012

About SWAP in linux

what is swap?
swap space is the area on a HD which is part of the VM of your machine.
swap space temporarily hold a memory pages that are inactive.

why do i need swap?
  1. Memory consuming programs Sometimes, a large program (like OpenOffice, Neverwinter Nights, or a video editor) make the entire system need extra memory.
  2. Hibernation (suspend-to-disk) The hibernation feature (suspend-to-disk) writes out the contents of RAM to the swap partition before turning off the machine.
  3. Optimizing memory usage
  4. Optimizing Swap performance

How much swap do i need?
As a base minimum, it's highly recommended that the swap space should be equal to the amount of physical memory (RAM).

Example Scenarios:
  • Low RAM and low disk space With 512 MiB RAM and 30 GB hard disk, use  512 MiB for swap since RAM is very low.
  • Low RAM and high disk space With 512 MiB RAM and 100 GB hard disk, use 1 GiB for swap since RAM is very low and hard disk space is plentiful.
  • High RAM and low disk space With 2 GiB RAM and 30 GB hard disk, use 1 GiB for swap since hard disk space is very low.
  • High RAM and high disk space With 2 GiB RAM and 100 GB hard disk, use 2 GiB for swap since hard disk space is plentiful.

Thursday, March 22, 2012

Program to find factorial using recursion in c

//1st way
#include <stdio.h>
#include <conio.h>

int fact(int);

int main()
{
    int num, f;
    printf ("\n Enter a number: ");
    scanf ("%d", &num);
    f = fact(num);
    printf (" \n Factorial of %d is: %d ", num, f);
}

int fact(int n)
{
    if(n == 1)
    {
        return 1;
    }
   
    else
    {
        return (n*fact(n-1));
    }

}

____________________________________________________________________
//2nd way
#include <stdio.h>
#include <conio.h>

int getFactorial(int n)
{
    int local;
    if (n > 1)
    {
        local = n * getFactorial(n-1);
        return local;
    }

    else
        return 1;
}

int main()
{
    int num, f;
    printf ( "\n Enter a number: " );
    scanf ("%d", &num);
    f = getFactorial(num);
    printf ( "\n Factorial of %d is: %d ", num, f);
}

Tuesday, March 20, 2012

What is kernel

What is linux -- it is a kernel.
What is GNU\Linux -- linux operating system.

Linux is the name of the kernel. The kernel is the core part of an operating system that keeps track of everything. The kernel is both a fence and a gate. As a gate, it allows programs to access hardware in a uniform way. Without the kernel, you would have to write programs to deal with every device model ever made. The kernel handles all device-specific interactions so you don't have to. It also handles file access and interaction between processes.

The kernel also controls the flow of information between programs. The kernel is a program's gate to the world around it. Every time that data moves between processes, the kernel controls the messaging.

As a fence
, the kernel prevents programs from accidentally overwriting each other's data and from accessing files and devices that they don't have permission to. It limits the amount of damage a poorly-written program can do to other running programs.

In our case, the kernel is Linux. Now, the kernel all by itself won't do anything. You can't even boot up a computer with just a kernel. Think of the kernel as the water pipes for a house. Without the pipes, the faucets won't work, but the pipes are pretty useless if there are no faucets. Together, the user applications (from the GNU project and other places) and the kernel (Linux) make up the entire operating system, GNU/Linux.

Sum of number divisible by 7 between 100 and 200

#include <iostream>
using namespace std;

int main()
{
    int i, count = 0, sum = 0;
   
    for (i=101; i<200; i++)
    {
        if(i%7 == 0)
        {
            count = count++;
            sum = sum + i;
        }
    }
   
    cout << "\n Total no of integer div by 7 is: " << count << endl;
    cout << "\n sum of these number: " << sum << endl;
   
    return 0;
}


OUTPUT:

Friday, March 16, 2012

What is oop

What is the definition of OOP:
Object oriented programming is a design approach where we thing in term of object rather than thinking in term of function.
                                                OR
Object-oriented programming is an approach that provides a way of molding
programs by creating partitioned memory area for both data,
and functions that can be used as templates for creating copies of such modules on demand.”
                                                OR
Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationships.


By applying oops we try to achieve  the Rules of software engineering. such as
  1. Reusability-------classes and object
  2. Extensibility-----inheritance, aggregation and composition
  3. Simplicity--------abstraction, encapsulation and polymorphism
  4. Maintainability & security---all the above 3 combined helped to maintain the code better

To understand this concept we can take a real world example of Students in a School.
    1. Lets take “Students” as a Class.
    2. Class can have any number of Objects. Here
       “Biology Students” & “Computer Students”.
    3. “Students” are controlled by “Teachers” which acts as an Interface.
    4. “Students” Class can be Re-Used to create
       “Biology Students” and “Computer Students”.

Further reading:
  1. code Project object oriented programming
  2. oops in java

Thursday, March 15, 2012

program to print sum of its integer in given digit

#include <iostream>
using namespace std;

int main()
{
    int num, temp, sum=0;
    cout << "\n Enter your number: ";
    cin >> num;
   
    while(num > 0)
    {
        temp = num%10;
        num = num/10;
        sum = sum + temp;
    }
   
    cout << "\n the sum of integer in your digit is: " << sum << endl;
    return 0;
}


OUTPUT:
 

Program to real roots of quadratic equation with output

// d = b^2 - 4ac

#include <iostream>
#include <cmath>          //when we used c library in c++
using namespace std;

int main()
{
    float a, b, c, d, root1, root2;


    cout << "\n Enter the value of a, b, c: " ;
    cin >> a >> b >> c;
 

    d = b*b - 4*a*c;
    cout << "\n discriminant is: " << d ;
   
    if ( d<0 )
    {
        cout << "\n Roots are imagenary.";
    }
    else
    {
        root1 = (-b + sqrt(d)) / (2*a);
        root2 = (-b - sqrt(d)) / (2*a);
        cout << "\n root1 is: " << root1 << "\n root2 is: " << root2 << endl;
    }
    return 0;
}


OUTPUT:

Tuesday, March 13, 2012

Program to print star pattern in C++

#include <iostream>
using namespace std;

int main()
{
    int i,j;
   
    for(i=1; i<=5; i++)
    {
        cout << "\n";
       
        for(j=1; j<=i; j++)
        {
            cout << "*";
        }
    }
   
    return 0;
}


OUTPUT:
 

Thursday, March 8, 2012

A simple program to find factorial of a number

 // A simple program to find factorial of a number
#include<iostream>
using namespace std;

int main()
{
    int  a;
    cout << "\n Enter the no. to find factorial: ";
    cin >> a;


        int fact=1;
        for(int i=1;i<=a;i++)
        {
            fact=i*fact;
        }
   
    cout << "\n\n FACTORIAL= " << fact;
}

____________________________________________________________________________________
// program to find factorial of a number using functions
#include<iostream>
using namespace std;

int factorial(int);                //declaration of a function named factorial

int main()
{
    int fact, a;
    cout << "Enter the no.  to find factorial: ";
    cin >> a;

    fact = factorial(a);
    cout << "\n\n FACTORIAL= " << fact;
   
}

int factorial(int x)               //definition above declared function is given here
    {
        int n=1;
        for(int i=1;i<=x;i++)
        {
            n=i*n;
        }
    return (n);
}

___________________________________________________________________________________
// factorial of 1 to 10 (first 10) natural number
#include<iostream>
using namespace std;

int main()
{
    int  b;
   
    for(b=1; b<=10; b++)
    {
        long int fact = 1;
        for(int i=1;  i<=b;  i++)
        {
            fact=i*fact;
        }
        cout<< "\n\nFACTORIAL of: " << b << " is " << fact;
    }   
}

Friday, March 2, 2012

Program to find out the sum of an integer array

#include <iostream>
using namespace std;

int main()
{
    int a[]={4, 6, 25, 8, 5};
    int sum=0;


    for (int i=0; i<5; i++)
    {
        sum = sum + a[i];
    }
 

    cout << sum;
}