Tuesday, 20 October 2015

C program to print whether the no. is Even or Odd

#include<stdio.h>
#include<conio.h>
void main()
{
 int num;
 clrscr();
 printf(" enter any no. :");
 scanf("%d",&num);
 if(num%2==0)
              printf("\n no. is even");
 else
              printf("\n no. is odd");
 getch();
 }


C program to print the results of students

  • When the following conditions are given

                                       PERCENTAGE(%)                                                          DIVISION       

                                              >=60                                                                                 1st
                                        >=45 & <60                                                                           2nd
                                        >=30 & <45                                                                           3rd
                                               <30                                                                                  FAIL

                                                                                                                                                                                                                                       

#include<stdio.h>
#include<conio.h>
void main()
{
int math,phy,chem,hin,eng;
float total,percentage;
clrscr();
printf("\nenter marks of maths=");
scanf("%d",&math);
printf("\nenter marks of phy=");
scanf("%d",&phy);
printf("\nenter marks of chem=");
scanf("%d",&chem);
printf("\nenter marks of hindi=");
scanf("%d",&hin);
printf("\nenter marks of english=");
scanf("%d",&eng);
total=math+phy+chem+hin+eng;
printf("\ntotal=%f",total);
percentage=(total*100)/500;
printf("\npercentage=%f",percentage);
if(percentage<30)
{
printf("\n you are fail");
}
else if(percentage>=30&&percentage<45)
{
printf("\n you have secured 3rd division");
}
else if(percentage>=45&&percentage<60)
{
printf("\n you have secured 2nd division");
}
else if(percentage>=60)
{
printf("\n Congrats ! you have secured 1st division");
}
getch();
}

       

C program to calculate the Net Salary,HRA,DA,Medical & PF

  • When the following conditions are given

                      SALARY(Rs)                          HRA(%)             DA(%)      MEDICAL(Rs)        PF(%)  
                             <5000                                     5                        60                   500                      10
                      >5000 & <10000                           7                        65                   550                      10
                     >10000 & <25000                         10                       75                   700                      15
                     >25000 & <50000                         12                       80                  1000                     17
                             >50000                                   15                     100                  1500                     20

                                                                                                                                                                       


#include<stdio.h>
#include<conio.h>
void main()
{
int salary,medical;
float hra,da,pf,total;
clrscr();
printf("\nenter salary:");
scanf("%d",&salary);
if(salary<5000)
{
hra=.05*salary;
da=.6*salary;
medical=500;
pf=.1*salary;
}
if(salary>5000&&salary<10000)
{
hra=.07*salary;
da=.65*salary;
medical=550;
pf=.1*salary;
}
if(salary>10000&&salary<25000)
{
hra=.1*salary;
da=.75*salary;
medical=700;
pf=.15*salary;
}
if(salary>25000&&salary<50000)
{
hra=.12*salary;
da=.8*salary;
medical=1000;
pf=.17*salary;
}
if(salary>50000)
{
hra=.15*salary;
da=1*salary;
medical=1500;
pf=.2*salary;
}
total=(salary+hra+da+medical)-pf;
printf("\n total net salary=%f",total);
getch();
}

C program to calculate the electricity bill

  • When charge is given slab-wise
                      UNIT                                                                                             CHARGE       

                     0 - 100                                                                                         $ 2.80/unit
                   100 - 200                                                                                       $ 3.50/unit
                   200 - 300                                                                                       $ 4.10/unit
                   300 - 400                                                                                       $ 5.00/unit
                  400 - 1000                                                                                      $ 5.20/unit

                                                                                                                                                              


#include<stdio.h>
#include<conio.h>
void main()
{
int unit;
float charge;
clrscr();
printf("\n enter unit :");
scanf("%d",&unit);
if(unit>0&&unit<=100)
{
charge=unit*2.80;
}
else if(unit>100&&unit<=200)
{
charge=100*2.80+(unit-100)*3.50;
printf("\n your charge of %d unit in 1st slab @ 2.80/unit is =%f",100,(100*2.80));
printf("\n your charge of %d unit in 2nd slab @ 3.50/unit is =%f",(unit-100),(unit-100)*3.50);
}
else if(unit>200&&unit<=300)
{
charge=100*2.80+100*3.50+(unit-200)*4.10;
printf("\n your charge of %d unit in 1st slab @ 2.80/unit is=%f",100,(100*2.80));
printf("\n your charge of %d unit in 2nd slab @ 3.50/unit is=%f",100,(100*3.50));
printf("\n your charge of %d unit in 3rd slab @ 4.10/unit is=%f",(unit-200),(unit-200)*4.10);
}
else if(unit>300&&unit<=400)
{
charge=100*2.80+100*3.50+100*4.10+(unit-300)*5.00;
printf("\n your charge of %d unit in 1st slab @ 2.80/unit is=%f",100,(100*2.80));
printf("\n your charge of %d unit in 2nd slab @ 3.50/unit is=%f",100,(100*3.50));
printf("\n your charge of %d unit in 3rd slab @ 4.10/unit is=%f",100,(100*4.10));
printf("\n your charge of %d unit in 4th slab @ 5.00/unit is=%f",(unit-300),(unit-300)*5.00);
}
else if(unit>400&&unit<=1000)
{
charge=100*2.80+100*3.50+100*4.10+100*5.00+(unit-400)*5.20;
printf("\n your charge of %d unit in 1st slab @ 2.80/unit is=%f",100,(100*2.80));
printf("\n your charge of %d unit in 2nd slab @ 3.50/unit is=%f",100,(100*3.50));
printf("\n your charge of %d unit in 3rd slab @ 4.10/unit is=%f",100,(100*4.10));
printf("\n your charge of %d unit in 4th slab @ 5.00/unit is=%f",100,(100*5.00));
printf("\n your charge of %d unit in 5th slab @ 5.20/unit is=%f",(unit-400),(unit-400)*5.20);
}
printf("\n your charge of total %d units is =%f",unit,charge);
getch();
}


