Showing posts with label Cplusplus. Show all posts
Showing posts with label Cplusplus. Show all posts

Wednesday, 19 March 2014

C++ Program To Implements Snake and Ladder Game ( Graphics )

Hello everyone, Here we implements Snake and Ladder Game in C++ graphic.

A two player Snake and Ladder game in C++ Graphics :

Source Code
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
#include<dos.h>
#include<iostream.h>

int k=1, i, user=0, dice=0, x1=50, y1=410, x2=70, y2=410, dir1=0,
dir2=0,
ch;
int cnt1=1, cnt2=1;
void *obj1, *obj2, *o1, *o2, *dot, *back, *turn, *ready;
unsigned int size;

void ladder1()
{
    int m,n;
    for(m=0;m<=250;m+=250)
    for(n=0;n<=m;n+=250)
    {
        setcolor(DARKGRAY);
        line(53+m,57+n,55+m,55+n);
        line(53+m,57+n,133+m,137+n);
        line(55+m,55+n,135+m,135+n);
        line(133+m,137+n,135+m,135+n);
        setfillstyle(SOLID_FILL, YELLOW);
        floodfill(55+m,58+n,DARKGRAY);
        line(68+m,42+n,70+m,40+n);
        line(68+m,42+n,148+m,122+n);
        line(70+m,40+n,150+m,120+n);
        line(148+m,122+n,150+m,120+n);
        floodfill(70+m,43+n,DARKGRAY);
        line(65+m,65+n,78+m,52+n);
        line(68+m,68+n,81+m,55+n);
        floodfill(79+m,54+n,DARKGRAY);
        line(75+m,75+n,88+m,62+n);
        line(78+m,78+n,91+m,65+n);
        floodfill(89+m,64+n,DARKGRAY);
        line(85+m,85+n,98+m,72+n);
        line(88+m,88+n,101+m,75+n);
        floodfill(99+m,74+n,DARKGRAY);
        line(95+m,95+n,108+m,82+n);
        line(98+m,98+n,111+m,85+n);
        floodfill(109+m,84+n,DARKGRAY);
        line(105+m,105+n,118+m,92+n);
        line(108+m,108+n,121+m,95+n);
        floodfill(119+m,94+n,DARKGRAY);
        line(115+m,115+n,128+m,102+n);
        line(118+m,118+n,131+m,105+n);
        floodfill(129+m,104+n,DARKGRAY);
        line(125+m,125+n,138+m,112+n);
        line(128+m,128+n,141+m,115+n);
        floodfill(139+m,114+n,DARKGRAY);
    }
} 


                                 Download Full Source Code:   Click Here


Output

                                Also Read: Quine - Print its own Source Code as Output

NOTE: Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Tuesday, 25 February 2014

Program To Find Whether A Number Is Prime Or Not




What is a
PRIME NUMBER?


" A Natural number greater than 1 which has only two divisor 1 and itself is called prime number ".
For Example:  
5 is prime, because it has only two divisors 1 and itself.

Source Code
    #include<iostream>
    #include<conio.h>
    using namespace std;
            int main()
            {
             //clrscr();
             int number,count=0;
    cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";
              cin>>number;
               for(int a=1;a<=number;a++)
                   {
                    if(number%a==0)
                       {
                      count++;
                       }
                   }
           if(count==2)
             {
              cout<<" PRIME NUMBER \n";
             }
           else
             {
              cout<<" NOT A PRIME NUMBER \n";
             }
           //getch();
           }


Output


Program to find factorial of Number in C++






What is a Factorial of a number 'n'?


 The factorial of a number 'n' is the product of all number from 1 upto the number 'n' it is denoted by n!.  For example n=5 then factorial of 5 will be 1*2*3*4*5= 120. 5!= 120

Factorial program C++ Logic:

  • First think what is the factorial of a number? How mathematically it can be calculated.
  •  If you got this info then it will be very easier to make a C++ Program logic to find the factorial.
  •  User enters a number and we have to multiply all numbers upto entered number.
  • Like if user enters 6  then Factorial should be equal to factorial= 1*2*3*4*5*6.
  • In this case a for Loop will be very helpful. It will start from one and multiply all numbers upto entered number after it loop will be terminated.
  • Take a variable and initialized it to 1 and in loop store multiplication result into it like in below program a variable 
  • Factorial is used for this purpose.what is we does not initialized it to 1 and initialized it to zero or remain it uninitialized. In case of 0 our result will be zero in case of any number entered
  • In case of not initializing it our answer will correct mostly but if variable contains garbage value then we will not be able to get correct result. 
  • It is recommended that to initialize it to one.

Source Code

#include<iostream>

using namespace std;

int main()

{

    int num,factorial=1;

    cout<<" Enter Number To Find Its Factorial:  ";

    cin>>num;

    for(int a=1;a<=num;a++)

    {

        factorial=factorial*a;

    }

cout<<"Factorial of Given Number is ="<<factorial<<endl;

    return 0;

}

Output


Program to calculate sum and average of three numbers
















Source Code

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
 //to clear the screen
float a,b,c,sum,av;
cout<<"Enter three numbers:";
cin>>a>>b>>c;


sum=a+b+c;
av=sum/3;
cout<<"\nSUM="<<sum;
cout<<"\nAverage="<<av;


getch(); //to stop the screen
}

Output


Contact Us

Want to ask anything? Be our guest, give us a message.

Name Email * Message *