Loading

Archive for 23 Aug 2012

c/c++ Program to implement RSA Algorithm

C/C++ Program to implement RSA Algorithm




#include<iostream.h>
#include<conio.h>
#include<math.h>

int main()
{
   int i,j,k,p,q,flag,e,x,d;
   clrscr();
  cout<<"\n Enter Value of p :: ";
  cin>>p;
  cout<<"\n Enter Value of q :: ";
  cin>>q;
  int n=p*q;
  int fn=(p-1)*(q-1);

  cout<<"\n Value of (p,q) :: "<<p<<" "<<q;
  cout<<"\n Value of n,fn  :: "<<n<<" "<<fn;
  cout<<"\n\n Select Public Key e (1 < e < "<<fn<<") ::";
  cin>>e;

  int sk;
  for(i=1;i>0;i++)
  {
    x=i*e;
    d=x%fn;
    if(d==1)
    {
    cout<<"\n\n Private Key ::"<<i;
    sk=i;
    break;
    }

  }

  int pt;
  cout<<"\n\n  Enter Plain Text ::";
  cin>>pt;

  int m=1;
  for(i=0;i<e;i++)
    m=(m*pt)%n;
  m=m%n;
  cout<<"\n\n  Cipher Text ::"<<m;
  getch();

  k=1;
  for(i=0;i<sk;i++)
    k=(k*m)%n;
  k=k%n;
  cout<<"\n\n  Decrypted Text ::"<<k;
  getch();
 return 0;
}

Posted in | Leave a comment

c program to calc in/out degree of vertices of graph

C Program to Calculate In/Out - Degree of Vertices of a Graph


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

typedef struct node
{
  struct node *next;
  int vertex;
}node;

node *g[20];
int n,visited[20];
int indegree(int i);
int outdegree(int i);
void dfs(int i);

void insert(int vi,int vj)
{
 node *p,*q;
 q=(node*)malloc(sizeof(node));
 q->vertex=vj;
 q->next=NULL;

 if(g[vi]==NULL)
    g[vi]=q;
 else
 {
   p=g[vi];
   while(p->next!=NULL)
    p=p->next;
   p->next=q;
 }
}

void readgraph()
{
 int vi,vj,i,j,k,no_of_edges;
 for(i=0;i<n;i++)
    g[i]=NULL;
 printf("\nEnter the no. of Vertices::");
 scanf("%d",&n);
 printf("\nEnter the no of Edges::");
 scanf("%d",&no_of_edges);
 for(i=0;i<no_of_edges;i++)
 {
    printf("\nEnter the Edge(u,v)::");
    scanf("%d%d",&vi,&vj);
    insert(vi,vj);
  }
}


void main()
{
  int i,j,k;
  clrscr();
  readgraph();
  for(i=0;i<n;i++)
    visited[i]=0;

 /* printf("\n=====================================================");
  printf("\nNode\tIndegree\tOutdegree");
  printf("\n=====================================================");
  for(i=0;i<n;i++)
  {
   j=indegree(i);
   k=outdegree(i);
   printf("\n%2d\t%4d\t\t%5d",i,j,k);
   }
  printf("\n-----------------------------------------------------");
  */
  dfs(0);
  getch();
}

int outdegree(int i)
{
  int j=0;
  node *p;
  p=g[i];
  while(p!=NULL)
  {
   p=p->next;
   j++;
  }
  return(j);
}

int indegree(int v)
{
  int i,j=0,k;
  node *p;
  for(i=0;i<n;i++)
  {
    p=g[i];
    while(p!=NULL)
    {
      if(p->vertex==v)
        j++;
      p=p->next;
    }
  }
  return(j);
}

void dfs(int i)
{
 node *p;
 p=g[i];
 visited[i]=1;
 printf("\nVisit->%d",i);
 while(p!=NULL)
 {
   i=p->vertex;
   if(!visited[i])
    dfs(i);
   p=p->next;
 }
}

Posted in | Leave a comment

c program to find factorial of number

C Program to Find Factorial of Number


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int a,i,j,k;
    clrscr();
    printf("\n\t\t***PROGRAMME TO CALCULATE FACTORIAL OF A NO.***");
    printf("\n\t\tENTER A NO.: ",a);
    scanf("%d",&a);
    j=1;
    for(i=a;i>0;i--)
    {
     j=j*(i);

     }
    printf("\n\t\tTHE FACTORIAL OF NO IS: %d",j);
    getch();
   }

Posted in , | Leave a comment

c program to copy a text file

C Program to Copy a Text File


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

void main()
{
  FILE *fp1,*fp2;
  char file1[500],file2[500];
  char x;
  printf("\nENTER THE SOURCE FILE:");
  gets(file1);
  fp1=fopen(file1,"rb");
  printf("\nENTER THE DESTINATION FILE:");
  gets(file2);
  fp2=fopen(file2,"wb");
  if(fp1!=NULL && fp2!=NULL)
  {
    while((x=getc(fp1))!=EOF)
    putc(x,fp2);

  }

  else
    printf("\nERROR CREATING FILE...");

  fclose(fp1);
  fclose(fp2);
  getch();
 }

Posted in , , , | Leave a comment

c program to display blinking text/character

C Program to Display Blinking Text/Character


#include <conio.h>

int main(void)
{
   int i;

   clrscr();
   for (i=0; i<9; i++)
   {
       textattr(i + ((i+1) << 4));
       cprintf("This is a test\r\n");
   }

   return 0;
}

Posted in , , , | Leave a comment