C program to design a CALCULATOR

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int choice,d,c,r;
float a,b,total;
clrscr();
printf("\t\t\t\tcalculator");
printf("\n\t\t\t\t----------");
printf("\n enter 1 for addition");
printf("\n enter 2 for subtraction");
printf("\n enter 3 for multiplication");
printf("\n enter 4 for division");
printf("\n enter 5 for modulus");
printf("\n enter 6 for square");
printf("\n enter 7 for square root");
printf("\n\n enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n enter 1st no : ");
scanf("%f",&a);
printf("\n enter 2nd no : ");
scanf("%f",&b);
total=a+b;
printf("\nyour %f + %f=%f",a,b,total);
break;

case 2:
printf("\n enter 1st no : ");
scanf("%f",&a);
printf("\n enter 2nd no : ");
scanf("%f",&b);
total=a-b;
printf("\nyour %f - %f=%f",a,b,total);
break;

case 3:
printf("\n enter 1st no : ");
scanf("%f",&a);
printf("\n enter 2nd no : ");
scanf("%f",&b);
total=a*b;
printf("\nyour %f * %f=%f",a,b,total);
break;

case 4:
printf("\n enter 1st no : ");
scanf("%f",&a);
printf("\n enter 2nd no : ");
scanf("%f",&b);
total=a/b;
printf("\n your %f / %f=%f",a,b,total);
break;

case 5:
printf("\n enter 1st no : ");
scanf("%d",&c);
printf("\n enter 2nd no : ");
scanf("%d",&d);
r=c%d;
printf("\n your %d  %%  %d = %d",c,d,r);
break;

case 6:
printf("\n enter any no : ");
scanf("%f",&a);
total=a*a;
printf("\n your %f * %f =%f",a,a,total);
break;

case 7:
printf("\n enter any no : ");
scanf("%f",&a);
total=sqrt(a);
printf("\n required square root of your no. =%f",total);
break;

default:
printf("error");
break;
}
getch();
}

C program to print total number of consonants in a string

#include<stdio.h>
#include<conio.h>
void main()
{
char name[50],ch=0;
int i=0,cnt=0,ccnt=0;
clrscr();
printf("\n enter a name :");
ch=getchar();
    while(ch!='.')
    {
        name[i]=ch;
        i++;
        ch=getchar();
    }
    name[i]='\0';
    printf("\n your name is :");
    i=0;
        while(name[i]!='\0')
        {
            ch=name[i];
                if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
                               cnt++;
                else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
                                ccnt++;
                 putchar(ch);
                 i++;
        }
printf("\n total no. of consonants is=%d",ccnt);
getch();
}

C program to find the frequency of vowels in a string

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20],ch=0;
int j=0,a=0,e=0,i=0,o=0,u=0,sum=0;
clrscr();
printf("\n enter a name :");
ch=getchar();
    while(ch!='.')
    {
        name[j]=ch;
        j++;
        ch=getchar();
    }
    name[j]='\0';
    printf("\n your name is :");
    j=0;
        while(name[j]!='\0')
        {
            ch=name[j];
                if(ch=='a'||ch=='A')
                        a=a++;
                if(ch=='e'||ch=='E')
                        e=e++;
                if(ch=='i'||ch=='I')
                        i=i++;
                if(ch=='o'||ch=='O')
                        o=o++;
                if(ch=='u'||ch=='U')
                        u=u++;
                sum=a+e+i+o+u;
                putchar(ch);
                j++;
        }
