Navigation Menu

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

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.

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.