Navigation Menu

Currently the navigation bar is not functioning. Use the Blog Archive or the Google Custom Search to find content.

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.

Wednesday 3 July 2013

C++ program to calculate m to the power n

C++ Program to calculate m to the power n(m raise to n).

Example :

22 = 4 which is same as 2 x 2 = 4
44 = 256 which is same as 4 x 4 x 4 x 4 = 256

Links that you may find important :

https://en.wikipedia.org/wiki/Nth_root
http://www.mathsisfun.com/numbers/nth-root.html
http://www.rkm.com.au/calculators/CALCULATOR-powers.html
http://www.free-online-calculator-use.com/exponent-calculator.html

Code :
#include <iostream.h>
#include <conio.h>
int power(int,int);
void main()
{
    int no,pow,val;
    clrscr();
    cout<<"Enter number : ";
    cin>>no;
    cout<<"Enter the power : ";
    cin>>pow;
    val=power(no,pow);//called function power which returns integer
    cout<<"The answer is : "<<val;
    getch();
}
int power(int no, int pow)
{
    int val;
    val=no;
    while(pow>=1)
    {
        if(pow==1)
            return no;
        if(pow>1)
            val=val*no;
        pow--;
    }
}
Output :
Enter number : 2
Enter the power : 8
The answer is : 256
Note : Since the type of number is integer the range of number which it can calculate is restricted.

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.

Monday 1 July 2013

Java program based on static members

Program to count the number of objects created for a class using static member.

Code :
class Obj{
    static int count;
    Obj(){
        count++;
        System.out.print("\nObject  "+count+" created...");
    }
    static void showCount(){
        System.out.print("\nTotal Object Count  :  "+count);
    }
}
class ObjCount{
    public static void main(String args[]){
        Obj o1=new Obj();
        Obj o2=new Obj();
        Obj.showCount();
        Obj o3=new Obj();
        Obj.showCount();
    }  
}
Output :
Object  1 created...
Object  2 created...
Total Object Count  :  2
Object  3 created...
Total Object Count  :  3
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 30 June 2013

Java program to sort a given array

Java program to sort a given array.

