Navigation Menu

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

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.