Thursday, December 6, 2007

My C Program....

After blogging and collecting informations for my story,I felt very bored.It has been quite a while since I programmed.So I did a c program(my favorite language) to group numbers based on the sum of digits(finally truncated to a single digit).I learnt hashing last year.But never really tried it out.In my sem I had a similar program where I had to group numbers based on the number of digits that repeat in that number.So I did this program as a time pass.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*
Name: GROUPING PROGRAM
Copyright: SD
Author: Deepak Senthilkumar
Date: 06/12/07 14:56
Description: It is a program to group numbers based on the sum of digits!!!
*/
#include stdio.h
/*
These header files are not required in dev c++...But they are requred in Turbo c/c++
#include stdlib.h
#include conio.h
#include alloc.h
*/

struct table
{
int num;
struct table *next;
};

struct table a[9];

int digitsum(int num)
{

int sum,temp,n;
temp=0;
n=num;
sum=0;

while(n>0)
{

temp=n%10;
sum=sum+temp;
n=n/10;

}

return sum;

}


int tdigitsum(int num)
{

int tsum;
tsum=digitsum(num);
if(tsum>9)
tsum=digitsum(tsum);
tsum=digitsum(tsum);
return tsum;
}
void input()
{

int inum,isum;
struct table *newn,*curr;
printf("\n\nEnter a number:");
scanf("%d",&inum);
isum=tdigitsum(inum);
curr=&a[isum];
while(curr->next!=NULL)
curr=curr->next;
newn=(struct table *)malloc(sizeof(struct table));
newn->num=inum; newn->next=NULL;
curr->next=newn;
}

void display()
{

int i;
struct table *curr;
for(i=1;i<10;i++)//>
{
curr=&a[i];
curr=curr->next;
while(curr!=NULL)
{
printf("%d ",curr->num);
curr=curr->next;
}

}

}


int main()
{

int choice,check;
check=0;
printf("\nWELCOME TO MY PROGRAM OF GROUPING THE NUMBERS BASED ON SUM OF DIGITS!!!!\n");
do
{

printf("\n\nMENU\n\n 1. INSERT \n 2. DISPLAY\n 3. EXIT\n");
printf("\nENTER CHOICE : ");
scanf("%d",&choice);
switch(choice)
{

case 1:

input();
check=1;
break;

case 2:

if(check)
display();
else printf("\n\nNothing there to be displayed\n");
break;

case 3:

exit(0);

}

}while(1);
return 0;

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


What do you think about the program?

Post your suggestions!

8 comments:

blank.fill said...

hmm.. seems like a nice program.. i'll have to run it myself and see..
i'm a skeptic u see..

±. ...The Tentacles of Thought....± said...

Show the output also.....

±. ...The Tentacles of Thought....± said...

2 things....why have u put a >after the forloop gets over and is there no other way to implement it without using linked lists??

Arwindh said...

hmmm...good time pass...

Deepak said...

that > after for loop was a mistyping...
i had corrected it now...
we can implement without using linked list also...
But using linked liskt is more efficient...
I had shown the output in the pic...
Carefully see the pic..

Deepak said...

just use a two dimensional array dude...
The whole program looks simple in terms of understanding...

Gagan said...

hmm nice dude !!

u gave me a new idea to write programs !!

Deepak said...

in what way?