Showing posts with label C_language. Show all posts
Showing posts with label C_language. Show all posts

Sunday, 6 July 2014

Amazing Graphics Programs in C/C++ Programming Language


Hola Coders,

It’s been a long time since Fuzzyprograms launched, but now I am going to create summary type posts, So that You’ll get more info in single post than ever !

I am sharing 5 Graphical programs here. I know that using Graphics.h seems like a crime in 2013 but, Schools and Colleges are still teaching this. ( Obviously, Learning anything never goes in vain )

My belief is that, this will be very useful to students.


1. Google Page Design

This program is submitted by : Ankit Kumar( ankit26kumar@gmail.com )

Google Page Design Program
Isn’t it amazing ?


Source Code

#include<iostream.h>
#include<conio.h>
#include<graphic.h>
#include<math.h>
void main()
{clrscr();
int g=0,d;
initgraph(&g,&d,"c:\turboc3\bgi");
setbkcolor(15);
setcolor(1);
settextstyle(3,0,5);
outtextxy(190,140,"G");
setcolor(4);
settextstyle(3,0,5);
outtextxy(220,140,"O");
setcolor(3);
settextstyle(3,0,5);
outtextxy(250,140,"O");
setcolor(1);
settextstyle(3,0,5);
outtextxy(280,140,"G");
setcolor(2);
settextstyle(3,0,5);
outtextxy(310,140,"L");
setcolor(4);
settextstyle(3,0,5);
outtextxy(340,140,"E");
setcolor(1);
settextstyle(3,0,5);
setcolor(4);
settextstyle(1,0,2);
outtextxy(100,350,"SEARCH");
settextstyle(1,0,2);
outtextxy(380,350,"I'M FEELING LUCKY");
setcolor(8);
rectangle(140,200,480,230);
rectangle(93,350,183,380);
rectangle(370,350,580,380);
getch();
}


2. Taj Mahal Drawing


Taj Mahal Drawing program

A Great Monument from India (Feeling proud). Excellent Program !

Download Source Code: Click Here

3. 3D Car Handling


3D Car Handling program

I have no words for this one, Amazing Effects and Animation !

I recommend you all that you must Compile and Execute this one.

Download Source Code: Click Here


4. Flowerpot Design

Flowerpot Design program

I am not saying anything, Just Execute it and see the output.

Download Source Code: Click Here

5. Classic Car Game

Well, Yeah ! Everybody has played this game before, at least those who were born in 90′s. :D
It’s time to play again, then. (But also understand the code)

Classic Car Game programs

Info by Coder : This is a car race game, and i generated random cars in front of your car. you have to dodge all the cars. The control is highly sensitive, You have to make the move on time. I have used graphics to develop this game.

Download Source Code: Click Here


What do you think about this programs ? Any errors or queries ?
Share your comment below.

NOTE: You Can Also Share Your Program HERE.


Tuesday, 17 June 2014

Swapping Of Two Number


Swapping of two numbers means exchange the values of two variables with each other.You can take a example:
Variable var1 contains 40 and Variable var2 contains 30
then After swapping
Variable var1 contains 30 and Variable var2 contains 40.

Also Read: Quine - Print its own Source Code as Output
Also Read: C++ Program To Implements Snake and Ladder Game ( Graphics )

Example: Suppose we have three glass X, Y, Temp. Glass X contain 50, Glass Y contain 100 and Glass Temp is empty now we want to interchange these two glass (X, Y) value. So we follow The steps...

    First we take Glass X and there values place inside Glass Temp
    Now Glass X is empty, take Glass Y and there values place inside Glass X.
    Now Glass Y is empty, take Glass Temp and there values place inside Glass Y   
Finally Both Glass values are exchange, Glass X contain 100 and Glass Y contain 50.


Swap two numbers using third variables


