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.

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.