printf("\n no. of occurences of a is=%d",a);
printf("\n no. of occurences of e is=%d",e);
printf("\n no. of occurences of i is=%d",i);
printf("\n no. of occurences of o is=%d",o);
printf("\n no. of occurences of u is=%d",u);
printf("\n total no. of vowels is=%d",sum);
getch();
}

C program to print total number of vowels in a string

#include<stdio.h>
#include<conio.h>
void main()
{
char name[50],ch=0;
int i=0,cnt=0;
clrscr();
printf("\n enter a name :");
ch=getchar();
    while(ch!='.')
    {
        name[i]=ch;
        i++;
        ch=getchar();
    }
    name[i]='\0';
    printf("\n your name is :");
    i=0;
        while(name[i]!='\0')
        {
            ch=name[i];
                if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
                                 cnt++;
                 putchar(ch);
                 i++;
        }
printf("\n total no. of vowels in string = %d",cnt);
getch();
}

C program to print total number of spaces in a string

#include<stdio.h>
#include<conio.h>
void main()
{
char name[50],ch=0;
int i=0,scnt=0;
clrscr();
printf("\n enter a name :");
ch=getchar();
    while(ch!='.')
    {
        name[i]=ch;
        i++;
        ch=getchar();
    }
    name[i]='\0';
    printf("\n your name is :");
    i=0;
        while(name[i]!='\0')
        {
            ch=name[i];
                if(ch=='  ')
                    scnt++;
                putchar(ch);
                i++;
        }
printf("\n total no. of spaces in the string=%d",scnt);
getch();
}

C program to print total number of characters in a string

#include<stdio.h>
#include<conio.h>
void main()
{
char name[50],ch=0;
int i=0,chcnt=0;
clrscr();
printf("\n enter a name :");
ch=getchar();
    while(ch!='.')
    {
        name[i]=ch;
        i++;
        ch=getchar();
    }
    name[i]='\0';
    printf("\n your name is :");
    i=0;
        while(name[i]!='\0')
        {
            ch=name[i];
            chcnt++;
            putchar(ch);
            i++;

        }
printf("\n total no. of characters in string=%d",chcnt);
getch();
}

C program to print total number of words in a string

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20],ch=0;
int i=0,wcnt=1;
clrscr();
printf("\n enter a name :");
ch=getchar();
    while(ch!='.')
    {
        name[i]=ch;
        i++;
        ch=getchar();
    }
    name[i]='\0';
    printf("\n your name is :");
    i=0;
        while(name[i]!='\0')
        {
            ch=name[i];
                if(ch==' ')
                      wcnt++;
                putchar(ch);
                i++;

        }
printf("\n total no. of words in string=%d",wcnt);
getch();
}

C program to implement circular doubly linked list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
int m,n;
struct node
{
    int data;
    struct node *next,*pre;
}*start;

void create_list(int);
void display();
void insert_at_beg(int);
void insert_at_end(int);
void insert_at_pos(int,int);
void selection(int);
void count();
void main()
{
    int i,choice;
    clrscr();
    start=NULL;
    while(1)
    {
        clrscr();
        printf("\n press 1 for list creation");
        printf("\n press 2 for displaying the list");
        printf("\n press 3 to insert a data at beginning");
        printf("\n press 4 to insert a data at end");
        printf("\n press 5 to insert at a position");
        printf("\n press 6 for selection");
        printf("\n press 7 for counting");
        printf("\n press 8 for exit");
        printf("\n enter your choice:");
        scanf("%d",&choice);

    switch(choice)
    {

    case 1:
        if(start!=NULL)
        {
            printf("\n list already created");
            break;
        }
        printf("\n enter total no. of lists to be created:");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            printf("\n enter the data:");
            scanf("%d",&m);
            create_list(m);
        }
        printf("\n list created");
        break;

    case 2:
        if(start==NULL)
        {
            printf("\n no lists created till now");
            break;
        }
        display();
        break;

    case 3:
        if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_beg(m);
        printf("\n data inserted");
        break;

    case 4:
        if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_end(m);
        printf("\n data inserted");
        break;

    case 5:
        if(start==NULL)
        {
            printf("\n no list created.....error");
            break;
        }
        printf("\n enter the data:");
        scanf("%d",&m);
        printf("\n enter the position:");
        scanf("%d",&n);
        insert_at_pos(m,n);
        break;

    case 6:
        if(start==NULL)
        {
            printf("\n no list created....error");
            break;
        }
        printf("\n enter the data for selection");
        scanf("%d",&m);
        selection(m);
    break;


    case 7:
        if(start==NULL)
        {
            printf("\n no list created......error");
            break;
        }
        count();
    break;

    case 8:
        exit(1);
    break;

        default:
            printf("\n wrong choice");

    }
    }
getch();
}

void create_list(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    if(start==NULL)
    {
        start=tmp;
        start->next=start;
        start->pre=start;
        return;
    }
    p=start;
    while(p->next!=start)
        p=p->next;
    p->next=tmp;
    tmp->pre=p;
    tmp->next=start;
    start->pre=tmp;
}

