Navigation Menu

Currently the navigation bar is not functioning. Use the Blog Archive or the Google Custom Search to find content.
Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Friday, 24 October 2014

C program to convert binary number to decimal

Given below is a c program to convert binary number to decimal.

Example :

1111 - Binary Number
1        1        1        1  
23 22 21 20
8 4 2 1
23x1 + 22x1 + 21x1 + 20 = 8+4+2+1 = 15

101 - Binary Number
1        0        1  
22 21 20
4 2 1
22x1 + 21x0 + 20 = 4+0+1 = 5

Links that you may find important :

http://www.wikihow.com/Convert-from-Binary-to-Decimal
http://www.binaryhexconverter.com/binary-to-decimal-converter

Code :
#include <stdio.h>
#include <conio.h>
void main()
{    
    long int n,i=1,d=0,m=0,l=0;
    clrscr();
    printf("Enter binary number : ");
    scanf("%ld",&n);
    while(n>0)
    {
        m=(n%10);
        n=(n/10);
        l=m*i;
        d=d+l;
        i=i*2;
    }
    printf("Decimal equivalent is : %ld",d);
    getch();
}
Output :
Enter binary number : 1111
Decimal equivalent is : 15
Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Thursday, 26 September 2013

C program to exchange the values of two variables with and without temporary variable

Given below is a c program to exchange the values of two variables with and without temporary variable.

Concept (using temporary variable):

Use a temporary variable(temp) to store the value of a variable(a) and then put the contents of another variable(b) into variable(a) and then put the value of variable(temp) into variable(b).

temp=a;
a=b;
b=temp;

Concept (without using temporary variable):

a=a-b;
b=a+b;
a=b-a

Code :
#include <conio.h>
#include <stdio.h>
main()
{
    int a=10,b=20,x;
    clrscr();
    printf("Before swapping\t");
    printf("a=%d\tb=%d",a,b);
    printf("\nAfter swapping\t");
    x=a;
    a=b;
    b=x;
    printf("a=%d\tb=%d",a,b);
    printf("\nBefore swapping\t");
    printf("a=%d\tb=%d",a,b);
    a=a-b;
    b=a+b;
    a=b-a;
    printf("\nAfter swapping\t");
    printf("a=%d\tb=%d",a,b);
    getch();
}
Output :
Before swapping    a=10    b=20
After swapping     a=20    b=10
Before swapping    a=10    b=20
After swapping     a=20    b=10
Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Thursday, 20 June 2013

Character generation in C using Bitmap method

Given below is a C program for character generation using Bitmap method.

Links that you may find important :

Computer Graphics And Multimedia By A.P.Godse, D.A.Godse

Code :
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main()
{
    int i,j,k,x,y;
    int gd=DETECT,gm;//DETECT is macro defined in graphics.h
    /* ch1 ch2 ch3 ch4 are character arrays that display alphabets */
    int ch1[][10]={ {1,1,1,1,1,1,1,1,1,1},
                    {1,1,1,1,1,1,1,1,1,1},
                    {0,0,0,0,1,1,0,0,0,0},
                    {0,0,0,0,1,1,0,0,0,0},
                    {0,0,0,0,1,1,0,0,0,0},
                    {0,0,0,0,1,1,0,0,0,0},
                    {0,0,0,0,1,1,0,0,0,0},
                    {0,1,1,0,1,1,0,0,0,0},
                    {0,1,1,0,1,1,0,0,0,0},
                    {0,0,1,1,1,0,0,0,0,0}};
    int ch2[][10]={ {0,0,0,1,1,1,1,0,0,0},
                    {0,0,1,1,1,1,1,1,0,0},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {0,0,1,1,1,1,1,1,0,0},
                    {0,0,0,1,1,1,1,0,0,0}};
    int ch3[][10]={ {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,1,1,1,1,1,1,1,1},
                    {1,1,1,1,1,1,1,1,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1}};
    int ch4[][10]={ {1,1,0,0,0,0,0,0,1,1},
                    {1,1,1,1,0,0,0,0,1,1},
                    {1,1,0,1,1,0,0,0,1,1},
                    {1,1,0,1,1,0,0,0,1,1},
                    {1,1,0,0,1,1,0,0,1,1},
                    {1,1,0,0,1,1,0,0,1,1},
                    {1,1,0,0,0,1,1,0,1,1},
                    {1,1,0,0,0,1,1,0,1,1},
                    {1,1,0,0,0,0,1,1,1,1},
                    {1,1,0,0,0,0,0,0,1,1}};
    initgraph(&gd,&gm,"D:\\TC\\BGI");//initialize graphic mode
    setbkcolor(LIGHTGRAY);//set color of background to darkgray
    for(k=0;k<4;k++)
    { 
        for(i=0;i<10;i++)
        {
            for(j=0;j<10;j++)
            {
                if(k==0)
                { 
                    if(ch1[i][j]==1)
                    putpixel(j+250,i+230,RED);
                }
                if(k==1)
                { 
                    if(ch2[i][j]==1)
                    putpixel(j+300,i+230,RED);
                }
                if(k==2)
                { 
                    if(ch3[i][j]==1)
                    putpixel(j+350,i+230,RED);
                }
                if(k==3)
                { 
                    if(ch4[i][j]==1)
                    putpixel(j+400,i+230,RED);
                }
            }
            delay(200);
        }
    }
    getch();
    closegraph();
}
Output :


Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Wednesday, 19 June 2013

