Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Tuesday, October 4, 2011

C program to evaluate factorials upto 100 (100!)


Well, this program can be extended to find values of factorial of numbers more than 100. It uses an array of size equal to maximum no. of digits of factorial value(100 in this case). (We can only find factorial of upto 13 with unsigned long, after that we are left with no choice, hence the program solves this problem)

#include<stdio.h>
int fact[160];
main()
{
    int n,carry,i,size;;
    carry=0;
    size=159;
    fact[0]=1;
    scanf("%d",&n);
    while(n>0)
    {
        i=0;
        do
        {
            fact[i]*=n;
            fact[i]+=carry;
            carry=fact[i]/10;
            fact[i]=fact[i]%10;
        }while(i++ != 158);

        n--;
    }

    for(i=159;i>=0;i--)
    {
        if(fact[i]==0)
        size--;
        else
        break;
    }
    for(i=size;i>=0;i--)
    printf("%d",fact[i]);
    printf("\n");


}

Sunday, October 2, 2011

Data Alignment and performance optimization: C source code, concept


/*Check out the following code. This code is an example of data alignment in C. Read out more at http://www.codeguru.com/forum/showthread.php?t=276622  */
#include<stdio.h>

struct stud1
{
    double fee;
    char branch_code;
    float marks;
    int b1;
    int b2;

};

struct stud2
{

    char branch_code;
    double fee;
    float marks;
    int b1;
    int b2;

};

struct stud3
{

    char branch_code;
    float marks;
    int b1;
    int b2;
    double fee;

};
main()
{
    printf("%d %d %d",sizeof(struct stud1),sizeof(struct stud2),sizeof(struct stud3));
}
//Compile it, and check the output. If you are surprised, have a look at http://www.codeguru.com/forum/showthread.php?t=276622

Thursday, September 29, 2011

Common Input-Output Mistakes and Concepts in C


1.       printf(“sanjay”+2);   //prints njay only.
2.       Always use fflush(stdin) after any scanf statement.
3.       Always use & operator in scanf statement.
4.       We cannot use %d for inputting values other than int in scanf(for eg. long, float).
5.       Global variables initialize to 0, local to some garbage value. Global ‘char’ also initializes to 0.
6.       int/int is int. float/int is float. Int/float is float. (‘/’ represents division operator).
7.       scanf(“%c%c”,&ch1,&ch2);    //keyboard input must be ‘AB’ and not ‘A B’.
scanf(“%c %c”,&ch1,&ch2);  //keyboard input must be ‘A B’.
scanf(“%c/%c”,&ch1,&ch2);  //keyboard input must be ‘A/B’.
8.       printf returns the no. of bytes printed by it.
9.       scanf returns the no. of variables passed to it.
10.   sprintf(str, “%d %d”,x,y); // where str is a string.
sprintf does not prints on screen like printf, but it stores the formatted output in str.
Similarly sscanf takes the formatted input from str rather than from the keyboard.

Friday, September 23, 2011

Linked List Complete C program with insertion, deletion, print, reversing of List


//Any Problems can be discussed in comments.
//operations: insertNode, deleteNode, printList, reverseList

#include<stdio.h>

typedef struct node *nodePointer;
struct node
{
    int data;
    struct node *link;
}node;
int length=0;

void insertNode(int,nodePointer);
void print(nodePointer);
void deleteNode(int,nodePointer);
nodePointer reverseList(nodePointer);

main()
{
    nodePointer root;
    root=malloc(sizeof(node));
    root->link=NULL;
    insertNode(34,root);
    insertNode(56,root);
    insertNode(45,root);
    print(root);
    printf("Length is %d\n",length);

    root=reverseList(root);
    print(root);
    printf("Length is %d\n",length);
    deleteNode(56,root);
    deleteNode(34,root);
    insertNode(53,root);
    print(root);
    printf("Length is %d\n",length);

}

void insertNode(int data,nodePointer root)
{
    int i;
    if(length>0)
    {
        while(root->link)
        root=root->link;
    }

    nodePointer temp;
    temp=malloc(sizeof(node));
    temp->data=data;
    root->link=temp;
    temp->link=NULL;
    length++;
}

void print(nodePointer root)
{
    printf("Root");
    while(root->link)
    {
        printf("->%d",root->link->data);
        root=root->link;
    }
    printf("\n");
}

void deleteNode(int data,nodePointer root)
{
    nodePointer temp;
    temp=root;
    while(root->link)
    {
        if(root->link->data==data)
        {
            temp=root->link->link;
            free(root->link);
            root->link=temp;
            length--;
            return;
        }
        else
        {
            temp=root;
            root=root->link;
        }
    }
}

nodePointer reverseList(nodePointer root)
{
    nodePointer middle,trail,temp;
    temp=root;

    middle=NULL;
    root=root->link;

    while(root)
    {
        trail=middle;
        middle=root;
        root=root->link;
        middle->link=trail;
    }
    temp->link=middle;

    return temp;
}

Saturday, June 25, 2011

Given n Boolean variables x1,x2,...xn , we wish to print all possible combinations of truth values they can assume. For instance, if n=2, there are four possibilities: 'true,true','false,true','true,false' and 'false,false'. Write a C program to do this. Q#3,Page#17, 'Fundamentals of Data Structures in C (2nd Edition)' by 'Horowitz and Sahni'

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

void comb(char *,int,int);
char toggle(char);

void main()
{
 char list[4]={'t','t','t','t'};
 int i=0,n=3;
 comb(list,i-1,n);
 getch();
}

void comb(char *list,int i,int n)
{
 int j,k;
 if(i==n)
 {
  for(j=0;j<=n;j++)
  printf("%c",list[j]);
  printf("  ");
 }
 else
 {
  for(k=0;k<=1;k++)
  {
   comb(list,i+1,n);
   list[i+1]=toggle(list[i+1]);
  }
}
}

char toggle(char c)
{
 if(c=='t')
 return 'f';
 else
 return 't';
}