void display()
{
    struct node *p;
    p=start;
    do
    {
        printf("\n data= %d",p->data);
        p=p->next;
    } while(p!=start);
    getch();
}

void insert_at_beg(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    p=start;
    while(p->next!=start)
    {
        p=p->next;
    }
    p->next=tmp;
    tmp->pre=p;
    tmp->next=start;
    start=tmp;
}

void insert_at_end(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=start;
    p=start;
        while(p->next!=start)
        {
            p=p->next;
        }
        p->next=tmp;
        tmp->pre=p;
        start->pre=tmp;
}

void count()
{
    int i=1,j;
    struct node *p;
    p=start;
    while(p->next!=start)
    {
        p=p->next;
        i++;
    }
        printf("\n total no. of lists created is %d",i);
    getch();
}

void insert_at_pos(int item,int pos)
{
    int i;
    struct node *tmp,*p;
    tmp=(struct node*)malloc(sizeof(struct node));
    tmp->data=item;
    if(pos==1)
    {
        insert_at_beg(item);
        return;
    }
    p=start;
    for(i=1;i<pos;i++)
    {
        p=p->next;
    }
    tmp->next=p->next;
    p->next->pre=tmp;
    tmp->pre=p;
    p->next=tmp;
    if(p==NULL)
    {
        printf("\n position not in range");
        return;
    }
    printf("\n list inserted at %d position",i);
}

void selection(int item)
{
    int i;
    struct node *p,*q;
    if(item==start->data)
    {
        p=start;
        start=start->next;
        start->pre->next=p->next;
        start->next->pre=start->pre;
        free(p);
        p=start;

        while(p->next!=start)
        {
            p=p->next;
        }
        start->pre=p;
        p->next=start;
        printf("\n node freed");
        return;
    }
    q=start;
    while(q->next->data!=item)
    {
        q=q->next;
        i++;
    }
    p=q->next;
    q->next=q->next->next;
    q->next->next->pre=q;
    free(p);
    if(q==NULL)
    {
        printf("\n wrong value");
        return;
    }
    printf("\n data found at %d position",i);
    printf("\n node deleted");
}

Monday, 19 October 2015

C program to transpose a matrix

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],t[3][3],i,j;
clrscr();
printf("\n enter d values of d array :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&a[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",a[i][j]);
    }
printf("\n");
}
printf("\n d transpose of matrix A is :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        t[i][j]=a[j][i];
    }
}
for(i=0;i<=2;i++)
{
printf("\n");
    for(j=0;j<=2;j++)
    {
        printf("\t %d",t[i][j]);
    }
}
getch();
}

C program to perform multiplication of 2 matrices using 2-d array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],i,j,k,res[3][3];
clrscr();
printf("\n enter 9 values of d 1st array:");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&a[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",a[i][j]);
    }
printf("\n");
}
printf("\n enter 9 values of d 2nd array :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&b[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",b[i][j]);
    }
printf("\n");
}
printf("\n the product of above 2 matrices are:");

for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        res[i][j]=0;

            for(k=0;k<=2;k++)
            {
            res[i][j]+=a[i][k]*b[k][j];
            }

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

C program to perform subtraction of 2 matrices using 2-d array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],i,j,diff[3][3];
clrscr();
printf("\n enter 9 values of d 1st array:");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&a[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",a[i][j]);
    }
printf("\n");
}
printf("\n enter 9 values of d 2nd array :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&b[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",b[i][j]);
    }
printf("\n");
}
printf("\n the difference of corresponding values are :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        diff[i][j]=a[i][j]-b[i][j];
    }
}
for(i=0;i<=2;i++)
{
printf("\n");
    for(j=0;j<=2;j++)
    {
    printf("\t %d",diff[i][j]);
    }
}
getch();
}

C program to perform addition of 2 matrices using 2-d array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],i,j,sum[3][3];
clrscr();
printf("\n enter 9 values of d 1st array:");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&a[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",a[i][j]);
    }
printf("\n");
}
printf("\n enter 9 values of d 2nd array :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        scanf("%d",&b[i][j]);
    }
}
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        printf("\t %d",b[i][j]);
    }
printf("\n");
}
printf("\n d sum of corresponding values r :");
for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        sum[i][j]=a[i][j]+b[i][j];
    }
}
for(i=0;i<=2;i++)
{
printf("\n");
    for(j=0;j<=2;j++)
    {
    printf("\t %d",sum[i][j]);
    }
}
getch();
}

C program to implement doubly linked list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
int m,n;
struct node
{
    int data;
    struct node *next,*pre;
}*start;