Source Code
#include< stdio.h>
#include< conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter value of a:= ");
scanf("%d",&a);
printf("Enter value of b:= ");
scanf("%d",&b);
c=a;
a=b;
b=c;
printf("After swap a=: %d b=: %d",a,b);
getch();
}

Output
                     images

Swap two numbers without using third variable



Source Code
#include< stdio.h>
#include< conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter value of a:= ");
scanf("%d",&a);
printf("Enter value of b:= ");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("After swap \na=: %d\nb=: %d",a,b);
getch();
}

Output

                      images

Swap two numbers using Pointers


Source Code
#include< stdio.h>
#include< conio.h>
int main()
{
  int x,y,*b,*a,temp;
  clrscr();
  printf("Enter any two number : ");
  scanf("%d%d",&x,&y);
  printf("Before swaping : x= %d and y=%d\n",x,y);
  a = &x;
  b = &y;
  temp = *a;
  *a = *b;
  *b = temp;
  printf("After swaping : x= %d and y=%d\n",x,y);
  getch();
}

Output
Enter any two number : 10 20
Before Swaping : x=10 and y=20
After swaping : x=20 and y=10

Swap two numbers using call by Reference


Source Code
#include< stdio.h>
#include< conio.h>
int main()
{
  int x,y,*b,*a,temp;
  clrscr();
  printf("Enter any two number : ");
  scanf("%d%d",&x,&y);
  swap(&x, &y);
  printf("Before swaping : x= %d and y=%d\n",x,y);
 }
 void swap(int *a, int *y)
 {
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
  printf("After swaping : x= %d and y=%d\n",x,y);
  getch();
}

Output
Enter any two number : 30 20
Before Swaping : x=30 and y=20
After swaping : x=20 and y=30

Swap two numbers using Bitwise XOR



Source Code
#include< stdio.h>
#include< conio.h>
int main()
{
  int x, y;
  clrscr();
  printf("Enter any two number : ");
  scanf("%d%d",&x,&y);
  printf("Before swaping : x= %d and y=%d\n",x,y);
  x = x ^ y;
  y = x ^ y;
  x = x ^ y;
  printf("After swaping : x= %d and y=%d\n",x,y);
  getch();
}

Output
Enter any two number : 4 5
Before Swaping : x=4 and y=5
After swaping : x=5 and y=4

Saturday, 22 March 2014

Algorithm For Quick Sort and Implement Quick Sort In C language

Quick sort is a very efficient sorting algorithm invented by C.A.R. Hoare. It has two phases:
  • the partition phase and
  • the sort phase





Quick sort
is the fastest internal sorting algorithm with the time complexity O (n log n). The basic algorithm to sort an array a[ ] of n elements can be described recursively as follows:

 Algorithm for Quick Sort


1. If n < = 1, then return.

2. Pick any element V in a[]. This is called the pivot.

3. Rearrange elements of the array by moving all elements xi > V right of V and all elements x­i < = V left of V. If the place of the V after re-arrangement is j, all elements with value less than V, appear in a[0], a[1] . . . . a[j - 1] and all those with value greater than V appear in a[j + 1] . . . . a[n - 1].

4. Apply quick sort recursively to a[0] . . . . a[j - 1] and to a[j + 1] . . . . a[n - 1].

                                                            

Visualization of the quicksort algorithm. The horizontal lines are pivot values.

Source Code
 #include <stdio.h>

void quick_sort(int[],int,int);
int partition(int[],int,int);

void main()
{
    int a[50],n,i;
    printf("How many elements?");
    scanf("%d",&n);
    printf("\nEnter array elements:");

    for(i=0;i<n;i++)
        scanf("%d",&a[i]);

    quick_sort(a,0,n-1);
    printf("\nArray after sorting:");

    for(i=0;i<n;i++)
        printf("%d ",a[i]);
}

void quick_sort(int a[],int l,int u)
{
    int j;
    if(l<u)
    {
        j=partition(a,l,u);
        quick_sort(a,l,j-1);
        quick_sort(a,j+1,u);
    }
}

