Search This Blog

Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Monday, 29 February 2016

CRC Error Detection (Dynamic)

#include<stdio.h>
int main()
{
int a[10],d[10],k[10],z[10],i,n,q,x=1,s,m,j;
printf("Enter Total Data Bits");
scanf("%d",&n);
printf("Enter Total Divisor Bits");
scanf("%d",&q);
for(i=0;i<n;i++)
{
    printf("Enter Data");
    scanf("%d",&a[i]);
}
for(i=0;i<q;i++)
{
    printf("Enter divisor");
    scanf("%d",&d[i]);
}
for(i=0;i<q;i++)
{
    printf("Enter 0's for zero detection");
    scanf("%d",&z[i]);
}
printf("\nData Before Entering temp 0's\n");
for(i=0;i<n;i++)
{
    printf("%d",a[i]);
}
s=n+(q-1);
printf("\n");
for(i=n;i<s;i++)
{
    printf("Enter 0's ");
    scanf("%d",&a[i]);
}
printf("\nData After Entering temp 0's\n");
for(i=0;i<s;i++)
{
    printf("%d",a[i]);
}
i=0;
if(a[i]==1)
{
    for(i=0;i<q;i++)
    {
    if(a[i]==d[i])
    {
        k[i]=0;
    }
    else
    {
        k[i]=1;
    }
    }
    k[i]=a[q];
    for(j=0;j<q;j++)
    {
        k[j]=k[j+1];
    }
}
else
{
    for(i=0;i<q;i++)
    {
    if(a[i]==z[i])
    {
        k[i]=0;
    }
    else
    {
        k[i]=1;
    }
    }
    k[i]=a[q];
    for(j=0;j<q;j++)
    {
        k[j]=k[j+1];
    }
}
printf("\nK:");
for(i=0;i<q;i++)
{
    printf("%d",k[i]);
}
m=q;
do
{
x=x+1;
i=0;
if(k[i]==1)
{
    for(i=0;i<q;i++)
    {
    if(k[i]==d[i])
    {
        k[i]=0;
    }
    else
    {
        k[i]=1;
    }
    }
    m++;
    k[i]=a[m];
    for(j=0;j<q;j++)
    {
        k[j]=k[j+1];
    }
}
else
{
    for(i=0;i<q;i++)
    {
    if(k[i]==z[i])
    {
        k[i]=0;
    }
    else
    {
        k[i]=1;
    }
    }
    m++;
    k[i]=a[m];
    for(j=0;j<q;j++)
    {
        k[j]=k[j+1];
    }
}
printf("\nK:");
for(i=0;i<q;i++)
{
    printf("%d",k[i]);
}
}
while(x<n);
printf("\nCRC = ");
if(x==n)
{
    for(i=0;i<q-1;i++)
    {
    printf("%d",k[i]);
    }
}
}


Sunday, 28 February 2016

Netwon_Raphson Method ( Non_Bracketing Method )

#include<stdio.h>
#include<math.h>
float fun(float x)
{
    float fun;
    fun=(x*x*x)-(5*x)+1;
    return fun;
}
float dfun(float x)
{
    float dfun;
    dfun=3*(x*x)-5;
    return dfun;
}
void main()
{
float x,xo,y,yd,d,xao=0,eps;
int i;
printf("Enter x= ");
scanf("%f",&x);
printf("Enter error limit=");
scanf("%f",&eps);
i=0;
xo=x;
yd=dfun(x);
if(fabs(yd)<=0)
{
    printf("x do not bracket any rule");
    return;
}
  do
    {
        i=i+1;
        x=xo;
        y=fun(x);
        yd=dfun(x);
        xo=x-(y/yd);
        d=fabs(xo-xao);
        printf("\ni=%d\nx=%f\ty=%f\tyd=%f\txa=%f\td=%f",i,x,y,yd,xo,d);
        xao=xo;
    }
    while(d>=eps);
    if(d<=eps)
    {
        printf("\nAns:The root of the equation is x = %f with error of %f",xo,d);
    }



}

False_Position Method ( Bracketing Method)