void create_list(int);
void display();
void insert_at_beg(int);
void insert_at_end(int);
void insert_at_pos(int,int);
void selection(int);
void count();
void main()
{
    int i,choice;
    clrscr();
    start=NULL;
    while(1)
    {
        clrscr();
        printf("\n press 1 for list creation");
        printf("\n press 2 for displaying the list");
        printf("\n press 3 to insert a data at beginning");
        printf("\n press 4 to insert a data at end");
        printf("\n press 5 to insert at a position");
        printf("\n press 6 for selection");
        printf("\n press 7 for counting");
        printf("\n press 8 for exit");
        printf("\n enter your choice:");
        scanf("%d",&choice);

    switch(choice)
    {

    case 1:
        if(start!=NULL)
        {
            printf("\n list already created");
            break;
        }
        printf("\n enter total no. of lists to be created:");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            printf("\n enter the data:");
            scanf("%d",&m);
            create_list(m);
        }
        printf("\n list created");
        break;

    case 2:
        if(start==NULL)
        {
            printf("\n no lists created till now");
            break;
        }
        display();
        break;

    case 3:
        if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_beg(m);
        printf("\n data inserted");
        break;

    case 4:
        if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_end(m);
        printf("\n data inserted");
        break;

    case 5:
        if(start==NULL)
        {
            printf("\n no list created.....error");
            break;
        }
        printf("\n enter the data:");
        scanf("%d",&m);
        printf("\n enter the position:");
        scanf("%d",&n);
        insert_at_pos(m,n);
        break;

    case 6:
        if(start==NULL)
        {
            printf("\n no list created....error");
            break;
        }
        printf("\n enter the data for selection");
        scanf("%d",&m);
        selection(m);
        break;


    case 7:
        if(start==NULL)
        {
            printf("\n no list created......error");
            break;
        }
        count();
        break;

    case 8:
        exit(1);

        default:
            printf("\n wrong choice");

    }
    }
getch();
}

void create_list(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=NULL;
    if(start==NULL)
    {
        start=tmp;
        start->pre=NULL;
        return;
    }
    p=start;
    while(p->next!=NULL)
        p=p->next;
        p->next=tmp;
        tmp->pre=p;
}

void display()
{
    struct node *p;
    p=start;
    while(p!=NULL)
    {
        printf("\n data= %d",p->data);
        p=p->next;
    }
    getch();
}

void insert_at_beg(int item)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=start;
    tmp->pre=NULL;
    start=tmp;
    getch();
}

void insert_at_end(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=NULL;
    p=start;
    while(p->next!=NULL)
        p=p->next;
    p->next=tmp;
    tmp->pre=p;
    getch();
}

void count()
{
    int i=1;
    struct node *p;
    p=start;
    while(p->next!=NULL)
    {
        p=p->next;
        i++;
    }
        printf("\n total no. of lists created is %d",i);
    getch();
}

void insert_at_pos(int item,int pos)
{
    int i;
    struct node *tmp,*p;
    tmp=(struct node*)malloc(sizeof(struct node));
    tmp->data=item;
    if(pos==1)
    {
        tmp->next=start;
        tmp->pre=NULL;
        start=tmp;
        printf("\n node inserted at 1st position");
        return;
    }
    p=start;
    for(i=1;i<pos;i++)
    {
        p=p->next;
    }
    tmp->next=p->next;
    tmp->pre=p;
    p->next=tmp;
    if(p==NULL)
    {
        printf("\n position not in range");
        return;
    }
    printf("\n list inserted at %d position",i);
}

void selection(int item)
{
    int i;
    struct node *p,*q;
    if(item==start->data)
    {
        p=start;
        start=start->next;
        start->pre=NULL;
        free(p);
        printf("\n node free");
        return;
    }
    q=start;
    while(q->next->data!=item)
    {
        q=q->next;
        i++;
    }
    p=q->next;
    q->next=q->next->next;
    q->next->next->pre=q;
    free(p);
    if(q==NULL)
    {
        printf("\n wrong value");
        return;
    }
    printf("\n data found at %d position",i);
    printf("\n node deleted");
}

C program to change the case of a string


  •  When 1 string is in lower case & another is in upper case

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char st[20],st1[20];
int l;
clrscr();
printf("enter 1st string: ");
gets(st);
printf("\n enter 2nd string:");
gets(st1);
printf("\n string 1 in lower case is:");
strlwr(st);
puts(st);
printf("\n string 2 in upper case is:");
strupr(st1);
puts(st1);
getch();
}

C program to calculate the length of a string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st[20];
int l;
clrscr();
printf("enter a string: ");
gets(st);
l= strlen(st);
printf("length of the string :%d ",l);
getch();
}

C program to compare 2 strings

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st1[15],st2[30];
int l;
clrscr();
printf("enter 2 strings: ");
gets(st1);
gets(st2);
l=stricmp(st2,st1);
 if(l==0)
 printf("same strings");
 else
 printf("different strings");