int partition(int a[],int l,int u)
{
    int v,i,j,temp;
    v=a[l];
    i=l;
    j=u+1;

    do
    {
        do
            i++;
        while(a[i]<v&&i<=u);

        do
            j--;
        while(v<a[j]);

        if(i<j)
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }while(i<j);

    a[l]=a[j];
    a[j]=v;
    return(j);
}



Output

Thursday, 20 March 2014

Simple Snake Game In C Language













Simple snake game written in C/SDL under linux, with Code::Blocks. The code can be compiled under windows without any problem. click "U" to speed up click "L" to speed down.

Source Code

 //**************************************
// Name: Simple Snake Game
// Description:Simple snake game written in C/SDL under linux, with Code::Blocks.
The code can be compiled under windows without any problem.
click "U" to speed up
click "L" to speed down
// By: Goundy
//

#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <time.h>
#define N 500 /*snake max length*/
#define NODESIZE 10 /*node height & width*/
#define WINSIZE 400 /*window height & width*/
/********************************* types **************************************/
typedef enum {Up, Down, Right, Left}TDirection; /*possible direction type*/
/******************************************************************************/
/********************************* ecran **************************************/
SDL_Surface* screen = NULL; /*main screen*/
SDL_Surface* Table[N] = {NULL}; /*snake node table*/
SDL_Surface* blank; /*background (will be totaly wait)*/
SDL_Surface* puce; /*snake puce*/
/******************************************************************************/
/********************************* positions **********************************/
SDL_RectTabpos[N], /*snake node positions*/
ppuce; /*puce positions*/
/******************************************************************************/
/*********************************** other ***********************************/
int NodeCounter; /*snake node counter*/
TDirection direction; /*current direction*/
SDL_TimerID timer; /*timer descriptor*/
/******************************************************************************/
/************************* functions Prototypes ***************************/
Uint32 Anime_it (Uint32, void *);
int snakeColl(void);
void Quitter(void); // will be executed when exiting the game
void SetPos(void);
void MoveSnake (void);
void Show (void);
void PurgeScreen (void);
void SetPuce(void);
void Lose (void);
void freeNodes (void);
void AddNode(void); 


                               Download Full Source Code:   Click Here


Output


Also Read: Program To Find Whether A Number Is Prime Or Not
Also Read : Program to find factorial of Number in C++

Tuesday, 25 February 2014

Program to calculate to average of three number
















Source Code

/*C program to find the average of three numbers*/

#include<stdio.h>
int main()
{

float a,b,c,av=0;
 
printf("Enter any three numbers to find their average \n");//Enter 3 number
scanf("%f%f%f",&a,&b,&c);
 
av=(a+b+c)/3.0; //calculate average
 
printf("\n Average of three numbers is \t %f",av);//result store in variable av
return 0;
}

Output


Program to calculate area of a triangle





Formula of
Area of Triangle:


Area = ( s (s-a) (s-b) (s-c) ) ^ (1/2)
where s = (a+b+c) / 2
a , b and c are the Sides of Triangle.

Statement of C Program: Write a Program to Find the Area of a Triange , given the Three Sides of Triangle:
Source Code

#include<stdio.h>

#include<math.h>

int main()

{

float a,b,c,s=0,area=0;

printf("Enter the length of sides of triangle \n");

scanf("%f %f %f",&a,&b,&c);

s = (a+b+c)/2.0; /* s is semi-perimeter */

area = (sqrt)(s*(s-a)*(s-b)*(s-c));

printf("Area of triangle =\t %f",area);

return 0;

}

Output


Program to add two numbers






In this program, user is asked to enter two integers and this program will add these two integers and display it.

Source Code

#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter first value="); // 1st number
scanf("%d", &a);
printf("Enter second value=n"); // 2nd number
scanf("%d", &b);
c=a+b; // Add
printf("sum=%d", c); // result store in c variable
return 0;
}

