1.Towers of hanoi(recursive and non -recursive)
2.GCD (recursive and non recursive)
*Maximum correct e vastai...........better execute once
Towers of hanoi(recursive)
#include<stdio.h>
#include<conio.h>
void main()
{
void hanoi(char,char,char,int);
char t1='a',t2='b',t3='c';
int n;
clrscr();
printf("\nenter the no of disks on tower a");
scanf("%d",&n);
if(n<1)
printf("\n to disk to move");
else
hanoi(t1,t2,t3,n);
getch();
}
void hanoi(char t1,char t2,char t3,int n)
{
printf("\n %c%c%c%d",t1,t2,t3,n);
if(n==1)
{
printf("\n move top disk from tower%c-->%c",t1,t3);
}
else
{
hanoi (t1,t2,t3,n-1);
printf("\n %c%c%c%d",t1,t2,t3,n);
printf("\n move the top disks from tower %c-->%c",t1,t2);
printf("\n %c%c%c%d",t1,t2,t3,n);
hanoi(t3,t2,t1,n-1);
printf("\n %c%c%c%d",t1,t2,t3,n);
}
getch();
}
#include<conio.h>
void main()
{
void hanoi(char,char,char,int);
char t1='a',t2='b',t3='c';
int n;
clrscr();
printf("\nenter the no of disks on tower a");
scanf("%d",&n);
if(n<1)
printf("\n to disk to move");
else
hanoi(t1,t2,t3,n);
getch();
}
void hanoi(char t1,char t2,char t3,int n)
{
printf("\n %c%c%c%d",t1,t2,t3,n);
if(n==1)
{
printf("\n move top disk from tower%c-->%c",t1,t3);
}
else
{
hanoi (t1,t2,t3,n-1);
printf("\n %c%c%c%d",t1,t2,t3,n);
printf("\n move the top disks from tower %c-->%c",t1,t2);
printf("\n %c%c%c%d",t1,t2,t3,n);
hanoi(t3,t2,t1,n-1);
printf("\n %c%c%c%d",t1,t2,t3,n);
}
getch();
}
Towers of hanoi(non recursive)
#include<stdio.h>
void main()
{
int *d,*s,i,n,dir;
clrscr();
printf("Enter the number of disk\n");
scanf("%d",&n);
dir=n&1;
for(i=0;i<=n+1;i++)
{
d[i]=1;s[i]=i+1;
}
for(;;)
{
i=s[0];
if(i>n)
break;
printf("Move disk %d from tower%d to tower%d\n",i,d[i]=(d[i]+(i&1?dir:1-dir))%3+1,d[i]);
s[0]=1;
s[i-1]=s[i];GCD
s[i]=i+1;
}
getch();
}
Gcd (recursive)
#include<stdio.h>
#include<conio.h>
int gcd(int a,int b)
{
if (a<0)
a=-a;
if (b<0)
b=-b;
if(a==0||b==1||a==b)
return b;
if (a>b)
return gcd (b,b%a);
else return gcd(a,b%a);
}
void main
{
int x,y;
clrscr();
printf("enter the 1st number");
scanf("%d",&x);
printf("enter the 2nd numbre");
scanf("%d",&y);
printf("\n gce is %d",gcd(x,y));
getch();
}
GCD(non recursive)
#include<conio.h>
int gcd(int a,int b)
{
if (a<0)
a=-a;
if (b<0)
b=-b;
if(a==0||b==1||a==b)
return b;
if (a>b)
return gcd (b,b%a);
else return gcd(a,b%a);
}
void main
{
int x,y;
clrscr();
printf("enter the 1st number");
scanf("%d",&x);
printf("enter the 2nd numbre");
scanf("%d",&y);
printf("\n gce is %d",gcd(x,y));
getch();
}
GCD(non recursive)
#include <stdio.h>
int GcdByEuclid (int a, int b) {
if (a < 0 || b < 0) return -1;
while (a > 0 && b > 0) if (a > b) a -= b; else b -= a;
if (a == 0) return b; else return a;
}
int main (int argc, char *argv[]) {
int a, b;
if (argc < 3) {
fprintf (stderr, "Usage: gcd a b\n");
return 1;
}
a = atoi(argv[1]);
b = atoi(argv[2]);
printf ("%d %d %d", a, b, GcdByEuclid (a, b));
return 0;
}
int GcdByEuclid (int a, int b) {
if (a < 0 || b < 0) return -1;
while (a > 0 && b > 0) if (a > b) a -= b; else b -= a;
if (a == 0) return b; else return a;
}
int main (int argc, char *argv[]) {
int a, b;
if (argc < 3) {
fprintf (stderr, "Usage: gcd a b\n");
return 1;
}
a = atoi(argv[1]);
b = atoi(argv[2]);
printf ("%d %d %d", a, b, GcdByEuclid (a, b));
return 0;
}
0 comments:
Post a Comment