Wednesday, 22 October 2014

Use pow and sqrt functions in C programming

In programming some time we need to use some mathematical operations. In math.h header defines various mathematical functions. Some important of them are pow and sqrt.. function of pow and sqrt is already defined in math.h header file. Let see how to use them
POW
Function pow is used for find out power of any integer. For example if we need to find out 25 then we use it.
Now let’s write the code
#include<stdio.h>
#include<math.h>
void main()
{
    float x,y,z;
    printf("enter the value");
    scanf("%f",&x);
    printf("enter the power");
    scanf("%f",&y);
    z=pow(x,y);
    printf("the answer is: %.2f",z);
}
The output of above program is
enter the value 2
enter the power 5
the answer is: 32.00
SQRT
Function sqrt used when we try to find out square root of any integer. For example if we need to find out √3 then we use it.
Now let’s write the code
#include<stdio.h>
#include<math.h>
void main()
{
    float x, z;
    printf("enter the value");
    scanf("%f",&x);
    z=sqrt(x);
    printf("the answer is: %.2f",z);
}
The output of above program is
enter the value 3
the answer is: 1.73

Friday, 17 October 2014

Find Factorial of a number using C programming

Find Factorial of a number using C programming
In mathematics, the factorial of a non-negative integer n, denoted by n! is the product of all positive integers less than or equal to n. For example,
    5! = 5 X 4 X 3 X 2 X 1 = 120.
Also factorial of 0! Is 1.
We are going to find out factorial by using C programming. Let’s see the code
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j,k=1;
    printf("Enter your number");
    scanf("%d",&i);
    for(j=1;j<=i;j++)
    {
        k=k*j;
    }
    printf("Factorial of number %d is %d",i,k);
}
In above program we use for loop to calculate the factorial. When we insert any value in this loop that value will be multiplied by all other integer value less than inserted value until loop ends.
Now let’s see the output
Enter your number 5
Factorial of number 5 is 120

Thursday, 16 October 2014

Compute nCr using C++ programming

nCr is an important function for mathematical operations. It’s mainly used in Binomial Distribution or Permutation and Combinations or Statistic Binomial Distribution. Now let’s see the mathematical expression for nCr

For example if we write 5C2 then this expression becomes

So, in our program we are going to calculate nCr. Now let’s write the code
#include<iostream>
using namespace std;
int fact(int );
main()
{
    int n,r;
    float ncr,a,b;
    cout<<"Insert the value of n";
    cin>>n;
    cout<<"Insert the value of r";
    cin>>r;
    if(r<n)
    {
    a=fact(n);
    b=fact(n-r)*fact(r);
    ncr=a/b;
    cout<<"Answer is: "<<ncr;
    }
    else
    {
        cout<<"r must be less then n";
    }
}
int fact(int i)
{   int m=1,j;
    for(j=1;j<=i;j++)
    {
        m=m*j;
    }
    return(m);
}
Now let’s see the output of this program
Insert the value of n 5
Insert the value of r 2
Answer is: 10

Saturday, 11 October 2014

Differences between C and C++


C and C++ are very important programming languages. As C is older than C++ there some difference between C and C++ language. They are:
1. C is Procedural Language but C++ is  multi-paradigm language i.e. procedural as well as object oriented.

2. The concept of virtual Functions are used in C++ but No virtual Functions are present in C.

3. In C Top down approach is used in Program Design where in C++ Bottom up approach adopted in Program Design.

4. No namespace Feature is present in C Language as it is not needed but Namespace Feature is present in C++ for avoiding Name collision example "using namespace std;".

5. Multiple Declaration of global variables are alloed in C but Multiple Declaration of global varioables are not allowed in C++.

6. In C scanf() Function used for Input and printf() Function used for output but in C++ Cin>> Function used for Input and Cout<< Function used for output.

7. Polymorphism is not possible in C but in C++ the concept of polymorphism is used.

8. Operator overloading is not possible in C where Operator overloading is a Feature of C++.

9. Mapping between Data and Function is difficult and complicated in C programming Mapping between Data and Function can be used using "Objects" in C++ programming.

10. In C, we can call main() Function through other Functions in that programming but in C++, we cannot call main() Function through other functions.

11. No inheritance is possible in C where Inheritance is possible in C++.

12. In C, malloc() and calloc() Functions are used for Memory Allocation and free() function for memory Deallocating where In C++,  new and delete operators are used for Memory Allocating and Deallocating.

13. In C, Exception Handling is not present where in C++, Exception Handling is done with Try and Catch block.

14. C requires all the variables to be defined at the starting of a scope C++ allows the declaration of variable anywhere in the scope i.e at time of its First use.

15. We can use functions inside structures in C++ programming but not in C programming.

16. We can say C is low level language but C++ is middle-level language.

17. C++ supports Exception Handling while C does not.

18. In case of C, the data is not secured but for C++, the data is secured.

Friday, 10 October 2014

Create a simple calculator using java programming

Creating basic calculator using java is the best way to know some basic arithmetic operations like addition, subtraction, multiplication, division etc.
Let’s write the code
class Calc{
int c;
void add(int a,int b)
{ c=a+b;
System.out.println("Add = "+c);
}
void sub(int a,int b)
{ c=a-b;
System.out.println("Sub = "+c);
}
void div(int a,int b)
{ c=a/b;
System.out.println("Div = "+c);
}
void mul(int a,int b)
{ c=a*b;
System.out.println("Mul = "+c);
}
}
class Cal{
public static void main(String args[])
{
int a=10;
int b=2;
Calc ob=new Calc();
ob.add(a,b);
ob.sub(a,b);
ob.div(a,b);
ob.mul(a,b);
}
}
In above program we are we use two classes. In main class we take two integer “a” and “b” then complete addition, subtraction, division, multiplication operation on it.
Now let’s see output
Add = 12
Sub = 8
Div = 5
Mul = 20