Skip to main content

C programs QnA

Q. If sum of two integer is 14 and if one of them is 8 then find other integer. Ans: Well, this is very simple. Simply, one integer is 8 and sum of two integers is 14. So, we need to subtract the number 8 from their sum of 14 to get the second number. So, the second number is: 14 - 8 = 6 C-Program of this task Code---> #include<stdio.h> int main () { int num1 = 8 , num2 , sum = 14 ; num2 = sum - num1 ; printf ( "The second number is: %d" , num2 ); return 0 ; }

The Dice Game in C-Language


The Dice Game

Description: The Dice Game is the result of different and innovative thinking. The idea is to play a game, made by a simple code in C-Language. No graphics, just a simple game. Nothing more to say. Simply, download the code, compile it and start playing.
The download link is given below:
Download the C-Program file of this Game.


            Compile and Play


Rules :


1.Every Player gets 100$.

2. Every Player bet 10$ on a number.

3. Dice will be thrown. A random number will generate.

4. The closest bet number to the dice will win.

5.If both players bet the same number, no prize. Better Luck, Next time !!

6. If no money left, then you have lost.

7. The last Player with money, WINS !!!

8. In the Best of 3 match, two-player(s) will be fighting for only three rounds. The major winner of those three rounds will win. However, if both players win only 1 round or no round, then it will call a "Draw".


About :


Hello guys...
Another C-program game is here.

This is a product of - ProgramJoy.blogspot.com

This is purely for entertainment and fun for new programmers or beginners.

If any damage caused by this product: Any kind of damage (hardware or software related) by this product is unintentional and ProgramJoy is not responsible for this any kind of damage.

The code is also available on our Blog/Website: https://programjoy.blogspot.com

Follow us on Facebook: https://facebook.com/programjoyofficial

Twitter: @ProgramJoy

Email: programjoyofficial@gmail.com

Please, subscribe and give a thumbs up, if you like the game.