getch();
}


                                                                         OR



#include<stdio.h>
#include<conio.h>
void main()
{
char st1[100],st2[100];
int i=0;
clrscr();
printf("\n enter 1st string :");
gets(st1);
printf("\n enter 2nd string :");
gets(st2);

    while(st1[i]==st2[i]&&st1[i]!='\0')
    i++;

        if(st1[i]>st2[i])
            printf("\n string 1 is greatest");

        else if(st1[i]<st2[i])
            printf("\n string 2 is greatest ");

        else
            printf("\n both the strings are equal");
getch();
}

C program to implement queue

#include<stdio.h>
#include<conio.h>
#define MAX 10

int queue[MAX],front=-1,rear=-1;

void push(int[],int);
int pop(int []);
int peep(int[]);

void display(int []);

void main()
{
    int ch,m,value;
    clrscr();
    while(1)
    {
        printf("\n press 1 for push operation");
        printf("\n press 2 for pop operation");
        printf("\n press 3 for peep operation");
        printf("\n press 4 for display");
        printf("\n press 5 for exit");
        printf("\n enter your choice:");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                push(queue,value);
            break;

            case 2:
                printf(" REar %d\n",rear);
                printf(" FRont %d\n",front);
                if(front==-1||front>rear)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=pop(queue);
                printf("%d value is deleted",m);
            break;

            case 3:

                printf(" FRont %d\n",front);
                printf(" REar %d\n",rear);

                if(front==-1||front>=rear)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=peep(queue);
                printf("%d",m);
            break;

            case 4:
                display(queue);
            break;

            case 5:
                exit(1);
            break;

            default:
                printf("\n wrong choice");
        }
        getch();
    }
}

void push(int queue[],int val)
{
        printf("\n enter data to be inserted:");
        scanf("%d",&val);
        if(rear==MAX-1)
              printf("\n queue overflow");
        if(front==-1&&rear==-1)
             front=rear=0;
        else
        {
             rear++;
        }

        queue[rear]=val;

}

int pop(int queue[])
{
    int val;
    val=queue[front];
    front++;
    return val;
}

int peep(int queue[])
{
     return queue[front];
}

void display()
{
    int i;
    for(i=front;i<=rear;i++)
    {
        printf("%d\n",queue[i]);
    }
}





C program to implement circular queue

#include<stdio.h>
#include<conio.h>
#define MAX 10
int dqueue[MAX];
int left=-1,right=-1;
int insertright();
int insertleft();
void deleteleft();
void deleteright();
void display();
void main()
{
 int choice,m;
 clrscr();
 while(1)
 {
  clrscr();
  printf("\n\n\t\t\t*****MAIN  MENU******\n\n");
  printf("PRESS 1 : TO INSERT RIGHT\n");
  printf("PRESS 2 : TO DELETE RIGHT\n");
  printf("PRESS 3 : TO DELETE LEFT\n");
  printf("PRESS 4 : TO DISPLAY\n");
  printf("PRESS 5 : TO EXIT\n");
  printf("\n\nenter your choice : ");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:
    m=insertright();
    printf("\nthe value %d inserted successfully\n",m);
   break;
   case 2:
    deleteright();
   break;
   case 3:
    deleteleft();
   break;
   case 4:
    display();
   break;
   case 5:
    exit(1);
   break;
   default:
    printf("\nERROR.......WRONG CHOICE\n");
  }
 getch();
 }
}
int insertright()
{
 int data;
 printf("\n enter the data to be inserted : ");
 scanf("%d",&data);
 if(left==0 && right==MAX-1 || left==right+1)
 {
  printf("\n overflow ");
  return;
 }
 if(left==-1)
 {
  left=0;
  right=0;
 }
       else if(right==MAX-1 && left>0)
 {
  right=0;
 }
 else
  right++;
 dqueue[right]=data;
 return data;
}
void deleteleft()
{
 if(left==-1)
 {
  printf("\n underflow");
  return;
 }
 else if(left==right)
 {
  left=-1;
  right=-1;
  printf("\n data deleted.....");
  return;
 }
 else
 {
  printf("\n data deleted");
  left++;
 }
}
void deleteright()
{
  if(left==-1)
 {
  printf("\n underflow");
  return;
 }
 if(left==right)
 {
  left=-1;
  right=-1;
  printf("\n data deleted");
  return;
 }
 else
 {
  printf("\n data deleted");
  right--;
 }
}
void display()
{
 int i;
 if(left==-1)
 {
  printf("\n underflow ");
  return;
 }
 if(left<right)
 {
  for(i=left;i<=right;i++)
  {
   printf("\n data : %d",dqueue[i]);
  }
 return;
 }
 else if(left>right)
 {
  for(i=left;i<=MAX-1;i++)
   printf("\n %d",dqueue[i]);
  for(i=0;i<=right;i++)
   printf("\n %d",dqueue[i]);
 }
 else
  printf("\n %d",dqueue[left]);
}

