LinkedList




#include <stdio.h>
#include <stdlib.h>
int count;
bool flag=YES;
int choice;
struct object
{
    int data;
    struct object *address;
};
void printlist(struct object*);
void insert(struct object*,int pos,int data);
void delete(struct object*,int pos);
int main (int argc, const char * argv[])
{
   
    int pos;
    int data;
    struct object *list;
   
    //create root if the list is empty
    if (list==NULL)
    {
        list = (struct object*) malloc(sizeof(struct object));
        list->data = 99;
        list->address = NULL;
    }
   
    printf("%lu",sizeof(struct object));
   
    while (flag==YES)
    {
        Printf("\nEnter the choice: 1-insert 2-print 3-delete 5-exit");
        scanf("%d",&choice);
       
        switch (choice)
        {
            case 1:
                    printlist(list);
                Printf("\nEnter the position:\n");
                scanf("%d",&pos);
                Printf("\nEnter the data:\n");
                scanf("%d",&data);
                insert(list, pos, data);
                break;
           
            case 2:
                printlist(list);
                break;
           
            case 3:
                printlist(list);
                Printf("\nEnter the position:\n");
                scanf("%d",&pos);
                delete(list,pos);
                break;
            case 5:
               
                Printf("Breaking");
                flag = NO;
                break;
               
               
            default:
                break;
        }
    }
   
    return 0;
}
//printing the list and counting the number of objects in them
void printlist(struct object* list)
{
    count = 0;
   
    if (list==NULL)
    {
        Printf("\nlist is null");
    }
   
    while (list->address!=NULL)
    {
        Printf("\n%d",list->data);
        list = list -> address;
        count++;
    }
    Printf("\n%d",list->data);
    printf("\ncount=%d",count);
}
//inserting elements in their respective position with data
void insert(struct object* list,int pos, int data)
{
 
    if (count==pos-1) //inserting to the last like appending
    {
        while (list->address!=NULL) {
            list = list->address;
        }
       
        list->address = (struct object*)malloc(sizeof(struct object));
        list->address->data = data;
        list->address->address = NULL;
    }
    else if (pos==1 && count>1)// inserting at first position after header
    {
            if (list->data == 99)
            {
                struct object *temp = (struct object*)malloc(sizeof(struct object));
                temp->address = list->address;
                temp->data = list->data;
               
                list->address = (struct object*)malloc(sizeof(struct object));
                list->address->data    = data;
                list->address->address = temp->address;
               
                free(temp);
            }
    }
    else //else insert it into the middle
    {
        int i;
       
        if (count>1 && pos >1 && count!=pos-1)
        {
            while (list->address!=NULL)
            {
                list = list->address;
                i++;
                if (i==pos)
                {
                    break;
                }
               
            }
           
            struct object *temp = (struct object*)malloc(sizeof(struct object));
            temp->address = list->address;
            temp->data = list->data;//just to check
           
            list->address = (struct object*)malloc(sizeof(struct object));
            list->address->data    = data;
            list->address->address = temp->address;
           
            free(temp);
           
        }
       
    }
 
}
void delete(struct object* list,int pos)
{
    int localcount;
       if (count==pos) //Deleting the last element
       {
          struct object *temp = (struct object*)malloc(sizeof(struct object));
           while (localcount!=count-1)
           {
               list = list->address;
               localcount++;
           }
         
         
           temp->address = list->address->address;
           temp->data = list->address->data;
         
           list->address = NULL;
         
           free(temp);
               
     
       }
     else if (pos==1 && count>1)//deleting the first element
     {
        struct object *temp = (struct object*)malloc(sizeof(struct object));
         while (list->address!=NULL)
         {
             if (list->data == 99)
             {
                 temp->address = list->address;
                 temp->data = list->data;
               
                 list->address = temp->address->address;
               
                 free(temp);
                 break;
             }
         }
         
       
     }
    else //else deleting the intermediate values
    {
        int k=0;
         struct object *temp = (struct object*)malloc(sizeof(struct object));
        if (count>1 && pos >1 && count!=pos-1)
        {
            while (list->address!=NULL)
            {
       
                k++;
                if (k==pos)
                {
                    break;
                }  
                       list = list->address;
            }
           
            temp->address = list->address;
            temp->data = list->data;
           
            list->address = temp->address->address;
           
            free(temp);
        }
       
    }
   
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

Convert any number into roman number


#include <stdio.h>
int numbercount(int);//function to count the number of digits.
int main (int argc, const char * argv[])
{
  
    int number;
    printf("\nEnter the number:");    scanf("%d",&number);
  
    //number = numbercount(number);
    // for calculating 1000th position
    //----------------------------------
    int quotient = number/1000;
    if (quotient>=1)
    {
        number = number%1000;
        for (int i=1; i<=quotient; i++)
        {
            printf("m");
        }
    }
  
    //for calculating 100th position
    //-----------------------------------
    int hunquotient = number/100;
  
    if (hunquotient>=1)
    {
      
        if (hunquotient>=5)
        {
            printf("d");
            hunquotient = hunquotient - 5;
        }
      
        number = number%100;
        for (int j=1; j<=hunquotient && hunquotient!=0; j++)
        {
            printf("c");
        }
    }
  
    //for calculating 10th position
    //-----------------------------------
  
    int tenquotient = number/10;
  
    if (tenquotient>=1)
    {
        if (tenquotient>=5) {
            printf("l");
            tenquotient = tenquotient-5;
        }
      
        number = number%10;
        for (int k=1; k<=tenquotient && tenquotient!=0; k++)
        {
            printf("x");
        }
    }
  
    //for calculating last digit
    //-------------------------------------
  
    if (number>=5) {
        number = number - 5;
        printf("v");
    }
    for (int l=1; l<=number; l++) {
        printf("i");
    }

    return 0;
}
//function to count the number of digits
int numbercount(int num)
{
    int count = 0;
    while (num>0)
    {
        num = num / 10;
        count++;
    }
  
    return count;
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

ArmStrong Number


#include <stdio.h>

void armstrong(void);
int main (int argc, const char * argv[])
{
    armstrong();  
    return 0;
}
void armstrong(void)
{
    int remainder,sum,n;
  
    for (int i=1; i<500; i++)
    {
        sum = 0;
        n=i;
      
        while (n>0)
        {
            remainder = n%10;
            sum += remainder*remainder*remainder;
            n  = n/10;
        }
        if (sum==i)
        {
            printf("\n%d",i);
        }
    }
  
  
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

Recursive and Normal addition


A 5-digit positive integer is entered , write a function to calculate sum of digits of the 5-digit number:
(1) Without using recursion (2) Using recursion



#include <stdio.h>
int add(int);
int recadd(int);
int sum=0;
int r;
int main (int argc, const char * argv[])
{
    printf("%d",recadd(12111));
    return 0;
}
//without using recursion
int add(int num)
{
  
  
    while (num>0)
    {
        r = num%10;
        sum = sum +r;
        num = num/10;
    }
  
    return sum;
}
//using recursion
int recadd(int num)
{
    if (num>0)
    {
        r = num%10;
         sum = sum + r;
        recadd(num/10);
    }
 
    return sum;
  
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

pattern printing2

         1  
       2   3  
     4   5   6  
   7   8   9   10 


#include <stdio.h>
int main (int argc, const char * argv[])
{
    int print=1;
  
    for (int i=0; i<=3; i++)
    {
  
        for (int j=4-i; j>=1; j--)
        {
            printf("  ");
        }
      
      
        for (int k=0; k<=i; k++)
        {
                printf(" %d",print);
                printf(" ");
                printf(" ");
                print++;
                   
        }
        printf("\n");
    }
  
  
    return 0;
}


  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

Printing pattern

 A B C D E F G F E D C B A
 A B C D E F   F E D C B A
 A B C D E       E D C B A
 A B C D           D C B A
 A B C               C B A
 A B                   B A
 A                       A


#include <stdio.h>
int main (int argc, const char * argv[])
{
    int j,k,l;
  
    for (int i=1; i<=7; i++)
    {
        for (j=65; j<=71-(i-1); j++)
        {
            printf(" %c",j);
        }
        for ( k=1; k<=(i-1); k++)
        {
             printf(" ");
             printf(" ");
            if (k>1)
            {
                printf(" ");
                printf(" ");
            }
        }
        for (l=71-(i-1); l>=65; l--)
        {
            if (l==71)
            {
               continue;
            }
            printf(" %c",l);
        }
      
        printf("\n");
    }
  
    return 0;
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

21 Match stick problem



Write a program for a matchstick game being played between the computer and a user. Your program should ensure that the computer always wins. Rules for the game are as follows:
There are 21 matchsticks. 
The computer asks the player to pick 1, 2, 3, or 4
matchsticks. 
After the person picks, the computer does its picking.
  Whoever is forced to pick up the last matchstick loses the game.


#include <stdio.h>
int main (int argc, const char * argv[])
{
    int matchstick = 21;
    int user,computer;
  
    while (matchstick>=1)
    {
      
        if (matchstick==1)
        {
            printf("\nMatch stick status:%d",matchstick);
            printf("\nYou loose!!!!!!:(:(");               break;
        }
      
        printf("\nMatch stick status:%d",matchstick);      
        printf("\nEnter the choice (1,2,3,4)):");        scanf("%d",&user);
      
        printf("\nYou picked %d",user);      
        if (user>=5 || user <=0)
        {
            printf("\nInvalid value");            continue;
        }
      
        computer = 5 - user;
      
        printf("\nComputer picked%d",computer);      
        matchstick = matchstick - computer - user;
      
    }
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

Gregorian calendar Date


According to the Gregorian calendar, it was Monday on the date 01/01/1900. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.

#include <stdio.h>enum days
    {
    Monday,Tuesday,wednesday,thursday,friday,saturday,sunday
    };
int main (int argc, const char * argv[])
{
  
    int sourceyear = 1900;
    int currentyear;
    printf("\nEnter the currentyear:");    scanf("%d",&currentyear);
  
    int x = ((currentyear -sourceyear )%7);
    switch (x)
    {
        case Monday:
            printf("Monday");
            break;
        case Tuesday:
            printf("Tuesday");
            break;
        case wednesday:
            printf("wednesday");
            break;
        case thursday:
            printf("thursday");
            break;
        case friday:
            printf("friday");
        case saturday:
            printf("saturday");
        case sunday:
            printf("sunday");
        default:
            break;
    }
    return 0;
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

String functions through pointers




#include <stdio.h>#include <stdlib.h>
int strlength(char *x);
char* strConcatenate(char *,char *);
char* strRev(char *);
int main (int argc, const char * argv[])
{
  
    ///////////length///////////////
    char *s ="welcome";
  
    char *y = "tocpmging";
  
    printf("Length:%d\n", strlength(s));  
    ///////////concatenation/////////
    char *result = strConcatenate(s, y);
   
     printf("concatenation:%s\n",result);   
     free(result);
  
    ///////////reversing////////////
    char *reverse = "texttrial";
  
    char *resultstring = strRev(reverse);
  
    printf("reversedstring:%s\n",resultstring);  
    free(resultstring);
  

  
    return 0;
}
int strlength(char *x)
{
    int count=0;
    for (int i=0; x[i]!='\0'; i++) {
      
        count ++;
      
    }
    return count;
}
char* strRev(char *x)
{
    int i,j;
    char *s  = malloc(strlength(x));
  
    for (i=0;x[i]!='\0'; i++);
    j=0;
    while (i>0) {
      
        s[j] = x[i-1];
      
        j++;
        i--;
    }
  
    s[j] = '\0';
  
    return s;
  
}

char* strConcatenate(char *x,char *y)
{
    int i,j;
    char *s = (char *)malloc(strlength(x)+strlength(y));
  
  
    for (i=0; x[i]!='\0'; i++)
    {
      
        s[i] = x[i];
    }
 
  
    for (j=0; y[j]!='\0'; j++) {
      
        s[i] = y[j];
        i++;
    }
    s[i]='\0';
      
  
  
    return s;
  
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

Function pointer and pointer sizes

Here is an example of function pointers and how different pointers remain same in size can be understood through the following code


Just a simple mathematical problem.....of perform the basic functions using function pointers, and also passing functions in void* as a parameter....

#include <stdio.h>
int add(int a,int b);
int sub(int a, void * b);
int mul(int a,int b);
int div(int a,int b);
int operation(int a,int b,int (*function)(int,int));


int main (int argc, const char * argv[])
{
    //Every of this address is of 8 bytes
  
    printf("%lu\n",sizeof(int *));
    printf("%lu\n",sizeof(float *));
    printf("%lu\n",sizeof(void *));
    printf("%lu\n",sizeof(&add));
  
    
 
    int (*addptr)(int,int);//create a function pointer for add
  
    addptr = &add; //though & is not necessary since its points to the address
  
  
      printf("%lu\n",sizeof(addptr));
  
  
    printf("addittion%d\n",(addptr)(2,3));
  
    int (*subptr)(int,void *);//create a function pointer for sub
  
    subptr =&sub;
  
     printf("subtract%d\n",(subptr)(5,(void *)3));//Pass an object of anytype
  
  
    printf("mutliply->%d\n",operation(2,3,mul));//passing the base address of the method to call
  
    printf("division->%d\n",operation(6,2,div));  
  
    return 0;
}
int operation (int a, int b, int (*functocall)(int,int))
{
  
    return (*functocall)(a,b);
  
}
int div(int a,int b)
{
    return (a/b);
}
int mul(int a,int b)
{
    return (a*b);
}
int add(int a,int b)
{
    return (a+b);
}
int sub(int a,void *b)//resultant is void so can send data of any type
{
    return (a-(int)b);
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS