Tuesday, November 29, 2011

Largest and smallest number Integers


/* Write a C program to find both the largest and smallest number in a list of integers*/

main( )
{
        float largest(float a[ ], int n);
        float value[4] = {2.5,-4.75,1.2,3.67};
        printf("%f\n", largest(value,4));
}
float largest(float a[], int n)
{
        int i;
        float max;
        max = a[0];
        for(i = 1; i < n; i++)
                if(max < a[i])
                max = a[i];
        return(max);
}

Integer Operands Result


/* Write a C program, which takes two integer operands and one operator form the user, performs the operation and then prints the result. Consider the operators +,-,*, /, % and use Switch Statement)*/

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,res,ch;
clrscr();
printf("\t   *********************");
printf("\n\tMENU\n");
printf("\t********************");
printf("\n\t(1)ADDITION");
printf("\n\t(2)SUBTRACTION");
printf("\n\t(3)MULTIPLICATION");
printf("\n\t(4)DIVISION");
printf("\n\t(5)REMAINDER");
printf("\n\t(0)EXIT");
printf("\n\t********************");
printf("\n\n\tEnter your choice:");
scanf("%d",&ch);
if(ch<=5 & ch>0)
{
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
}
switch(ch)
{
 case 1:
 res=a+b;
 printf("\n Addition:%d",res);
 break;
 case 2:
 res=a-b;
 printf("\n Subtraction:%d",res);
 break;
 case 3:
 res=a*b;
 printf("\n Multiplication:%d",res);
 break;
 case 4:
 res=a/b;
 printf("\n Division:%d",res);
 break;

 case 5:
 res=a%b;
 printf("\n Remainder:%d",res);
 break;
 case 0:
 printf("\n Choice Terminated");
 exit();
 break;
 default:
 printf("\n Invalid Choice");
}
getch();
}

Friday, November 18, 2011

Hanoi Problem Recursive and Non Recursive Functions

Write C programs that use both recursive and non-recursive functions - To solve Towers of Hanoi problem.

#include<conio.h>
#include<stdio.h>
/* Non-Recursive Function*/
void hanoiNonRecursion(int num,char sndl,char indl,char dndl)
{
  char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
  int top,add;
  top=NULL;
  one:
    if(num==1)
    {
      printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
      goto four;
    }
  two:
    top=top+1;
    stkn[top]=num;
    stksndl[top]=sndl;
    stkindl[top]=indl;
    stkdndl[top]=dndl;
    stkadd[top]=3;
    num=num-1;
    sndl=sndl;
    temp=indl;
    indl=dndl;
    dndl=temp;
    goto one;
  three:
    printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
    top=top+1;
    stkn[top]=num;
    stksndl[top]=sndl;
    stkindl[top]=indl;
    stkdndl[top]=dndl;
    stkadd[top]=5;
    num=num-1;
    temp=sndl;
    sndl=indl;
    indl=temp;
    dndl=dndl;
    goto one;
  four:
    if(top==NULL)
      return;
    num=stkn[top];
    sndl=stksndl[top];
    indl=stkindl[top];
    dndl=stkdndl[top];
    add=stkadd[top];
    top=top-1;
    if(add==3)
      goto three;
    else if(add==5)
      goto four;
}

/* Recursive Function*/
void  hanoiRecursion( int num,char ndl1, char ndl2, char ndl3)
{
    if ( num == 1 ) {
    printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
    return;
     }
     hanoiRecursion( num - 1,ndl1, ndl3, ndl2 );
     printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
     hanoiRecursion( num - 1,ndl3, ndl2, ndl1 );
}
void main()
{
  int no;
  clrscr();
  printf("Enter the no. of disks to be transferred: ");
  scanf("%d",&no);
  if(no<1)
     printf("\nThere's nothing to move.");
  else
     printf("Non-Recursive");
     hanoiNonRecursion(no,'A','B','C');
     printf("\nRecursive");
     hanoiRecursion(no,'A','B','C');
  getch();
}