#include<stdio.h>
#include<math.h>
float fun(float x)
{
    float fun;
    fun=(2*sin(x))-x;
    return fun;
}
void main()
{
float xl,xu,fl,fu,eps,xr,fr,d,xro=0;
int i,n;
printf("Enter xl & xu = ");
scanf("%f %f",&xl,&xu);
printf("Enter error limit=");
scanf("%f",&eps);
fl=fun(xl);
fu=fun(xu);
printf("fl=%f ,fu=%f",fl,fu);

if(fl*fu>0)
{
    printf("xl & xu do not bracket any rule");
    return;
}
    i=1;
    xr=((xu*fl)-(xl*fu))/(fl-fu);
    d=fabs(xr-xro);
    printf("\nxl=%f,xu=%f,\nroot=%f , d=%f, i=%d",xl,xu,xr,d,i);
  do
    {
        i=i+1;
        fr=fun(xr);
        if(fl*fr>=0)
        {
           xl=xr;
           fl=fr;
        }
        else
        {
           xu=xr;
           fu=fr;
        }
        xr=((xu*fl)-(xl*fu))/(fl-fu);
        d=fabs(xr-xro);
        printf("\nxl=%f,xu=%f,\nroot=%f , d=%f, i=%d",xl,xu,xr,d,i);
        xro=xr;
    }
    while(d>=eps);
    if(d<=eps)
    {
        printf("\nAns:The root of the equation is x = %f with error of %f",xr,d);
    }
}

Monday, 18 January 2016

Cipher text example with file handling

 Simple Cipher text with file Handling
Input : hello karan gajjar
key : 3
Output : khoor ndudq jdmmdu

As we know encryption and decryption. I have kept input in encode.txt file and aftyer entering key value the decrypted text will be stored in decode.txt file!

CODE:

 #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    int i,key,k;
    char str[100],str2[100];
    FILE *fp,*fq;
    fp=fopen("encode.txt","r");
    fgets(str,sizeof(str),fp);
    puts(str);
    fq=fopen("decode.txt","a");
    clrscr();
    printf("Enter Key:");
    scanf("%d",&key);
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]>=97 && str[i]<120)
        {
            str[i]=str[i]+key;
            str2[i]=str[i];
        }
        else if(str[i]==32)
        {
            str2[i]=str[i];
        }
        else
        {
            str[i]=str[i]-26+key;
            str2[i]=str[i];
        }
    }
    str2[i]='\0';
    puts(str2);
    fprintf(fq,"\n");
    fprintf(fq,str2);
    fclose(fq);
    fclose(fp);
    getch();
}


NOTE: You can add more elseif condition for "," , "!" , "?" this type of ascii value i have added for "space".
              and yes in this example you can't add encoded message with every new line!

Wednesday, 13 January 2016

Quick Sort

/* Quick Sort */

    #include<stdio.h>
    #include<conio.h>
    void quicksort(int [],int,int);
    void main()
    {

    int a[10],n,i;
    printf("\nEnter size of array");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
         printf("\nEnter a[%d]",i);
         scanf("%d",&a[i]);
    }
    quicksort(a,0,n-1);
    printf("\nSorted array is:\n");
    for(i=0;i<n;i++)
    {
         printf("%d ",a[i]);
    }

    }
    void quicksort(int x[10],int first,int last)
    {

    int pivot,i,j,temp;
    if(first<last)
    {
         pivot=first;
         i=first;
         j=last;

    while(i<j)
    {
         while(x[i]<=x[pivot] && i<last)
         i++;

         while(x[j]>x[pivot])
         j--;

         if(i<j)
         {
              temp=x[i];
              x[i]=x[j];
              x[j]=temp;
         }
    }
    temp=x[pivot];
    x[pivot]=x[j];
    x[j]=temp;
    quicksort(x,first,j-1);
    quicksort(x,j+1,last);
    }

    }

