Showing posts with label mathematical program. Show all posts
Showing posts with label mathematical program. Show all posts

Wednesday, 8 June 2016

Babylonian method of finding square root : C program

There are many algorithms to find square root of the number !
Here is the probably coolest and very easy method to find square root in C .

What this method actually is , guessing a number , squaring it , if the difference between square of a guess number and original number is acceptable than assigning the guess number as square root and if not updating the guess number .

Algorithm :

  1. Given a number say x.
  2. Guess the square root of the number , say guess=x/2.
  3. Square your guessed number and subtract it from original number.
  4. If the absolute difference is less than the acceptable tolerance , say ACCURACY , then you are done .
  5. Else calculate new guess number .
      guess = (guess + (original /guess))/2 .
  6. Go to step 3.

C Program : Finding Square Root 



#include < stdio.h >
#define ACCURACY 0.001 // tolerance : you can set by yourself according to requirement

int main()
{
    float number,guess,square_root;
    printf("Enter a number to find square root of it:\n");
    scanf("%f",&number);// storing the number
    guess=number/2;// initializing guess number
    while((guess*guess - number) > ACCURACY)//loop runs until the square root is found
    {
        guess = (guess + number/guess )/2;// updating the guess number
    }
    square_root = guess;//assigning the value of guess number to square root.
    printf("square root of %.2f = %.4f",number,square_root);//final answer
} 

Thanking you !


Thursday, 26 May 2016

Permutations and Combinations #1 : C program.

Hello friends !
Today we are going to deal with extremely powerful tool of 'C' language , and that is user-defined "Functions".
Some prerequisite knowledge is required to understand this program, and that is -

  1. Switch case:
  2. Functions 
  3. Mathematical meaning of Permutations and Combination.
Basically , Permutation is the number of possible arrangements and Combination is the number of possible selections in a given case and under certain constraints .
Here we are going to calculate simply , nPr and nCr. 
We have certain mathematical formulas for calculating this -
  • nPr = n!/r! .
  • nCr = n!/(r!*(n-r)!) .
So , what we need to do is , make three functions for calculating 1.Factorial 2.Permutations & 3.Combinations .

* For finding factorial we can initiate fact=1, and run a loop using counter variable i n times performing the calculation fact=i*fact and increment i by 1 in each loop.
* And for finding nPr and nCr all we need to do is call function of factorial and put the required in factorial in the given formula .
And that's it !
Try to code yourself or copy the program given below , and check whether it is running !


C Program : Permutations and Combinations !



#include < stdio.h > 
/*program to calculate permutations and combinations !*/
int comb();/*function for calculating combination*/
int per();/*function for calculating permutation*/
int fact();/*function for calculating factorial*/

int main()
{
    int e,n,r,i,c,p;
    printf("Enter \n1. Permutations\n2. Combinations !\n");
    scanf("%d",&e);
    printf("Enter n and r\n");
    scanf("%d%d",&n,&r);
    switch(e)
    {
    case 1:
        p=per(n,r);/*calling a function for calculating permutation */
        /* take note that values within the parenthesis are assigned to parameters of the function*/
        printf("nPr=%d\n",p);/*printing the answer*/
        break;/*break is essential while using Switch-case after every step*/
        /* If break is not used then the all following cases will be calculated*/
    case 2:
        c=comb(n,r);
        printf("nCr=%d\n",c);
        break;
    default :
        printf("bye!");
    }

}
int per(int x,int y)
{
    int a,b,c;
    a=fact(x);
    b=fact(y);
    c=a/b;/* according to formula of nPr */
    return(c);/*returning the calculated value of nPr*/
}
int comb(int x,int y)
{
    int a,b,c,d;
    a=fact(x);
    b=fact(y);
    c=fact(x-y);
    d=a/(b*c);/*according to formula of nCr*/
    return(d);/*returning the calculated value of nCr*/
}
int fact(int x)
{
    int i,p=1;
    for(i=1;i < = x ; i++)
    {
        p=p*i;/* multiplying 1*2*3*...n times to find factorial*/
    }
    return(p);/* Returning value of calculated factorial*/
}


"Thanks for referring this , any modification and suggestions are accepted. "

Wednesday, 25 May 2016

Prime numbers #1 : C program.

First be clear.
What is prime number?
"A prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself. "

simply, a number that can be divided by 1 and itself only.

How to start thinking ?
Clearly , every number is divisible by 1. So , our task is to check whether the number has any other divisor except itself or not, and If not , the number is prime number.

Program #1 : Print prime numbers up to n , where n is entered by user .

So we need to check every number falling in the domain 2<=i<=n,
where i is arbitrary number starting from 2 and ending with n itself .

Now,
How to check-

  • Consider numbers from 2 to that number.
  • Check for divisibility of the number from 2 to the number under consideration by using %(division modulo).
  • If there is a divisor apply break!
  • Means number may not be prime unless the number under consideration is divisor itself.
  • So if the divisor is the considered number .
  • The number is prime .
  • Print that number.

Now try to follow this steps for building a program and if you are unable to build copy the code given here. The code contains comments for explanation purpose . 
How to follow these procedure in c program :


C Program : Prime numbers !




#include < stdio.h > 
int main()
{
    int i,j,n;/*declaration of required variables , i for considering number ,j for checking divisibility and n for user entry.*/
    printf("Enter n upto which you want prime number !\n");
    scanf("%d",&n);
    printf("Required prime numbers are:\n");
    for(i=2;i < = n;i++)/*loop for considering number from 2 to n*/
    {
        for(j=2;j < = i;j++)/*loop for checking divisibility of number*/
        {
            if(i%j==0)/*the number has divisor , break here*/
                break ;
        }
            if(i==j)/*the divisor is number itself, the number is prime according to definition*/
                printf("%d ",i);/*printing the prime number*/
    }
}



Thanking you !

Tuesday, 24 May 2016

Prime numbers #2 : C program .

Program #2 : 

Print the first n prime numbers, where n is entered by user .

Hi there !

Now we already know the logic of printing prime numbers .
Here what we wanna do is printing FIRST n prime numbers and not prime numbers up to n. So simply following that logic but in a little different way, we can achieve this.
Here we will introduce a counter variable , which will continue the process of printing prime number until it prints n prime numbers.
As we need to stop the procedure when the required numbers of prime numbers are printed , we must use EXIT control loop here . i.e. do{}while();
Try to develop program by yourself or copy the code given below !


C program : Prime numbers !



#include < stdio.h > 
int main()
{   int i,j,n;/*program to print first n prime numbers*/
    int count=0;/*variable for counting must be initiated*/
    printf("Enter n for printing first n prime numbers:\n");
    scanf("%d",&n);
    printf("First %d prime numbers are\n",n);
    i=2;/*consideration of numbers from 2 to nth prime number starts*/
    do{
            for(j=2;j < = i;j++)/*checking divisibility*/
                {
                    if(i%j==0)
                    {
                        break;
                    }
                }
                if(i==j)
                {printf("%d\n",i);/*braces must be used as if block contains two statements*/
                count++;}/*increment in counter variable for each prime number*/
                i++;/*increment in i for loop to be continued*/
    }
    while(count < n);/*test condition: printing n prime numbers up to sentinel variable*/


}


Thanking you !