CODE---->

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void str_gm();void rules();void about();void Exit();void delay(int);void menu();void PVP();void PVC();void BST3();
void str_gm()
{
 int c;
 while(1)
 {
 
  system("cls");
  printf("\n1.Player Vs. Player\n2.Player Vs. Computer\n3.Best of 3(Player Vs Player)\n4.Main Menu\n");
  printf("Your Choice: ");
  scanf("%d",&c);
  printf("\n\n");
  switch(c)
  {
   case 1: PVP();
     break;
   case 2: PVC();
     break;
   case 3: BST3();
     break;
   case 4: system("cls");
     menu();
     break;
   default: printf("\nInvalid Choice. Try again.");
     break;
  }
 }
}
void PVP()
{
 int p1,p2,m1=100,m2=100,x;
 int rand_num,dif1,dif2;
 while(1)
 {
 system("cls");
 printf("\n\n\"Player 1\", Choose a number between (1-10): ");
 scanf("%d",&p1);
 while(1)
 {
   if(p1>=1&&p1<=10)
    break;
   else
   {
    printf("\n\"Player 1\", please, choose a number between(1-10): ");
    scanf("%d",&p1);
   }
 }
 printf("\n\n\"Player 2\", Choose a number between (1-10): ");
 scanf("%d",&p2);
 while(1)
 {
   if(p2>=1&&p2<=10)
    break;
   else
   {
    printf("\n\"Player 2\", please, choose a number between(1-10): ");
    scanf("%d",&p2);
   }
 }
 rand_num=rand()%10;
 printf("\nThe dice says...");
 delay(3000);
 printf("\n\t%d\n",rand_num);
 dif1=rand_num-p1;
 dif2=rand_num-p2;
 if(dif1<0)
  dif1=dif1*(-1);
 if(dif2<0)
  dif2=dif2*(-1);
 if(dif1<dif2)
 {
  m1=m1+10;
  m2=m2-10;
  printf("\n\"Player 1\" has won this round. \n");
  printf("\nSo, \"Player 1 has %d$ money.\"",m1);
  printf("\nSo, \"Player 2 has %d$ money.\"",m2);
 }
 else if(dif1==dif2)
 {
  m1=m1+0;
  m2=m2+0;
  printf("\n\"No Player\" has won this round. \n");
  printf("\nSo, \"Player 1 has %d$ money.\"",m1);
  printf("\nSo, \"Player 2 has %d$ money.\"",m2);
 }
 else
 {
  m1=m1-10;
  m2=m2+10;
  printf("\n\"Player 2\" has won this round. \n");
  printf("\nSo, \"Player 1 has %d$ money.\"",m1);
  printf("\nSo, \"Player 2 has %d$ money.\"",m2);
 }
 delay(2000);
 printf("\n\n\nNow, Press 1 to continue and press 0 to for main menu(do not enter other value): ");
 scanf("%d",&x);
 if(x==1)
  system("cls");
 else if(x==0)
  {
   system("cls");
   menu();
   break;
  }
 else
 {
  while(x!=1)
  {
   printf("\npress 1 to continue and press 0 for main menu(do not enter other value): ");
   scanf("%d",&x);
   if(x==1)
    system("cls");
  else if(x==0)
  {
   system("cls");
   menu();
   break;
  }
  }
 }
 if(m2==0)
 {
  printf("\n\nHoorah !!! Player 1 Won !!!");
  delay(8000);
  break;
 }
 if(m1==0)
 {
  printf("\n\nHoorah !!! Player 2 Won !!!");
  delay(8000);
  break;
 }
 }
}
void PVC()
{
 int p1,p2,m1=100,m2=100,x; //p2 is computer.
 int rand_num,rand_comp,dif1,dif2;
 while(1)
 {
 system("cls");
 printf("\n\n\"Player 1\", Choose a number between (1-10): ");
 scanf("%d",&p1);
 while(1)
 {
   if(p1>=1&&p1<=10)
    break;
   else
   {
    printf("\n\"Player 1\", please, choose a number between(1-10): ");
    scanf("%d",&p1);
   }
 }
 printf("\n\n\"Computer\", Choosing a number between (1-10): ");
 rand_comp=rand()%10;
 delay(2000);
 printf("%d",rand_comp);
 p2=rand_comp;
 rand_num=rand()%10;
 printf("\nThe dice says...");
 delay(3000);
 printf("\n\t%d\n",rand_num);
 dif1=rand_num-p1;
 dif2=rand_num-p2;
 if(dif1<0)
  dif1=dif1*(-1);
 if(dif2<0)
  dif2=dif2*(-1);
 if(dif1<dif2)
 {
  m1=m1+10;
  m2=m2-10;
  printf("\n\"Player 1\" has won this round. \n");
  printf("\nSo, \"Player 1 has %d$ money.\"",m1);
  printf("\nSo, \"Computer has %d$ money.\"",m2);
 }
 else if(dif1==dif2)
 {
  m1=m1+0;
  m2=m2+0;
  printf("\n\"No one\" has won this round. \n");
  printf("\nSo, \"Player 1 has %d$ money.\"",m1);
  printf("\nSo, \"Computer has %d$ money.\"",m2);
 }
 else
 {
  m1=m1-10;
  m2=m2+10;
  printf("\n\"Computer\" has won this round. \n");
  printf("\nSo, \"Player 1 has %d$ money.\"",m1);
  printf("\nSo, \"Computer has %d$ money.\"",m2);
 }
 delay(2000);
 printf("\n\n\nNow, Press 1 to continue and press 0 for main menu(do not enter other value): ");
 scanf("%d",&x);
 if(x==1)
  system("cls");
 else if(x==0)
 {
  system("cls");
  menu();
  break;
 }
 else
 {
  while(x!=1)
  {
   printf("\npress 1 to continue and press 0 for main menu(do not enter other value): ");
   scanf("%d",&x);
   if(x==1)
    system("cls");
   else if(x==0)
   {
    system("cls");
    menu();
    break;
   }
  }
 }
 if(m2==0)
 {
  printf("\n\nHoorah !!! Player 1 Won !!!");
  delay(8000);
  break;
 }
 if(m1==0)
 {
  printf("\n\nHoorah !!! Computer Won !!!");
  delay(8000);
  break;
 }
 }
}
void BST3()
{
 int p1,p2,x,p1m=0,p2m=0,c=1;
 int rand_num,dif1,dif2;
 while(c<=3)
 {
 system("cls");
 printf("\n\n\"Player 1\", Choose a number between (1-10): ");
 scanf("%d",&p1);
 while(1)
 {
   if(p1>=1&&p1<=10)
    break;
   else
   {
    printf("\n\"Player 1\", please, choose a number between(1-10): ");
    scanf("%d",&p1);
   }
 }
 printf("\n\n\"Player 2\", Choose a number between (1-10): ");
 scanf("%d",&p2);
 while(1)
 {
   if(p2>=1&&p2<=10)
    break;
   else
   {
    printf("\n\"Player 2\", please, choose a number between(1-10): ");
    scanf("%d",&p2);
   }
 }
 rand_num=rand()%10;
 printf("\nThe dice says...");
 delay(3000);
 printf("\n\t%d\n",rand_num);
 dif1=rand_num-p1;
 dif2=rand_num-p2;
 if(dif1<0)
  dif1=dif1*(-1);
 if(dif2<0)
  dif2=dif2*(-1);
 if(dif1<dif2)
 {
  printf("\n\"Player 1\" has won this round. \n");
  p1m++;
  c++;
  printf("\nScore Board:\t Player 1: %d\t Player 2: %d",p1m,p2m);
 }
 else if(dif1==dif2)
 {
  printf("\n\"No Player\" has won this round. \n");
  c++;
  printf("\nScore Board:\t Player 1: %d\t Player 2: %d",p1m,p2m);

 }
 else
 {
  printf("\n\"Player 2\" has won this round. \n");
  p2m++;
  c++;
  printf("\nScore Board:\t Player 1: %d\t Player 2: %d",p1m,p2m);
 }
 delay(2000);
 printf("\n\n\nNow, Press 1 to continue and press 0 to for main menu(do not enter other value): ");
 scanf("%d",&x);
 if(x==1)
  system("cls");
 else if(x==0)
  {
   system("cls");
   menu();
   break;
  }
 else
 {
  while(x!=1)
  {
   printf("\npress 1 to continue and press 0 for main menu(do not enter other value): ");
   scanf("%d",&x);
   if(x==1)
    system("cls");
  else if(x==0)
  {
   system("cls");
   menu();
   break;
  }
  }
 }
 }
 if(p1m>p2m)
 {
  printf("\n\nHoorah !!! Player 1 Won !!!");
  delay(8000);
 }
 if(p1m==p2m)
 {
  printf("\n\nAww Snap !!! Match Draw !!!");
  delay(8000);
 }
 if(p1m<p2m)
 {
  printf("\nHoorah !!! \"Player 2\" has won");
  delay(8000);
 }
 
}
void rules()
{
 int x;
 printf("\n\n\n## Rules ##\n\n\n1.Every Player gets 100$.\n\n2.Every Player bet 10$ on a number.\n\n3.Dice will be thrown. A random number will generate.\n\n4.The closest bet number to the dice will win.\n\n5.If both player bets the same number, no prize. Better Luck, Next time !!\n\n6.If no money left, then you have lost.\n\n7.The last Player with money, WINS !!!\n\n8.In the Best of 3 match, two player(s) will be fighting for only three rounds. The major winner of those three rounds will win. However, if both player(s) win only 1 round or no round, then it will call a \"Draw\"");
 delay(3000);
 printf("\n\n\nPress 1, to back to Main Menu. Do not enter other value: ");
 scanf("%d",&x);
 if(x==1)
  system("cls");
 else
 {
  while(x!=1)
  {
   printf("\npress 1 to back, do not enter other value: ");
   scanf("%d",&x);
   if(x==1)
    system("cls");
  }
 }
}
void about()
{
 int x;
 printf("\n\n\n## About ##\n\n\nHello guys...\nAnother C-program game is here.\n\nThis is a product of - ProgramJoy.blogspot.com\n\nThis is purely for entertainment and fun for new programmers or beginners.\n\nIf any damage caused by this product: Any kind of damage (hardware or software related) by this product is unintentional and ProgramJoy is not responsible for this any kind of damage.\n\nThe code is also available on our Blog/Website: https://programjoy.blogspot.com\n\nFollow us on Facebook: https://facebook.com/programjoyofficial\n\nTwitter: @ProgramJoy\n\nEmail: programjoyofficial@gmail.com\n\nPlease, Subscribe and give a thumbs up, if you like the game.");
 delay(3000);
 printf("\n\n\nPress 1, to back to Main Menu. Do not enter other value: ");
 scanf("%d",&x);
 if(x==1)
  system("cls");
 else
 {
  while(x!=1)
  {
   printf("\npress 1 to back, do not enter other value: ");
   scanf("%d",&x);
   if(x==1)
    system("cls");
  }
 }
}
void Exit()
{
 exit(0);
}
void delay(int milli_sec) 
{
    // Stroing start time
    clock_t start_time=clock();
    // looping till required time is not acheived
    while(clock()<start_time+milli_sec);
}
void menu()
{
 int choice;
 while(1)
 {
 printf("\n\n### Main Menu ###\n\n1.Start Game\n2.Rules\n3.About\n4.Exit");
 printf("\n\nEnter Your Choice: ");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1: system("cls");
    str_gm();
    break;
  case 2: system("cls");
    rules();
    break;
  case 3: system("cls");
    about();
    break;
  case 4: system("cls");
    Exit();
    break;
  default: printf("\nWrong Choice. Try Again.");
     delay(2000);
     system("cls");
     menu();
     break;
 }
 }
}
int main()
{
 menu();
 return 0;
}



            Compile and Play