Merge Sort


    #include<stdio.h>
    #include<conio.h>
    void partition(int [],int,int);
    void merge_sort(int [],int,int,int);
    void main()
    {

    int a[10],i,n;
    clrscr();
    printf("\nEnter no of elements:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
         printf("\nEnter a[%d]",i);
         scanf("%d",&a[i]);
    }
    partition(a,0,n-1);
    for(i=0;i<n;i++)
    {
         printf("%d ",a[i]);
    }
    getch();

    }

    void partition(int a[],int low,int high)
    {

    int mid;
    if(low<high)
    {
         mid=(low+high)/2;
         partition(a,low,mid);
         partition(a,mid+1,high);
         merge_sort(a,low,mid,high);
    }

    }

    void merge_sort(int a[],int low,int mid,int high)
    {

    int temp[10];
    int i,j,k,m;
    m=mid+1;

    for(i=low;j<=mid && m<=high;i++)
    {
         if(a[j]<=a[m])
         {
              temp[i]=a[j];
              j++;
         }
         else
         {
              temp[i]=a[m];
              m++;
         }
    }
    if(j>mid)
    {
         for(k=m;k<=high;k++)
         {
              temp[i]=a[k];
              i++;
         }
    }
    else
    {
         for(k=j;k<=mid;k++)
         {
               temp[i]=a[k];
               i++;
         }
    }
    for(k=low;k<=high;k++)
    {
         a[k]=temp[k];
    }

    }

Insertion Sort


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

    void main()
    {

    int i,j,temp,arr[10],n;
    clrscr();
    printf("\nEnter size of ayyar");
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
         scanf("%d",&arr[i]);
    }

    for (i = 1; i < n; i++)
    {
         int tmp = arr[i];
         int j;
         for (j = i; j > 0; j--)
         {
              if (arr[j - 1] < tmp)
                   break;
              arr[j] = arr[j - 1];
         }
         arr[j] = tmp;
    }

    printf("\nSorted array is \n");
    for(i=0;i<n;i++)
    {
         printf("%d\n",arr[i]);
    }
    getch();

    }

Selection Sort



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

    void main()
    {

    int i,j,min,n,a[6],temp;
    clrscr();
    printf("Enter number");
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
         scanf("%d",&a[i]);
    }
    printf("\nSorted List is:\n");

    for(i=0;i<n;i++)
    {
         min = i;
         for(j=i+1;j<n;j++)
         {
              if(a[j]<a[min])
              min = j;
         }
         temp=a[i];
         a[i]=a[min];
         a[min]=temp;
    }

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

    }

Bubble Sort



   #include<stdio.h>

    void main()
    {

    int i,j,n,temp,a[10];
    printf("\nEnter n\n");
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
    printf("Enter a[%d]",i);
    scanf("%d",&a[i]);
    }

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

    printf("\nSorted ayyay using bubble sort is\n");
    for (i = 0; i < n; i++)
    {
    printf("\n%d", a[i]);
    }

    }

Reverse string using pointer and function

#include<stdio.h>
#include<conio.h>
int strlen1(char *);
void strrev1(char *);
void main()
{
    char str[50];
    clrscr();
    printf("Enter string");
    gets(str);
    strrev1(str);
    printf("Rev is %s",str);
    getch();
}
void strrev1(char *start)
{
    char *end,temp;
    int len;
    len=strlen1(start);
    end=start+len-1;
    while(start<end)
    {
    temp=*start;
    *start=*end;
    *end=temp;
    start++,end--;
    }
    }
int  strlen1(char *s)
    {
        int count=0;
        while(*s++)
        count++;
        return(count);
    }

Concate two Strings

#include<stdio.h>
#include<conio.h>
void main()
{
    int len=1,i,j;
    char a[100],b[100];
    clrscr();
    printf("enter string a:");
    gets(a);
    printf("Enter string b:");
    gets(b);
    for(len=0;a[len]!=NULL;len++);
    for(i=0,j=len;b[i]!=NULL;i++,j++)
    {
        a[j]=b[i];
    }
    a[j]=NULL;
    printf("string a is ");
    puts(a);
    getch();

}

Find char in string and print it's position from left and right

#include<stdio.h>
#include<conio.h>
void main()
{
    int len=0,i,pos,flag;
    char a[100],c;
    clrscr();
    printf("Enter string");
    gets(a);
    printf("Enter charactor to match");
    scanf("%c",&c);
    for(len=0;a[len]!=NULL;len++);
    for(i=0;i<len;i++)
    {

        if(a[i]==c)
        {
            pos=i;
            flag=0;
        }
    }
    if(flag==0)
    {
        printf("%c is matched",c);
        printf("\nit is at %d possition from left",pos+1);
        printf("\nit is at %d possition from right ",len-pos);
    }
    else
    printf("Not matched");
    getch();
}