C program to convert temperature from Celsius to Fahrenheit

Given below is a C program to convert temperature from Celsius to Fahrenheit.

Formula to convert Celsius to Fahrenheit :

Multiply by 9, then divide by 5, then add 32

°C x 9/5 + 32 = °F

Formula to convert Fahrenheit to Celsius :

Sunday, 16 June 2013

C Program for Bresenham’s Line Drawing Algorithm

Given below is a C program to draw a line using Bresenham’s Line Drawing Algorithm.

Code :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void draw(int x1,int y1,int x2,int y2);
void main() 
{
    int x1,x2,y1,y2;
    int gd=DETECT,gm;//DETECT is macro defined in graphics.h
    initgraph(&gd,&gm,"d:\\tc\\bgi");//initialize graphic mode
    printf("Enter values of x1 and y1: ");
    scanf("%d %d",&x1,&y1);
    printf("Enter values of x2 and y2 : ");
    scanf("%d %d",&x2,&y2);
    draw(x1,y1,x2,y2);//call to function that draws the line
    getch();
}
void draw(int x1,int y1,int x2,int y2)
{
    float dx,dy,p;
    int i,x,y,xend;
    dx=x2-x1;
    dy=y2-y1;
    p=2*dy-dx;
    if(x1>x2)
    {
        x-x2;
        y=y2;
        xend=x1;
    }
    else
    {
        x=x1;
        y=y1;
        xend=x2;
    }
    putpixel(x,y,10);
    while(x<xend)
    {
        if(p<0)
        {
            x=x+1;
            putpixel(x,y,10);
            p=p+(2*dy);
        }
        else
        {
            x=x+1;
            y=y+1;
            putpixel(x,y,10);
            p=p+(2*dy)-(2*dx);
        }
    }
}
Output :


Note : In the above screenshot the line is hardly visible. The image above is a grayscale of original image.

Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Saturday, 15 June 2013

C Program for DDA (graphics algorithm)

Given below is a C program to draw a line using DDA algorithm.

Code :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int sign(int nd)
{
    if(nd>0)
        return +1;
    else if(nd<0)
        return -1;
    else
        return 0;
}
void main()
{
    int gd=DETECT,gm,i=1,j;//DETECT is macro defined in graphics.h
    int x1,x2,y1,y2,length;
    float dx,dy,ndx,ndy,x,y;
    initgraph(&gd,&gm,"d:\\tc\\bgi");//initialize graphic mode
    setbkcolor(DARKGRAY);//set the background color
    printf("Enter x1:");
    scanf("%d",&x1);
    printf("Enter y1:");
    scanf("%d",&y1);
    printf("Enter x2:");
    scanf("%d",&x2);
    printf("Enter y2:");
    scanf("%d",&y2);
    dx=x2-x1;
    dy=y2-y1;
    if(abs(dx)>=abs(dy))
        length=abs(dx);
    else
        length=abs(dy);
    ndx=dx/length;
    ndy=dy/length;
    x=x1+(0.5*sign(ndx));
    y=y1+(0.5*sign(ndy));
    while(i<=length)
    {
        putpixel(x,y,4);
        x=x+ndx;
        y=y+ndy;
        i=i+1;
    }
    getch();
    closegraph();
}
Output :


