Wednesday, 22 February 2012

find GCD of two numbers

/*P5.8 Program to find GCD of two numbers*/

#include<stdio.h>
int GCD(int a, int b);
int gcd(int a, int b);

main()
{

    int a, b;
    printf("Enter a and b : \n");
    scanf("%d%d",&a, &b);
    printf("%d\n",GCD(a,b));
    printf("%d\n",gcd(a,b));
}/*End of main()*/

/*Recursive*/
int GCD(int a, int b)  
{
    if(b==0)
        return a;
    return GCD(b, a%b);
}/*End of GCD()*/

/*Iterative*/
int gcd(int a, int b)
{
    int rem;
    while(b != 0)
    {
        rem = a%b;
        a = b;
        b = rem;
    }
    return a;
}/*End of gcd()*/



print the prime factors

/*P5.7 Program to print the prime factors*/

#include<stdio.h>
void PFactors( int num);
void IPFactors( int n);

main( )
{
    int num;
    printf("Enter a number : ");
    scanf("%d", &num);
    PFactors(num);    printf("\n");
    IPFactors(num);    printf("\n");
}/*End of main()*/

void PFactors( int num)
{
    int i = 2;
    if( num == 1 )
        return;
    while( num%i != 0 )
        i++;
    printf("%d ", i);
    PFactors(num/i);
}/*End of PFactors()*/

/*Iterative*/
void IPFactors( int num)
{
    int i;
    for( i = 2; num!=1; i++)
        while( num%i == 0 )
        {
            printf("%d ", i);
            num = num/i;
        }
}/*End of IPFactors()*/

raise a floating point number to a positive integer

/*P5.6 Program to raise a floating point number to a positive integer*/
#include<stdio.h>
float power(float a , int n);
float Ipower(float a , int n);
main( )
{
    float a, p;
    int n;
    printf("Enter a and n : ");
    scanf("%f %d", &a, &n);
    p = power(a, n);
    printf("%f raised to power %d is %f\n", a, n, p);
    p = Ipower(a, n);
    printf("%f raised to power %d is %f\n", a, n, p);
}/*End of main()*/

/*Recursive*/
float power(float a , int n)
{
    if(n == 0)
        return(1);
    else
        return(a * power(a,n-1));
}/*End of power()*/

/*Iterative*/
float Ipower(float a , int n)
{
    int i;
    float result=1;
    for(i=1; i<=n; i++)
        result = result * a;
    return result;
}/*End of Ipower()*/

convert a positive decimal number to Binary, Octal or Hexadecimal

/* P5.5 Program to convert a positive decimal number to Binary, Octal or Hexadecimal */
#include<stdio.h>
void convert(int, int);
main()
{
    int num;
    printf("Enter a positive decimal number : ");
    scanf("%d", &num);
    convert(num, 2);
    printf("\n");
    convert(num, 8);
    printf("\n");
    convert(num, 16);
    printf("\n");
}/*End of main()*/

void convert (int num, int base)
{
    int rem = num%base;
   
    if(num==0)
        return;
    convert(num/base, base);
   
    if(rem < 10)
        printf("%d", rem);   
    else
        printf("%c", rem-10+'A' );
}/*End of convert()*/


display integer as sequence of digits and find sum of its digits

/*P5.4 Program to display integer as sequence of digits and find sum of its digits*/

#include<stdio.h>
void display(long int n);
void Rdisplay(long int n);
int sumdigits( long int n);

main( )
{
    long int num;
    printf("Enter number : ");
    scanf("%ld", &num);
    printf("%d\n",sumdigits(num));
    printf("\n");
    display(num);
    printf("\n");
    Rdisplay(num);
    printf("\n");
}/*End of main()*/

/*Finds the sum of digits of an integer*/
int sumdigits(long int n)
{
    if( n/10 == 0 ) /* if n is a single digit number*/
        return n;
    return n%10 + sumdigits(n/10);       
}/*End of sumdigits()*/

/*Displays the digits of an integer*/
void display(long int n)
{
    if( n/10==0 )
    {
        printf("%d",n);
        return;
    }
    display(n/10);
    printf("%d",n%10);   
}/*End of display()*/

/*Displays the digits of an integer in reverse order*/
void Rdisplay(long int n)
{
    if(n/10==0)
    {
        printf("%d",n);
        return;
    }
    printf("%d",n%10);
    Rdisplay(n/10);
}/*End of Rdisplay()*/   

   

Series : 1 + 2 + 3 + 4 + 5 +.......

/*P5.3 Program to display and find out the sum of series*/
/* Series : 1 + 2 + 3 + 4 + 5 +....... */

#include<stdio.h>
int series(int n);
int rseries(int n);

main( )
{
    int n;
    printf("Enter number of terms : ");
    scanf("%d", &n);
   
    printf("\b\b = %d\n", series(n));    /*  \b to erase last +sign */
    printf("\b\b = %d\n\n\n", rseries(n));
}/*End of main()*/

/*Iterative function*/
int series(int n)
{
    int i, sum=0;
    for(i=1; i<=n; i++)
    {
        printf("%d + ", i);
        sum+=i;   
    }
    return sum;
}/*End of series()*/

/*Recursive function*/
int rseries(int n)
{
    int sum;
    if(n == 0)
        return 0;
    sum = (n + rseries(n-1));
    printf("%d + ",n);
    return sum;
}/*End of rseries()*/

display numbers from 1 to n and their sum

/*P5.2 Program to display numbers from 1 to n and their sum*/

#include<stdio.h>
int summation(int n);
void display1(int n);
void display2(int n);

main( )
{
    int n;
    printf("Enter number of terms : ");
    scanf("%d", &n);
   
    display1(n);
    printf("\n");

    display2(n);
    printf("\n");
   
    printf("sum = %d\n", summation(n));   
}/*End of main()*/
int summation( int n)
{
    if(n==0)
        return 0;
    return ( n + summation(n-1) );
}/*End of summation()*/

/*displays in reverse order*/
void display1(int n)
{
    if( n==0 )
       return;
    printf("%d ",n);   
    display1(n-1);
}/*End of display1()*/

void display2(int n)
{
    if( n==0 )
       return;
    display2(n-1);
    printf("%d ",n);   
}/*End of display2()*/