C program to implement stack

#include<stdio.h>
#include<conio.h>
#define MAX 10
int st[MAX],top=-1;
void push(int st[],int val);
int pop(int st[]);
int peep(int st[]);
void display(int st[]);

void main()
{
    int ch,m,value;
    clrscr();
    while(1)
    {
        printf("\n press 1 for push operation");
        printf("\n press 2 for pop operation");
        printf("\n press 3 for peep operation");
        printf("\n press 4 for display");
        printf("\n press 5 for exit");
        printf("\n enter your choice:");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                if(top==MAX-1)
                {
                    printf("\n stack overflow");
                    break;

                }
                printf("\n enter data to be inserted:");
                scanf("%d",&value);
                push(st,value);
            break;

            case 2:
                if(top==-1)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=pop(st);
                printf("%d value is deleted",m);
            break;

            case 3:
                if(top==-1)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=peep(st);
                printf("%d",m);
            break;

            case 4:
                if(top==-1)
                {
                    printf("\n stack underflow");
                    break;
                }
                display(st);
            break;

            case 5:
                exit(1);
            break;

            default:
                printf("\n wrong choice");
        }
        getch();
    }
}

void push(int st[],int val)
{
    top++;
    st[top]=val;
}

int pop(int st[])
{       int val;
    val=st[top];
    top--;
    return val;
}

int peep(int st[])
{
     return st[top];
}

void display()
{
    int i;
    for(i=top;i>=0;i--)
    {
        printf("%d\n",st[i]);
    }
}



                                                                               OR


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
int m,n;

struct node
{
    int data;
    struct node *next;
}*top;

void push(int val);
int pop();
void display();

void main()
{
    int ch,i;
    top=NULL;
    clrscr();
    while(1)
    {
        printf("\n press 1 for push operation");
        printf("\n press 2 for pop operation");
        printf("\n press 3 for display");
        printf("\n press 4 for exit");
        printf("\n enter your choice:");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:
                printf("\n enter total no. of nodes to create:");
                scanf("%d",&m);
                for(i=0;i<m;i++)
                {

                    printf("\n enter data to be inserted:");
                    scanf("%d",&n);
                    push(n);
                }
            break;

            case 2:
                if(top==NULL)
                {
                    printf("\n stack underflow");
                    break;
                }
                m=pop();
                printf("%d value is deleted",m);
            break;

            case 3:
                if(top==NULL)
                {
                    printf("\n stack underflow");
                    break;
                }
                display();
            break;

            case 4:
                exit(1);
            break;

            default:
                printf("\n wrong choice");
        }
        getch();
    }
}

void push(int val)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=val;
    if(top==NULL)
    {
        top=tmp;
        top->next=NULL;
        return;
    }
    else
    {
        tmp->next=top;
        top=tmp;
    }
}

void display()
{
    struct node *q;
    q=top;
    while(q!=NULL)
    {
        printf("\n data=%d",q->data);
        q=q->next;
    }
}

int pop()
{
    struct node *tmp,*p;

    if(p!=top)
    {
        p=top;
        tmp=p;
        top=top->next;

        free(p);
    }
    return tmp->data;

}





C program to implement single linked list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
int m,n;
struct node
{
    int data;
    struct node *next;
}*start;

void create_list(int);
void display();
void insert_at_beg(int);
void insert_at_end(int);
void insert_at_pos(int,int);
void selection(int);
void count();
void main()
{
    int i,choice;
    clrscr();
    start=NULL;
    while(1)
    {
        clrscr();
        printf("\n press 1 for list creation");
        printf("\n press 2 for displaying the list");
        printf("\n press 3 to insert a data at beginning");
        printf("\n press 4 to insert a data at end");
        printf("\n press 5 to insert at a position");
        printf("\n press 6 for selection");
        printf("\n press 7 for counting");
        printf("\n press 8 for exit");
        printf("\n enter your choice:");
        scanf("%d",&choice);

    switch(choice)
    {

    case 1:
        if(start!=NULL)
        {
            printf("\n list already created");
            break;
        }
        printf("\n enter total no. of lists to be created:");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            printf("\n enter the data:");
            scanf("%d",&m);
            create_list(m);
        }
        printf("\n list created");
        break;

    case 2:
        if(start==NULL)
        {
            printf("\n no lists created till now");
            break;
        }
        display();
        break;

    case 3:
        if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_beg(m);
        printf("\n data inserted");
        break;

    case 4:
            if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_end(m);
        printf("\n data inserted");
        break;

    case 5:
        if(start==NULL)
        {
            printf("\n no list created......error");
            break;
        }
        printf("\n enter the data:");
        scanf("%d",&m);
        printf("\n enter the position:");
        scanf("%d",&n);
        insert_at_pos(m,n);
        break;

    case 6:
        if(start==NULL)
        {
            printf("\n no list created till now....error");
            break;
        }
        printf("\n enter data for selection:");
        scanf("%d",&m);
        selection(m);
        break;

    case 7:
        if(start==NULL)
        {
            printf("\n no list created......error");
            break;
        }
        count();
        break;

    case 8:
        exit(1);

        default:
            printf("\n wrong choice");

    }
    }
