WELCOME IN THE SEA OF C

Here you find some good quality programs.
If any one want any type of help in C and C++ then you can mail me.
Google
Showing posts with label C Programing Intermediate. Show all posts
Showing posts with label C Programing Intermediate. Show all posts

Tuesday, April 29, 2008

A TYPICAL PATTERN:

A triangular pattern
"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
void main()
{
int i,j,k=1,n,p;
printf("enter the no of rows");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<(n-i);j++)
printf(" ");
for(j=1;j<=i;j++)
printf("%d",k++);
for(j=(i-1);j>=1;j--)
{
p=k-2;
k=k-1;
printf("%d",p);
}
printf("\n");
}
getch();
}

Wednesday, March 12, 2008

A CHALLENGING GAME:

There are 21 matchsticks, you have to choose matchsticks between 1 to 4 and then computer will choose, and the one who takes the last matchstick will lose........just try it!!!!!!

"/" is used instead of "<" and ">".
#include/stdio.h/
#include/conio.h/
void game(int);
void check(int);

void main()
{
static int s=21;
clrscr();
printf("NO of matchstick is %d\n",s);
game(s);

getch();
}

void game(int s)
{

int n=1,m=3,x=4,y=2,p,choice;
printf("\nenter your choice between 1 and 4 is \n");
scanf("%d",&p);
s=s-p;
if(s>1 && s<=5)
{
check(s);
printf("\n1.want to play again");
printf("\n2.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
main();
break;
case 2:
exit();
}
}
else
if(p==1)
{
printf("computer choice is %d",x);
s=s-x;
printf("No of Matchstick left is %d",s);
if(s>0)
{
game(s);
}
}

if(p==4)
{

printf("computer choice is %d",n);
s=s-n;
printf("No of Matchstick left is %d",s);
if(s>0)
{
game(s);
}
}
else
if(p==2)
{

printf("computer choice is %d",m);
s=s-m;
printf("No of Matchstick left is %d",s);
if(s>0)
{
game(s);
}}

if(p==3)
{

printf("computer choice is %d",y);
s=s-y;
printf("No of Matchstick left is %d",s);
if(s>0)
{
game(s);
}}
else
printf("enter correct choice");
}


void check(int s)
{

switch(s)
{
case 5:
printf("Computer choice is 4 \n");
printf("Computer Wins");
printf("\n You Lose");
break;

case 4:
printf("Computer choice is 3 \n");
printf("Computer Wins");
printf("\n You Lose");
break;

case 3:
printf("Computer choice is 2 \n");
printf("Computer Wins");
printf("\n You Lose");
break;

case 2:
printf("Computer choice is 1 \n");
printf("Computer Wins");
printf("\n You Lose");
break;
}
}