Friday, 17 October 2014

Fibonacci program in java

/*CSEMATTER.BLOGSPOT.IN
Program-fibonacci series in java*/

import java.util.Scanner;
class fib
{
public static void main(String a[])
{
Scanner sc=new Scanner(System.in);
int prev=0,next=1,sum,num,i;
System.out.println("ENTER THE NO.");
num=sc.nextInt();
System.out.println(""+prev);
System.out.println(""+next);
for(i=2;i<num;i++)
{
sum=prev+next;
prev=next;
next=sum;
System.out.println(""+sum);
}
}
}

Saturday, 11 October 2014

FACTORIAL JAVA PROGRAM

/*CSEMATTER.BLOGSPOT.IN
Program-java program to calculate factorial*/

import java.util.Scanner;
class factorial
{
public static void main(String a[])
{
Scanner sc=new Scanner(System.in);
int i,num,f=1;
System.out.println("ENTER THE NO. TO CALCULATE FACTORIAL");
num=sc.nextInt();
for(i=1;i<num;i++)
{
f=f*i;
}
System.out.println("factorial is"+f);

}
}

Friday, 10 October 2014

Concurrent checkpoint

/*CSEMATTER.BLOGSPOT.IN
Program-to implement concurrent checkpoint*/


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
int main()
{
    int pr[10],p[10],n,i,j,count,temp;

    printf("Enter the no. of processes: ");
    scanf("%d",&n);
    for(i=0; i<n; i++)
        p[i]=i+1;
    printf("Enter the priority of processes \n");
    for(i=0; i<n; i++)
    {
        printf("Priority of process P%d : \n",i+1);
        scanf("%d",&pr[i]);
    }

    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(pr[i]>pr[j])
            {
                temp=pr[i];
                pr[i]=pr[j];
                pr[j]=temp;

                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
    for(i=0; i<n; i++)
    {
        count=0;
        for(j=0; j<n; j++)
        {
            if(i!=j)
                printf("P%d is sending request to P%d \n",p[i],p[j]);
             else
                 continue;
        }
        for(j=0; j<n; j++)
        {
            if(i!=j)
            {
                printf("P%d is sending ack to P%d \n",p[j],p[i]);
                count++;
            }
            else
                continue;
        }
        if(count==n-1)
        {
            printf("Ack from all the processes has been received \n");
            printf("P%d is forming a Consistent Checkpoint point \n",p[i]);
             delay(10000);
            for(j=0; j<n; j++)
            {
                if(i!=j)
                    printf("P%d sending release msg to P%d \n",p[i],p[j]);
                else
                    continue;
            }
            printf("\n \n");
        }
        else
            printf("Ack from all the processes not received hence Process P%d cann't form a consistent checkpoint \n \n",p[i]);
    }
    getch();

}

/* OUTPUT:

Enter the no. of processes: 3
Enter the no. of processes: 3
Enter the priority of processes
Priority of process P1 :
3
Priority of process P2 :
2
Priority of process P3 :
1
P3 is sending request to P2
P3 is sending request to P1
P2 is sending ack to P3
P1 is sending ack to P3
Ack from all the processes has been received
P3 is forming a Consistent Checkpoint point
P3 sending release msg to P2
P3 sending release msg to P1


P2 is sending request to P3
P2 is sending request to P1
P3 is sending ack to P2
P1 is sending ack to P2
Ack from all the processes has been received
P2 is forming a Consistent Checkpoint point
P2 sending release msg to P3
P2 sending release msg to P1


P1 is sending request to P3
P1 is sending request to P2
P3 is sending ack to P1
P2 is sending ack to P1
Ack from all the processes has been received
P1 is forming a Consistent Checkpoint point
P1 sending release msg to P3
P1 sending release msg to P2    */

Thursday, 9 October 2014

bayzentine program

/*CSEMATTER.BLOGSPOT.IN
Program-implement bayzentine program in c*/

#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>


int main()
{
int i,j,p[20][20],n,nf,cnt0=0,cnt1=1;
printf("Enter the no. of processes:");
scanf("%d",&n);
printf("Enter the process no. of faulty process:");
scanf("%d",&nf);
for(i=0;i<n;i++)
{
if(i==nf)
{
for(j=0;j<n;j++)
{
if(j!=i)
p[i][j]=random(2);
else
p[i][j]=NULL;}
}
else
{
for(j=0;j<n;j++)
{
if(j!=i)
p[i][j]=1;
else
p[i][j]=NULL;
}}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(p[i][j]==1)
++cnt1;
else if(p[i][j]==0)
++cnt0;
else
continue;
}
if(cnt1>cnt0)
continue;
else
{
printf("Processes are not synchronised \n \n");

}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d \t",p[i][j]);
}
printf("\n");
}
getch();
}