getch();
}

void create_list(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=NULL;
    if(start==NULL)
    {
        start=tmp;
        return;
    }
    p=start;
    while(p->next!=NULL)
        p=p->next;
     p->next=tmp;
    getch();
}

void display()
{
    struct node *p;
    p=start;
    while(p!=NULL)
    {
        printf("\n data= %d",p->data);
        p=p->next;
    }
    getch();
}

void insert_at_beg(int item)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=start;
    start=tmp;
    getch();
}

void insert_at_end(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=NULL;
    p=start;
    while(p->next!=NULL)
        p=p->next;
    p->next=tmp;
    getch();
}

void insert_at_pos(int item,int pos)
{
    int i;
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    if(pos==1)
    {
        tmp->next=start;
        start=tmp;
        printf("\n node inserted at 1st position");
        getch();
    }
    p=start;
    for(i=1;i<pos;i++)
        p=p->next;
    tmp->next=p->next;
    p->next=tmp;
    if(p==NULL)
    {
        printf("\n position not in range");
        return;
    }
    printf("\n list inserted at %d position",i);
    getch();
}



void selection(int item)
{
    int i=1;
    struct node *p,*q;
    if(item==start->data)
    {
        p=start;
        printf("\n data found at 1st position");
        start=start->next;
        free(p);
        printf("\n node free");
        getch();
    }

    q=start;
    while(q->next->data!=item)
    {
        i++;
        q=q->next;
    }
    p=q->next;
    q->next=q->next->next;
    free(p);

    if(q==NULL)
    {
        printf("\n error");
        return;
    }
    printf("\n data found at %d position",i);
    printf("\n node deleted");
    getch();
}

void count()
{
    int i=1;
    struct node *p;
    p=start;
    while(p->next!=NULL)
    {
        p=p->next;
        i++;
    }
        printf("\n total no. of lists created is %d",i);
    getch();
}


C program to copy one string into another string



 #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
clrscr();
printf("\n enter 1st string:");
gets(str1);
printf("\n enter 2nd string:");
gets(str2);
printf("\n string after copying is :");
strcpy(str2,str1);
puts(str2);
getch();
}


                                                                     OR

#include<stdio.h>
#include<conio.h>
void main()
{
char st1[100],st2[100];
int i=0;
clrscr();
printf("\n enter 1st string :");
gets(st1);
printf("\n enter 2nd string :");
gets(st2);

    while(st2[i]!='\0')
    {
        st1[i]=st2[i];
        i++;
    }

printf("\n copied string is :");
puts(st2);
getch();
}

C program to convert farhenheit into degree celcius

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float f,c;
clrscr();
printf("enter value in farhenheit= ");
scanf("%f",&f);
c=0.59*(f-32);
printf("value in celcius=%f",c);
getch();
}

C program to concatnate 2 strings

#include<stdio.h>
#include<conio.h>
void main()
{
char st1[15],st2[30];
clrscr();
printf("\n enter 2 strings :");
gets(st1);
gets(st2);
strcat(st2,st1);
printf("\n concentated string is :");
puts(st2);
getch();
}


                                                                                  OR       


#include<stdio.h>
#include<conio.h>
void main()
{
char st1[100],st2[100],st3[100];
int i=0,j=0;
clrscr();
printf("\n enter 1st string:");
gets(st1);
printf("\n enter 2nd string :");
gets(st2);
while(st1[i]!='\0')
{
        st3[j]=st1[i];
        i++;
        j++;

 }
i=0;
while(st2[i]!='\0')
{
        st3[j]=st2[i];
        i++;
        j++;
 }
printf("\n concatnated string is :");
puts(st3);
getch();
}


C program to print the table of a no.


                                                         USING  FOR  LOOP
#include<stdio.h>
#include<conio.h>
void main()
{
int i,t,r;
clrscr();
printf("\n enter any no. :");
scanf("%d",&t);
for(i=1;i<=10;i++)
{
         r=t*i;
         printf("%d\n",r);
}
getch();
}

                                                     USING  GOTO  STATEMENT


#include<stdio.h>
#include<conio.h>
void main()
{
int a,i=1;
clrscr();
printf("\n enter a value:");
scanf("%d",&a);
table:
printf("\n%d",a*i);
i=i+1;
if(i<=10)
goto table;
getch();
}