Note : In the above screenshot the line is hardly visible. The image above is a grayscale of original image.

Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Thursday, 13 June 2013

C program to implement Insertion sort

Given below is a C program to implement Insertion sort.
What is Insertion sort? Click here to read about it.
In Insertion sort the element is positioned at its proper place by inserting it and shifting the other elements to the right. The elements are sorted in increasing order of their position.

Example :
75 35 47 15

Pass 0 : 75 35 47 15
Pass 1 : 35 75 47 15
Pass 2 : 35 47 75 15
Pass 3 : 15 35 47 75

Links that you may find important :

http://www.sorting-algorithms.com/insertion-sort
http://www.algolist.net/Algorithms/Sorting/Insertion_sort
https://www.youtube.com/watch?v=c4BRHC7kTaQ
https://www.youtube.com/watch?v=Kg4bqzAqRBM

Code :
#include <stdio.h>
#include <conio.h>
void insert(int n,int a[])
{
    int i,j,k; 
    for(i=1;i<=n;i++)
    {
        int temp=a[i];
        printf("\nPass %d: ",i);
        for(k=0;k<n;k++)
        {
            printf("%d\t",a[k]);
        }
        for(j=i-1;j>=0;j--)
        {
            if(temp<a[j])
            { 
                a[j+1]=a[j];
                a[j]=temp;
            }
        }
    }
}
void main()
{
    int i,n,a[10];
    clrscr();
    printf("Enter no. of elements : ");
    scanf("%d",&n);
    printf("Enter the elements in array : \n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    insert(n,a);//call to function that performs the sorting
    printf("\nElements after sorting\n");
    for(i=0;i<n;i++)
    {
        printf("%d\t",a[i]);
    }
    getch();
}
Output :
Enter no. of elements : 4
Enter the elements in array :
75
35
47
15

Pass 1: 75      35      47      15
Pass 2: 35      75      47      15
Pass 3: 35      47      75      15
Pass 4: 15      35      47      75
Elements after sorting
15      35      47      75
Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Monday, 10 June 2013

Draw a smiley in C

C program to draw a smiley in C using graphics.

Code :
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main()
{
    int gd=DETECT,gm;//DETECT is macro defined in graphics.h
    int color,pixel,maxx,maxy;
    initgraph(&gd,&gm,"C:\\TC\\BGI");//initialize graphic mode
    setbkcolor(DARKGRAY);//set the background color
    maxx=getmaxx();//get maximum value for x co-ordinate
    maxy=getmaxy();//get maximum value for y co-ordinate
    setcolor(YELLOW);//color for drawing shapes
    circle(maxx/2,maxy/2,20);//draw a circle
    setfillstyle(1,YELLOW);//the style to fill the area
    fillellipse(maxx/2,maxy/2,100,100);//fill the ellipse with color(face)
    pixel=getpixel(1,1);
    setfillstyle(1,pixel);
    setcolor(pixel);
    fillellipse(maxx/2-50,maxy/2-30,10,10);//fill the ellipse with color(eye)
    fillellipse(maxx/2+50,maxy/2-30,10,10);//fill the ellipse with color(eye)
    ellipse(maxx/2,maxy/2,220,320,60,60);//draw an ellipse(mouth)
    line(maxx/2,maxy/2-10,maxx/2,maxy/2+20);//draw a line(nose)
    getch();
    closegraph();//close graphic mode
}
Output :


Note : The image above is a graysace of the original image.

Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Sunday, 9 June 2013

C program to implement Bubble sort

C program to implement Bubble sort.
What is Bubble sort? Click here to read about it.
Bubble sort can be used to sort the items in ascending or descending order. It involves iterating through the list and swapping the adjacent elements until the list is sorted that is no swaps are needed.

Example :
45 36 28 10 5

Pass 0 :  45  36  28  10  5
Pass 1 :  36  28  10  5   45
Pass 2 :  28  10  5   36  45
Pass 3 :  10  5   28  36  45
Pass 4 :  5   10  28  36  45
Links that you may find important :

http://www.sorting-algorithms.com/bubble-sort
http://www.algolist.net/Algorithms/Sorting/Bubble_sort
http://www.algorithmist.com/index.php/Bubble_sort
http://www.youtube.com/watch?v=tT4bJB0J4H4
http://www.youtube.com/watch?v=NiyEqLZmngY

Code :
#include <stdio.h>
#include <conio.h>
void bubble(int n,int a[])
{ 
    int temp,i,j,k;
    for(i=0;i<n;i++)
    {
        printf("\nPass %d: ",i);
        for(k=0;k<n;k++)
        {  
            printf("%d\t",a[k]);
        }
        for(j=0;j<n;j++)
        {
            if(a[j]>a[j+1])
            {   
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
}
void main()
{
    int i,n,a[10];
    clrscr();
    printf("Enter no. of elements : ");
    scanf("%d",&n);
    printf("Enter the elements in array : \n");
    for(i=0;i<n;i++)
    { 
        scanf("%d",&a[i]);
    } 
    bubble(n,a);//call to the function which sorts elements
    printf("\nElements after sorting\n");
    for(i=0;i<n;i++)
    {
        printf("%d\t",a[i]);
    }
    getch();
}
Output :
Enter no. of elements : 5
Enter the elements in array :
45
36
28
10
5

Pass 0: 45      36      28      10      5
Pass 1: 36      28      10      5       45
Pass 2: 28      10      5       36      45
Pass 3: 10      5       28      36      45
Pass 4: 5       10      28      36      45
Elements after sorting
5       10      28      36      45
Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Saturday, 8 June 2013

C program to find Simple and Compound interest

C program to find Simple and compound interest.
What is Simple interest? Click here to read about it.
What is Compound interest? Click here to read about it.
Simple interest Formula : ( P x N x R )/100
Compound interest Formula : P ( 1 + R / 100)N
Where
P : The principal amount.
N : Number of years.
R : Rate of interest.

Links that you may find important :

http://math.about.com/od/businessmath/ss/Interest.htm
http://math.about.com/od/formulas/a/compound.htm  

Code :
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
        float p,si,ci,r;
        int n;
        clrscr();
        printf("Program to find Interest");
        printf("\n\nPrinciple amount :");
        scanf("%f",&p);
        printf("\nNumber of years :");
        scanf("%d",&n);
        printf("\nRate of interest:");
        scanf("%f",&r);
        si=p*n*r/100; //Simple interest calculation
        printf("\nSimple Interest = %1.2f",si);
        ci=p*pow(1+r/100,n); //Compound interest calculation
        printf("\nCompound Interest = %1.2f",ci);
        getch();
}
Output :
Program to find Interest

Principle amount :5000

Number of years :10

Rate of interest:7.5

Simple Interest = 3750.00
Compound Interest = 10305.16
Note : Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Sunday, 2 June 2013

Having problems while running C or C++ program ?

Many a time beginners are faced with the problem while running a C or a C++ program. The common being not able to compile and run it. The error's like

Unable to open include file 'IOSTREAM.H'
Unable to open include file 'CONIO.H'
Unable to open include file 'STDIO.H'

It seems to give an error for every file included using #include. Its common to have such errors since people usually place the TurboC or TC or TurboCPP folder according to the location they prefer in the partition.

Method 1 :

Placing it in the partition where Windows is loaded could solve this problem.
Locate the folder TC or TCPP or TC++ or TurboC
the above names may differ according to the source you have 
acquired it from
Copy the folder
Go to the partition where you have installed your Windows 
operating system
Open the partition
Paste it 
After installation the path should be like [drive :]\TC . Where drive is the letter of your partition which in most of the cases is C. You can use the below method if still you aren't able to run the program.

Method 2 :

Alternatively if you dont want to place the folder in windows partition you can follow the steps below to solve the problem.
Locate the TurboC or TCPP folder
again names vary according to sources you have acquired it from
Open the bin folder
Click TC.EXE
In the IDE or the window you are working in locate the menu bar 
at the top
Click on Options in the Menu bar
Click on Directories
Now besides Include Directories type the whole path to where you 
TC is located. For example
[drive : ]\path\tc\include
Now do the same for the text field located besides 
Library Directories. For example
[drive : ]\path\tc\lib
For Source and Output directories do the same. For example
[drive : ]\path\tc
Note : By default the path specified are relative paths and hence dont work in partition's other than Windows. When you place your TCPP or TurboC folder in location other than Windows partition you have to explicitly mention the path to directories.

Note : If you feel something is incorrect or missing leave a comment below.