Reverse string simple

#include<stdio.h>
#include<conio.h>
void main()
{
    int len=0,i,j;
    char a[10],b[10];
    clrscr();
    printf("Enter string");
    gets(a);
    for(len=0;a[len]!=NULL;len++);
    for(i=len-1,j=0;i>=0;i--,j++)
    b[j]=a[i];
    puts(b);
    getch();
}

Find array element position using pointer

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[20],i,n;
    int *p;
    p=a;
    printf("Enter n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter elements");
        scanf("%d",&n);
        p++;
    }
    p=p-n;
    for(i=0;i<n;i++)
    {
        printf("%d",*p);
        p++;
    }

    getch();
}

Structure of students marks

#include<stdio.h>
#include<conio.h>
struct student
{
    int roll;
    char name[100];
    int sub[3],tot,perc;
};
void main()
{
    struct student a[5];
    int i,j,n;
    clrscr();
    printf("Enter no of student");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter roll no");
        scanf("%d",&a[i].roll);
        printf("Enter the name of student");
        flushall();
        gets(a[i].name);
        for(j=0;j<3;j++)
        {
            printf("Enter the marks of subject =%d",j+1);
            scanf("%d",&a[i].sub[j]);
            a[i].tot=a[i].tot+a[i].sub[j];
        }
        a[i].perc=a[i].tot/3;
        for(j=0;j<=n;j++)
        {
            if(a[i].perc>65)
            {
                printf("\nroll num =%d", a[i].roll);
                printf("\nname");
                flushall();
                puts(a[i].name);

                for(j=0;j<3;j++)
                {
                    printf("\nsub %d = %d",j+1,a[i].sub[j]);
                }
                printf("\ntotal marks =%d",a[i].tot);
                printf("\npercentage %d",a[i].perc);
            }
        }
    }
    getch();
}

Fibonacci with recursive

#include<stdio.h>
#include<conio.h>
void fibo(int,int,int);
void main()
{
    int l=1,p=1,n;
    clrscr();
    printf("Enter the last num");
    scanf("%d",&n);
    fibo(l,p,n);
    getch();

}
void fibo(int l,int p,int n)
{
    int c;
    c=l+p;
    printf(" %d",l);
    if(n>0)
        fibo(p,c,--n);
}

Reversr an array

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,a[5];
    clrscr();
    for(i=0;i<5;i++)
    {
        printf("Enter the a[%d]",i);
        scanf("%d",&a[i]);
    }
    for(i=0;i<=5;i++)
    {
        printf("\n a[%d] is %d",i,a[i]);
    }
    getch();

}

Print A to Z in given case

#include <stdio.h>
#include<conio.h>
void main()
{
    char c;
    clrscr();
    printf("Enter u to display characters in uppercase and l to display in lowercase: ");
    scanf("%c",&c);
    if(c=='U' || c=='u')
    {
       for(c='A'; c<='Z'; ++c)
     printf("%c ",c);
    }
    else if (c=='L' || c=='l')
    {
    for(c='a'; c<='z'; ++c)
     printf("%c ",c);
    }
    else{
     printf("Error !!!");
     printf("Enter only u or l char!!")
    }

getch();
}

Find given number is a perfact or not

 #include<stdio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf("Enter a number: ");
scanf("%d",&n);

  for(i=1;i<n;i++)
  {
      if(n%i==0)
      sum=sum+i;

  }
  if(sum==n)
      printf("%d is a perfect number",i);
  else
      printf("%d is not a perfect number",i);

getch();
}

Find Fibonacci numbers

#include <stdio.h>



void main()

{

    int fib1 = 0, fib2 = 1, fib3, num, count = 0;



    printf("Enter the value of num \n");

    scanf("%d", &num);

    printf("First %d FIBONACCI numbers are ...\n", num);

    printf("%d\n", fib1);

    printf("%d\n", fib2);

    count = 2; /* fib1 and fib2 are already used */

    while (count < num)

    {

        fib3 = fib1 + fib2;

        count++;

        printf("%d\n", fib3);

        fib1 = fib2;

        fib2 = fib3;

   }

}