Guessing game

The below code will generate a guessing game.

Logic 

Store a number in an variable.This will be the hidden number.If the user enters number which is greater or smaller then the hidden number then display an appropriate message.Limit the user to fixed number of tries so that the game can end if the user is unable to guess the hidden number.


Code : 

#include <stdio.h>
#include <conio.h>

void main()

{
    int x,i,j,num,count;
    printf("Welcome...\nMax number of tries is 4");
    count=0;
    x=37;    //this is the value which is to be guessed

    for(i=1;i!=x;i)

    {
        printf("\nEnter number:");
        scanf("%d",&i);
        if(i>x)
            printf("Too Large");
        if(i<x)
            printf("To Small");
        count++;
        if(count==4)
            break;
    }

    if(count==4)

        printf("\n\nYou lose\nnumber of tries:%d",count);
    else
        printf("\n\nYou win\nnumber of tries:%d",count);

    getch();

}

Teaase your Friends with this code

The following program will keep on printing the statement which is written in printf if the user is pressign any key you can send the .exe file of this code to your friends to fool them .

#include<stdio.h>
#include<conio.h>
void main()
{
char ch[]="I AM AN IDIOT.";
char c='A';
int i=0;

  while(c)
   {
     c=getch();
     printf("%c\a",ch[i]);
     i++;
    if(i==14)//if you change the printf statement then replace this 14 with the size of you string.
        {
            printf(" ");
            i=0;
        }
  }
}

Enjoy.

Write a C Program to print Hello world without using Semicolon

Semicolon is the statement terminator in compiler of C language. Our task is to write a piece of code to print Hello World! or any other line without using semicolons.
this can be done in the part of syntax which is free from bound of semicolons and these are IF-ELSE statements and LOOPS. It means that you can write your printf statement in these parts.

EXAMPLE:

#include<stdio.h>
#include<conio.h>

void main()
 {
     if(!printf("Without Semicolons ? Piece of Cake %c\n",2))

 getch();
 }


Same thing can be done with WHILE LOOP

#include<stdio.h>
#include<conio.h>

void main()
 {
     while(!printf("Without Semicolons ? Piece of Cake %c\n",2))

 getch();
 }


Newer Posts Older Posts
© Copyright Encyclopedia of C | Designed By Code Nirvana
Back To Top