Code :
class Array{
    int a[]={9,7,4,-1,0};
    voidsortArray(){
    int i,j,temp;
    for(i=0;i<a.length;i++){
        for(j=0;j<a.length-1;j++){
            if(a[j]>a[j+1]){
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
    }
    voidshowArray(){
        for(inti=0;i<a.length;i++){
            System.out.print(" a["+i+"] : "+a[i]+"\t");
        }
    }
}
class Sort{
    public static void main(String args[]){
        Array t=new Array();
        System.out.print("\n Array Elements : \n ");
        t.showArray();
        System.out.print("\n Sorted Array Elements : \n ");
        t.sortArray();
        t.showArray();
    }
}
Output :
Array Elements :
  a[0] : 9       a[1] : 7        a[2] : 4        a[3] : -1       a[4] : 0

 Sorted Array Elements :
  a[0] : -1      a[1] : 0        a[2] : 4        a[3] : 7        a[4] : 9
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.

Saturday 29 June 2013

Java program based on constructor

Program to keep record of height and weight of people.

Code :
import java.io.*;
class Person{
    float height;
    float weight;
    Person(float h,float w){
        height=h;
        weight=w;
    }
    void putPersonData(){
        System.out.print("\nHeight : "+height+" Weight : "+weight);
    }
};
class PersonCount{
    static int count;
    static Person p[]=new Person[10];
    static void showCount(){
        for(int i=0;i<10;i++){
            p[i].putPersonData();
            if(p[i].height>170 && p[i].weight<50){
                count=count+1;            
            }
        }
        System.out.print("\n\nTotal Count (weight<50kg AND height>170cm ) : "+count+"\n");
    }
    public static void main(String args[]){
        p[0]=new Person(173,40);
        p[1]=new Person(170,30);
        p[2]=new Person(168,55); 
        p[3]=new Person(172,55);
        p[4]=new Person(169,55);
        p[5]=new Person(175,70);
        p[6]=new Person(171,45);
        p[7]=new Person(172,55);
        p[8]=new Person(168,55);
        p[9]=new Person(168,65);
        showCount();
    }
}
Output :
Height : 173.0 Weight : 40.0
Height : 170.0 Weight : 30.0
Height : 168.0 Weight : 55.0
Height : 172.0 Weight : 55.0
Height : 169.0 Weight : 55.0
Height : 175.0 Weight : 70.0
Height : 171.0 Weight : 45.0
Height : 172.0 Weight : 55.0
Height : 168.0 Weight : 55.0
Height : 168.0 Weight : 65.0

Total Count (weight<50kg AND height>170cm ) : 2


Program to maintain score card of students.

Code :
import java.io.*;
class Student{
    String name;
    Student(String nm){
        name=nm;
    }
    void putStudentName(){
        System.out.print("\n Student Name : "+name);
    }
};
class Marks{
    Student s;
    int mark1,mark2;
    Marks(String name,int m1,int m2){
        s=new Student(name);
        mark1=m1;
        mark2=m2;
    }
    void putmarks(){
        s.putStudentName();
        System.out.print("\nMark 1 : "+mark1);
        System.out.print("\nMark 2 : "+mark2);
        putAverage();  
    } 
    void putAverage(){
        float t=(mark1+mark2)/(float)2;
        System.out.print("\nAverage : "+t);
    } 
};
class Result{
    public static void main(String args[]){
        Marks m=new Marks("Maverick",80,85);
        m.putmarks();
    }
}
Output :
Student Name : Maverick
Mark 1 : 80
Mark 2 : 85
Average : 82.5
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.

Wednesday 26 June 2013

C++ program to print a checkboard (8-by-8 grid)

Print a checkboard (8-by-8 grid). Each square should be 5-by-2 characters wide. A 2-by-2 example follows:
+-----+-----+
|     |     |
|     |     |
+-----+-----+
|     |     |
|     |     |
+-----+-----+
Code :
#include <iostream.h>
#include <conio.h>
void main()
{
    int i,j,k,l;
    clrscr();
    for(i=0;i<9;i++)
    {
        for(j=0;j<8;j++)
        {
            cout<<"+";
            for(k=0;k<5;k++)
            {
                cout<<"-";
            }
        }
        cout<<"+"<<endl;
        if(i<8)
        {
            for(l=0;l<2;l++)
            {
                for(j=0;j<9;j++)
                {
                    for(k=0;k<2;k++)
                    {
                        if(k%5==0)
                            cout<<"|     ";
                    }
                }
                cout<<endl;
            }
        }
    }
    getch();
}
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.

Monday 24 June 2013

C++ program to find a binary equivalent of a number using recursion

Given below is a C++ Program to find a binary equivalent of a number using recursion.

Example :

2 / 14 = 7 Quotient | 0 Remainder
2 / 7 = 3 Quotient | 1 Remainder
2 / 3 = 1 Quotient | 1 Remainder
2 / 1 = 0 Quotient | 1 Remainder

Now take all the Remainder in reverse order. Now we have 14 = 11102.

Saturday 22 June 2013

Shell scripting using general-purpose utilities - Menu driven program

Given below is a menu driven shell script which will print the following menu and execute the given task to display result on standard output.
MENU
1 Display calendar of current month
2 Display today’s date and time
3 Display usernames those are currently logged in the system
4 Display current directory
5 Display your terminal number
6 Exit

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 :

Tuesday 18 June 2013

Java program to swap two numbers

Given below is a Java program to swap two numbers.

Code :
// Class Swap
class Swap{
    // method swap 
    void swap(int num1,int num2){
        num1 = num1 + num2;
        num2 = num1 - num2;
        num1 = num1 - num2;
        System.out.println("After swapping\nNumber 1= " + num1 + " and Number 2= "+ num2);
    }
}
// Class SwapDemo 
class SwapDemo{
    public static void main(String[] args) {
        int num1=10,num2=20;
        System.out.println("Number 1: "+num1);
        System.out.println("Number 2: "+num2);
        Swap s=new Swap();
        s.swap(num1,num2);//call to the swap method
    }
}
Output :
Number 1: 10
Number 2: 20
After swapping
Number 1= 20 and Number 2= 10
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 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.

Friday 14 June 2013

Shell script to display prime numbers

Bash script to display prime numbers.

Links that you may find important :

https://en.wikipedia.org/wiki/Prime_number
http://en.wikipedia.org/wiki/List_of_prime_numbers
http://en.wikipedia.org/wiki/2_%28number%29

Code :
echo 'Enter no'
read x
n=2
while [ $n -le $x ]
do
i=2
count=1

while [ $i -lt $n ]
do
if [ `expr $n % $i` -eq 0 ]
then
count=0
break
fi
i=`expr $i + 1`
done

if [ $count -eq 1 ]
then
echo "$n is Prime"
fi

n=`expr $n + 1`
done
Output :
2 is Prime
3 is Prime
5 is Prime
7 is Prime
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.

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.

Saturday 1 June 2013

Setting path for running Java programs

The path is an environment variable in operating system like MS Windows,Linux. This variable can be set to values as per users convenience. The path environment variable is usually set so that it enables to compile and run Java program directly from any directory without having to set the path again and again.

Steps to set path :

Method 1 : 
Click on start menu
Click on Run (Alternatively you can press Win+R keys)
Type cmd in the run window and press Enter key
In order to compile Java program you need to set path
set path=[drive:]\Program Files\Java\jdkx.x.x_x\bin;
where x.x.x_x is the version
Note : The above method requires you to set the variable for every new instance of command prompt window opened.

Method 2 : 
Right click on My Computer icon on Desktop
Click on Properties
Select the Advanced tab from the System Properties window
(Alternatively you can press Win+Pause/Break keys)
Click on the button named Environment Variables
In the Environment Variables window find variable named PATH under 
System variables and select it
Either double click the selection or click on Edit option below
In the field named Variable value put a semicolon (;) 
at the end of line (to go to end of line press the END key).
Copy paste the whole path to the bin directory in java
[drive:]\Program Files\Java\jdkx.x.x_x\bin
where x.x.x_x is the version
Click Ok in Edit Environment Variable window
Click Ok in Environment Variables window
Click Ok in System Properties window.
Note : If you feel something is wrong or missing leave a comment below. Please report about broken links.

Friday 31 May 2013

Compiling and running Java program

To create a Java program type the code in a file and save it as ClassName.java (where ClassName is the name of the class in java file). If the ClassName doesn't match with the name of the class in ClassName.java file the program wont compile. The class you are compiling should have a main() method since this is where the program begins its execution. A program can have one main() method and any number of classes. You dont need to compile individual classes.

Steps to compile and run :

Method 1 :
Click on start menu
Click on Run (Alternatively you can press Win+R keys)
Type cmd in the run window and press Enter key
In order to compile Java program you need to set path
set path=[drive:]\Program Files\Java\jdkx.x.x_x\bin;
where x.x.x_x is the version
In the command prompt type the path to locate the class file
cd /d [drive:]\path
Then to compile the program type 
javac ClassName.java
where ClassName.java is the file that contains the main() method.
Then to run the program type
java ClassName
Note : Notice in the above we didnt mentioned the .java extension that is because the ClassName.java file doesn't run. Instead the ClassName.class file that is created after compilation is run.

Method 2:
Click on start menu
Click on Run (Alternatively you can press Win+R keys)
Type notepad in the run window and press Enter key
In order to compile Java program you need to set path. 
Type in notepad
set path=[drive:]\Program Files\Java\jdkx.x.x_x\bin;
where x.x.x_x is the version
Then to compile the program type in notepad
javac ClassName.java
where ClassName.java is the file that contains the main() method.
Then to run the program type in notepad
java ClassName
Save the file in the same directory where your .java file exists.
Name it as Filename.bat (where Filename is name of the file for 
example run.bat)
Double click on the .bat file to run.
Note : For programs that require an argument while running you have to specify the parameter/s in the file while saving (for example java ClassName 1 2 3)where 1 2 3 are three parameters that are separated using space.

For Netbeans click on the link below :
https://netbeans.org/kb/docs/java/quickstart.html

Note :If you feel something is incorrect or missing leave a comment below. Please report about broken links.