Posts

Showing posts from 2016

Pascal Triangle

The below program is going to print the Pascal Triangle: 0 row = 1 1 row = adding the two numbers above them to the left and the right          = (0+1) , (1+0)          = 1 , 1 2 row = (0+1) , (1+1) , (1+0)             = 1 , 2 , 1 3 row = (0+1), (1+2), (2+1), (1+0)           = 1 , 3 , 3 , 1 4 row = (0+1), (1+3)  , (3+3), (3+1), (1+0)           =  1 , 4 , 6 , 4 , 1  1 1        1 1      2       1 1      3       3      1  1      4        6     4        1 #include <stdio.h> #include <conio.h> void main() { int i,j,n,c,k,space; clrscr(); printf("Enter the limit "); scanf("%...

C program to print prime numbers between 1 to n.

In this post we are going to write a simple C program which prints prime numbers from 1 to n. Prime numbers are those numbers whose only two whole- number  factors are 1 and itself.   - 1 is not a prime number #include < stdio.h > #include < conio.h > int main() {  int nr,n,div,p;  printf("Enter any number: ");  scanf("%d",  & nr);  for(n=2; n<=nr; n++)  {   for(div=2; div<n; div++)   {    if(n%div==0)    {      p=0;       break ;    }    p=1;   }   if(p)     printf("\t%d",n);  }   return 0 ; }

Add Linux System Call

Image
In this post we are going to add a new Linux system call. First off all we need to modify our current kernel so we add our system call and then compile it. First off all lets write the system call, which is a very simple c program. #include <linux/linkage.h> #include <linux/kernel.h> asmlinkage long sys_hello() {      printk(“Hello World\n”);      return 0; } Then we need to add this system call to our kernel. 1.       - Download Linux Kernel : Version 4.5.2  www.kernel.org       Lets  extract the downloaded file using     sudo mv linux-4.5.2 /usr/src  command. Then we will create a new folder and name it Hello. In this folder we are going to put our hello.c file and a Makefile which is going to be like this: In the next step we are going to open  linux-4.5.2/arch/x86/entry/syscalls/syscall_32.tbl and add our call, like in the p...

C program to find the factorial value of a number.

/*c program to find out factorial value of a number*/ #include < stdio.h > #include < conio.h > int main() {  int n,i,factorial=1;  printf("Enter any number : ");  scanf("%d",  & n);  for(i=1; i<=n; i++)     factorial = factorial * i;  printf("Factorial value of %d = %d",n,factorial);   return 0 ; }

Quick Sort

Quick sort  is a divide and conquer algorithm. Its divided large list in mainly three parts: 1.       Elements less than pivot element. 2.       Pivot element. 3.       Elements greater than pivot element. Where pivot as middle element of large list. Let’s understand through example: List : 3 7 8 5 2 1 9 5 4 In above list assume  4  is pivot element so rewrite list as:   3 1 2   4   5 8 9 5 7 Here, I want to say that we set the  pivot element ( 4 ) which has in left side elements are less than and right hand side elements are greater than. Now you think, how’s arrange the less than and greater than elements? Be patient, you get answer soon. Now let’s start understand the concept of quick sort. The  steps are : 1.      Pick a pivot element. 2.      Reorder the list so that all elements with values less than the...

SQL SELECT

The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set. To select only some columns you want you can use the following syntax: SELECT  column_name , column_name FROM  table_name ; By using the following syntax you select all columns of a table: SELECT * FROM  table_name ; If you want to select only some records who fulfill a specific criterion, you can use the where clause:  SELECT  column_name , column_name FROM  table_name WHERE  column_name operator value ;

TIME INTERVAL in Oracle

Understanding TIME INTERVAL Data Types in Oracle INTERVAL YEAR TO MONTH and INTERVAL DAY TO SECOND are two data types added to store interval of two dates. They can be use both in SQL and PL/SQL. The syntax is as below: INTERVAL YEAR[(year_precision)] TO MONTH INTERVAL DAY[(day_precision)] TO SECOND[(fractional_seconds_precision)] There are defaults for precision. Two digest for year and day and six digest for fractional seconds. SQL>   SELECT INTERVAL '5-9' YEAR TO MONTH FROM DUAL Result: +05-09 Cover a time interval of 5 years and 9 months SQL>   SELECT INTERVAL '7 15:20:07.7' DAY TO SECOND FROM DUAL Result: +07 15:20:07.700000 Cover a time interval of 7 days, 15 hours, 20 minutes, 7 seconds and 7 milliseconds. SQL>   SELECT INTERVAL '7 15:20:07.7' DAY TO SECOND(1) FROM DUAL Result: +07 15:20:07.7 Cover a time interval of 7 days, 15 hours, 20 minutes, 7 seconds and 7 milliseconds.

Find Max and Min

/*Program ti find minimum and maximum number when the user gives ten numbers*/ #include<stdio.h> #include<conio.h> int main() {  int arr[10];  int i,num,min,max;  for(i=0; i<10; i++)  {    printf("Enter %d Number : ",i+1);    scanf("%d",&arr[i]);  }  min=max=arr[0];  for(i=0; i<10; i++)  {    if(arr[i] < min)       min=arr[i];    else if(arr[i] > max)       max=arr[i];  }  printf("\nMaximum number is %d",max);  printf("\nMinimum number is %d",min);  getch();  return 0; }

Fibonacci Series

Fibonacci numbers are in the following integer sequence : 0 1 1 2 3 5 8 13 21 34 55 89 ... /*Print Fibonacci series*/ #include<stdio.h> #include<conio.h> int main() {  int x,y,z;  x=0;  y=1;  z=x+y;  printf("%d\t",x);  x=y;  y=z;  getch();  return 0; }

Banker's Algorithm in C

Implementation of bankers algorithm using C language, pthreads and mutex locks. #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUMBER_OF_RESOURCES 3 #define NUMBER_OF_CUSTOMERS 5 int i = 0; int j = 0; pthread_mutex_t mutex; //mutex lock to lock global variables int initResourceVector [NUMBER_OF_RESOURCES]; //available, max, allocation, need int available [NUMBER_OF_RESOURCES]; int allocation [NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES] = {{1,1,0},{1,3,0},{0,0,2},{0,1,1},{0,2,0}}; int max [NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES] = {{5,5,5},{3,3,6},{3,5,3},{7,1,4},{7,2,2}}; int need [NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES]; int requestResource(int processID,int request[]); int releaseResource(int processID,int releaseVector[]); int ifGreaterThanNeed(int processID,int request[]); int ifEnoughToRelease(int processID,int releaseVector[]); int ifInSafeMode(); int ifEnoughToAlloc(); void printNeed(); ...