/*OUTPUT:
Enter the no. of processes:3
Enter the process no. of faulty process:2
Processes are not synchronised

    1   1
1       1
1   0          */

Wednesday, 8 October 2014

Encryption using Hashing

/* CSEMATTER.BLOGSPOT.IN
    Encryption using Hashing         */


#include <iostream>
#include<cstring>
#include<cmath>
using namespace std;
int evaluate(string str);

int myatoi(string num)
{
int temp=1,res=0,i=0;
while(num[i]!='\0')
{
res=res+temp*num[i++]-'0';
temp=temp*10;
}
return res;
}
int main()
{
string num;
getline(cin,num);
int num=myatoi(num);
cout<<"\n";
while(num>0)
{
int var;
string str1;
getline(cin,str1);
cout << str1<< "\n";
var=evaluate(str1);
--num;
}
return 0;
}

int evaluate(string str)
{
   // cout << "Hello World!" << endl;
   int n,i,k,l,int1,int2;
 
   //string str;
 
    // getline(cin,str);
    int arr[256]={0},arr_cpyi[256]={0},arr_cpy[256]={0};
    for(int j=0;j<str.size();j++)
    {
    ++arr[str[j]];
    ++arr_cpyi[str[j]];
    ++arr_cpy[str[j]];
    }
    // int *ini=&arr_cpy[0];
   
   
       for(k=0;k<256;k++)
    {
   
    for(l=0;l<255;l++)
    {
    if(arr_cpyi[l]>arr_cpyi[l+1])
    {
    // cout<<"*";
    int temp1=arr_cpyi[l];
    arr_cpyi[l]=arr_cpyi[l+1];
    arr_cpyi[l+1]=temp1;
    }
    }
    }
    int z=0;
    for(i=0;i<256;i++)
    {
    if(arr_cpyi[i]!=0)
    ++z;
    else
    continue;
    }
   
    int j,q,v=0;
    int y=ceil(+(z/2))+1;
    // cout<<y<<"\n";
    // int y=str.size()-2;
   
   
    //for(i=255;i>=(256-str.size());i--)
    while(y>0)
    {
    j=0;
    q=255;
    char ch1,ch2;
    // while(arr_cpyi[i]!=arr_cpy[j])
    // j++;
   
    while(arr_cpyi[256-z]!=arr_cpy[j])
    j++;
    while(arr_cpyi[255-v]!=arr_cpy[q])
    q--;
    // cout<<"j="<<j;
    // cout<<"q="<<q<<"\n";
    arr_cpy[j]=0;
    arr_cpy[q]=0;
   
   
    // cout<<"j="<<j<<"\n";
    // cout<<"q="<<q<<"\n";
   
    // ch=*temp-*ini;
    int1=&arr_cpy[j]-&arr_cpy[0];
    int2=&arr_cpy[q]-&arr_cpy[0];
    ch1=(char)int1;
    ch2=(char)int2;
    for(int x=0;x<str.size();)
    {
    if(ch1==str[x])
    {
    str[x]=ch2;
   
    ++x;
    }
    else if(ch2==str[x])
    {
    str[x]=ch1;
   
    ++x;
    }
    else
    ++x;
    }
    --z;
    ++v;
    --y;
   
   
    }
   
   
    cout<< str << endl;
   
    return 0;
}
   

Tuesday, 7 October 2014