Comments

Popular posts from this blog

C-program of printing "Hello World", 10 times on the screen.

In this C program we will print "Hello World", 10 times on the screen. We will use "body-less-loop" for this C-Program. "Body-less-loop" : it means that the inner part(code block to be executed) of the loop will be empty and the loop will have a semi-colon(;) in it's end. i.e: for ( .....  ; .....  ; .....  ) ; input: Null. output: "Hello World" will be printed on the screen, 10 times. CODE----> #include<stdio.h> int main() {      int i;      for( i=1 ; i<=10 ;printf( "   Hello World ! \n ", i++ )) ;      return 0 ; } Don't just read, write it, run it..... RESULT :

Calculate sum of 5 numbers using Array in C-language

In this C -program we will scan the numbers using array and then we will calculate the sum of the given numbers, also using array. The numbers will be taken from the user. input: 5 numbers.(i.e : 5,6,9,56,548) output: The sum of the given numbers will be printed on the screen. CODE----> #include<stdio.h> #include<stdlib.h> main() { int arr[5],i,sum=0; printf ("       Enter Five numbers : "); for ( i=0 ; i<5 ; i++ ) {       scanf (" %d ",&arr[i]); } for ( i=0 ; i<5 ; i++ ) {      sum=sum+arr[i]; } printf (" \n      The sum of the given 5 numbers is : %d\n ",sum); system (" pause "); } Don't just read, write it, run it..... RESULT :

Linear Queue Program in C-Language

In this C program we will perform operations over Linear Queue ( i.e: Data insert Operation, Data remove Operation and display Operation ). The Choice( i.e: data insert,remove or display ) will be made by the user and The Number will be taken from the user( i.e: For Data insert Operation ). input: The Choice(i.e Data insert, remove or display) & The Number (integers) (i.e. for Data insert Operations) (15,10,128 etc.) output: The Operations will be excecuted as choosen by the user. CODE----> #include<stdio.h> #include<stdlib.h> #define MAXSIZE 10 int Q [ MAXSIZE ], front =- 1 , rear =- 1 ; void qinsert ( int x ) { if ( rear == MAXSIZE - 1 ) printf ( "\n Queue is Full." ); else if ( front ==- 1 ) { front = 0 ; rear = 0 ; Q [ front ]= x ; } else { rear ++; Q [ rear ]= x ; } } void qdelete () { if ( front ==- 1 ) printf ( "\n Queue is Empty." ); else if ( front == rear ) { printf ( "