Friday, December 24, 2010

C Program of a slam book using Structures.

#include<stdio.h>

int main()
{
   int i,choice,n,search,x;
   struct slam_book
     {
int Id;
       char name[20];
       char bdate[20];
       char sign[20];
       char prof[20];
       char hobby[20];
       char bfnd[20];
     };
struct slam_book member[10];
   member[0].Id=0;

start:
printf("\nWhat you want to do?\n1.Entry\n2.Search\n3.Exit\nYour choice: ");
   scanf("%d",&choice);

switch(choice)
{
 case 1: printf("\nHow much entry you want do do?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
                 {
                 printf("\nEntry no.: ");
                 scanf("%d",&member[i].Id);
                 printf("Enter your first name: ");
                 scanf("%s",member[i].name);
                 printf("Enter your birth date(dd/mm/yy): ");
                 scanf("%s",member[i].bdate);
                 printf("Enter your sun sign: ");
                 scanf("%s",member[i].sign);
                 printf("Enter Your profession: ");
                 scanf("%s",member[i].prof);
                 printf("Enter your hobby: ");
                 scanf("%s",member[i].hobby);
                 printf("Enter your best friend: ");
                 scanf("%s",member[i].bfnd);
}
goto start;

 case 2: if(member[0].Id==0)
{
printf("There are no entries do at least one entry!\n");
goto start;
}
else
printf("Enter the 'Entry no.' you want to know: ");
scanf("%d",&search);
x=search-1;
                 printf("\n");
                 printf("First Name:\t%s\n",member[x].name);
                 printf("Birth Date:\t%s\n",member[x].bdate);
                 printf("Sun Sign:\t%s\n",member[x].sign);
                 printf("Profession:\t%s\n",member[x].prof);
                 printf("Hobby:\t\t%s\n",member[x].hobby);
                 printf("Best Friend:\t%s\n",member[x].bfnd);
goto start;

 case 3: goto exit;

 default: printf("\nInvalid selection. please enter correct choice!\n");
goto start;
}

exit:
printf("\nThank You!!\n");

return 0;
}

Tuesday, December 21, 2010

C Program that will sort the numbers prompt by the user using Selection Sort method.


#include<stdio.h>

int main()
{
        int a[5],i,j,iMin,temp,n;

        printf("Enter the five no with any order : ");

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

        for(i=0;i<5;i++)
        {
                iMin=i;
                for(j=i+1;j<5;j++)
                {
                   if(a[j]<a[iMin])
                     iMin=j;
                }
                if(iMin!=i){
                        temp=a[i];
                        a[i]=a[iMin];
                        a[iMin]=temp;
                }
        }

        printf("Sorted list in ascending array\n");
        for(i=0;i<5;i++)
        {
          printf("%d\n",a[i]);
        }
}

C Program will sort the numbers prompt by user using Bubble Sort method.

#include<stdio.h>

int main()
{
int a[100],i,j,n,b;

printf("How many numbers do you want to sort?  ");
scanf("%d",&n);

printf("Please enter the numbers and press the ENTER key.\n");
for(i=0;i<n;i++)
{
   printf("Enter No.[%d]: ",i+1);
   scanf("%d",&a[i]);
}

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

printf("-------------------\n");
printf("The sorted list is:");
for(i=0;i<n;i++)
{
 printf("\n%d",a[i]);
}
printf("\n-------------------\n");
}

Monday, December 20, 2010

Program which will display list of Questions (4), and prompt user to enter the question number, program display the question and prompt for the answer. same will repeat till user enter answer for all the questions. once doe program display the question and the user input answer.

#include<stdio.h>

char que[4][100]={
  {"[1]. What is your name?"},
  {"[2]. What is your age?"},
  {"[3]. What is your Profession?"},
  {"[4]. Are you interested in C Programs?"}
};

char ans[4][100]={
  {""},
  {""},
  {""},
  {""}
};

int choice[4]={0,0,0,0};

int capture_input();
void display_que(int choice[]);
void display_que_ans();

int main()
{
  int i=0,j=0,input=0;
 
  do
  {  
    input=capture_input();  
  
    switch(input)
    {
      case 1:
choice[j++]=input;
printf("%s\n",que[0]);
scanf("%s",ans[0]);
break;

      case 2:
choice[j++]=input;
printf("%s\n",que[1]);
scanf("%s",ans[1]);
break;

      case 3:
choice[j++]=input;
printf("%s\n",que[2]);
scanf("%s",ans[2]);
        break;

      case 4:
choice[j++]=input;
printf("%s\n",que[3]);
scanf("%s",ans[3]);
        break;

      case 9: // Option to exit.
display_que_ans();
return 0;

      default:
printf("Incorrect input, please try again.\n\n");
    }
  }while(j<4);
 
  display_que_ans();
  return 0;
}

int capture_input()
{
  int input=0,i=0,repeat;
 
  do
  {
    display_que(choice);
    repeat=-1;
    printf("Enter your choice (9-For Exit) [X]: ");
    scanf("%d",&input);
  
    // Check for already answered.
    for(i=0;i<4;i++){
      if(choice[i]==input && input !=0)
      {
printf("Already answered, please try again.\n\n");
repeat=0;
break;
      }    
    }
  }while(repeat==0);
  
  return input;
}

void display_que(int choice[])
{
  int i,j,f;
 
  for(i=0;i<4;i++)
  {
    f=0;
    for(j=0;j<4;j++)
    {
      if(choice[j]==(i+1))
      {
f=1;
break;
      }
    }
    if(f==0)
    {
      printf("%s\n",que[i]);
    }
  }
  printf("\n");
}

void display_que_ans()
{
  int i;
 
  printf("\n\n-------------------------------------------------------------\n");
  for(i=0; i<4; i++)
  {
    printf("Que: %s\nAns: %s\n",que[i],ans[i]);
  }
  printf("-------------------------------------------------------------\n");
}

Saturday, December 18, 2010

First C Program - Hello World!

My First C Program which will print 'Hello World' at console.

# include <stdio.h>

int main()
{
    printf("Hello World!\n");
    return 0;
}