MAEKAWA ALGORITHM DISTRIBUTED SYSTEM

/*CSEMATTER.BLOGSPOT..IN
Program name-Maekawa algorithm in distributed system*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

void process1();
void process2();
void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
int p[10],pr[10],n,i,j,count=0,ran_pr,temp,max_lmt,min_lmt;

int main()
{
    printf("Enter the no. of processses \n");
    scanf("%d",&n);
    for(i=0; i<n; i++)
        p[i]=i+1;
      do
       {
           ran_pr=random(n);
       }
       while(ran_pr!=0);
    ran_pr=3;
    printf("Control process is P%d \n",ran_pr);
    printf("Enter the priority of processes \n");
    for(i=0; i<n; i++)
    {
        printf("Priority of process P%d : \n",p[i]);
        scanf("%d",&pr[i]);
    }
    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(pr[i]>pr[j])
            {
                temp=pr[i];
                pr[i]=pr[j];
                pr[j]=temp;

                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
    for(i=0; i<n-1; i++)
    {
        if(p[i]<ran_pr)
        {
            max_lmt=ran_pr;
            process1();
        }
        else
        {
            min_lmt=ran_pr+1;
            process2();
        }
    }

    getch();
}

void process1()
{
    for(j=0; j<ran_pr; j++)
    {
        if(i!=j)
            printf("Process P%d is sending request to P%d \n",p[i],p[j]);
        else
            continue;
    }
    printf("\n");
    for(j=0; j<ran_pr; j++)
    {
        if(i!=j)
        {
            printf("Process P%d is acknowledging P%d \n",p[j],p[i]);
            count++;
        }
        else
            continue;
    }
    printf("\n");
    if(count==(ran_pr-1))
    {
        printf("Process P%d is entering CS \n",p[i]);
        delay(10000);
    }
    printf("\n");
    for(j=0; j<ran_pr; j++)
    {
        if(i!=j)
            printf("Process P%d is sending reply to P%d \n",p[i],p[j]);
        else
            continue;
    }
}

void process2()
{
    for(j=ran_pr+1; j<n; j++)
    {
        if(i!=j)
            printf("Process P%d is sending request to P%d \n",p[i],p[j]);
        else
            continue;
    }
    printf("\n");
    for(j=ran_pr+1; j<n; j++)
    {
        if(i!=j)
        {
            printf("Process P%d is acknowledging P%d \n",p[j],p[i]);
            count++;
        }
        else
            continue;
    }
    printf("\n");

    if(count==(n-ran_pr-1))
    {
        printf("Process P%d is entering CS \n",p[i]);
        delay(10000);
    }
    printf("\n");
    for(j=ran_pr+1; j<n; j++)
    {
        if(i!=j)
            printf("Process P%d is sending reply to P%d \n",p[i],p[j]);
        else
            continue;
    }
}

/*OUTPUT:
Enter the no. of processses 5
Control process is P3
Enter the priority of processes
Priority of process P1 :2
Priority of process P2 :1
Priority of process P3 :5
Priority of process P4 :3
Priority of process P5 :4

Process P2 is sending request to P1
Process P2 is sending request to P3
Process P1 is acknowledging P2
Process P3 is acknowledging P2
Process P2 is entering CS
Process P2 is sending reply to P1
Process P2 is sending reply to P3

Process P1 is sending request to P2
Process P1 is sending request to P3
Process P2 is acknowledging P1
Process P3 is acknowledging P1
Process P1 is entering CS
Process P1 is sending reply to P2
Process P1 is sending reply to P3

Process P4 is sending request to P5
Process P5 is acknowledging P4
Process P4 is entering CS
Process P4 is sending reply to P5

Process P5 is sending request to P4
Process P4 is acknowledging P5
Process P5 is entering CS
Process P5 is sending reply to P4

Process P3 is sending request to P1
Process P3 is sending request to P2
Process P1 is acknowledging P3
Process P2 is acknowledging P3
Process P5 is entering CS
Process P3 is sending reply to P1
Process P3 is sending reply to P2
*/