Output


Program to find whether a number is even or odd

This Program checks whether the entered number is even or odd...
Any integer is input through the keyboard.
Source Code
#include<stdio.h>
main()
{

int num;

printf ("Enter a number to be checked for even/odd: ");
scanf ("%d",&num);

if (num%2==0)
{
printf ("The entered number is EVEN.\n");

}
else
{
printf ("The entered number is ODD.\n");

}
}


Output


Made your first program in C ( Print "Hello World" )



After talking about the history and compilers it is time to make our first program. We like to make C programs under Linux, so we will use a text editor and the gnu compiler. But all programs we make in these tutorials will also work under Windows(Complier Codeblock). Remember, the examples included in the C and C++ tutorials are all console programs. That means they use text to communicate. All compilers support the compilation of console programs. Check the user’s manual of your compiler for more info on how to compile them. (It is not doable for us to write this down for every compiler).

Open a text editor (vi, emacs, notepad) or make a console project in Visual Studio Express and type the following lines of code (Don’t use cut/paste, type them. This is better for learning purposes):

#include<stdio.h>
int main()
{

printf(“Hello World\n”);
return 0;
 }


With this line of code we include a file called stdio.h. (Standard Input/Output header file). This file lets us use certain commands for input or output which we can use in our program.  For instance it has commands for input like reading from the keyboard and output commands like printing things on the screen.

int main()


The int is what is called the return value (in this case of the type integer). Where it used for will be explained further down. Every program must have a main(). It is the starting point of every program. The round brackets are there for a reason, in a later tutorial it will be explained, but for now it is enough to know that they have to be there.

{}


The two curly brackets (one in the beginning and one at the end) are used to group all commands together. In this case all the commands between the two curly brackets belong to main(). The curly brackets are often used in the C language to group commands together. (To mark the beginning and end of a group or function.).

printf(“Hello World\n”);


The printf is used for printing things on the screen, in this case the words: Hello World. As you can see the data that is to be printed is put inside round brackets. The words Hello World are inside inverted ommas, because they are what is called a string. (A single letter is called a character and a series of characters is called a string). Strings must always be put between inverted commas. The \n is called an escape sequence. In this case it represents a newline character. After printing something to the screen you usually want to print something on the next line. If there is no \n then a next printf command will print the string on the same line.

Commonly used escape sequences are:


    \n (newline)
    \t (tab)
    \v (vertical tab)
    \f (new page)
    \b (backspace)
    \r (carriage return)

After the last round bracket there must be a semi-colon. The semi-colon shows that it is the end of the command. (So in the future, don’t forget to put a semi-colon if a command ended).
return 0;

When we wrote the first line “int main()”, we declared that main must return an integer int main(). (int is short for integer which is another word for number). With the command return 0; we can return the value null to the operating system. When you return with a zero you tell the operating system that there were no errors while running the program.
Compile

If you have typed everything correct there should be no problem to compile your program. After compilation you now should have a hello.exe (Windows) or hello binary (UNIX/Linux). You can now run this program by typing hello.exe (Windows) or ./hello (UNIX/Linux).

If everything was done correct, you should now see the words “Hello World” printed on the screen.

Congratulations!!!!!
You have just made your first program in C.

Comments in your program


The Hello World program is a small program that is easy to understand. But a program can contain thousands of lines of code and can be so complex that it is hard for us to understand. To make our lives easier it is possible to write an explanation or comment in a program. This makes it much easier to understand the code. (Even if you did not look at the code for years).These comments will be ignored by the compiler at compilation time.

Comments have to be put after // or be placed between /* */ .
Here is an example of how to comment the Hello World source code :

/*	Description	: First Program in C language
		Author		: Your Name
		Date		: 02/07/2013				*/
 

#include<stdio.h>

 int main()
{
//Print something that you want
printf(“Hello World\n”); // Entering in the world of C language
return 0;
}

Contact Us

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

Name Email * Message *