Chapter 5 : Functions & Pointers (Let us C)

functions nd pointers in c

 
(a) Write a function to calculate the factorial value of any integer entered through the keyboard
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-1
/* Write a function to calculate the factorial value of any integer entered through the keyboard */
#include<stdio.h>
int N;
void Fact(int);
void main()
{
    clrscr();
    printf("Enter a No: ");
    scanf("%d",&N);
    if(N<0)
        printf("Factorial of Negative Numbers not possible\n");
    else
        Fact(N);
}

void Fact(int a)
{
    long long Fact=1;    //Factorial will be large value hence used long long
    int temp;
    temp = a;
    while(temp>1)
    {
        Fact=Fact*temp;
        temp--;
    }
    printf("Factorial of %d is %lld",a,Fact);
}
(b) Write a function power ( a, b ), to calculate the value of a raised to b.
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-2
/* Write a function power ( a, b ), to calculate the value of a raised to b
This program gives output for power raised to -ve or +ve whole number only*/

#include<stdio.h>
float a,b;
void power(float, float);
void main()
{
    clrscr();
    printf("Program to calculate power of 'a' raised to 'b'\n");
    printf("Enter value of 'a' : ");
    scanf("%f",&a);
    printf("Enter value of 'b' : ");
    scanf("%f",&b);
    power(a,b);
}

void power(float x, float y)
{
    double mult=1.0;
    int temp;
    temp=y;
    if(temp<0)
    {
        temp = temp*(-1);
        while(temp)
        {
            mult=mult*x;
            temp--;
        }
        mult=1/mult;  // If -ve power
    }
    else
    {
        while(temp>0)
        {
            mult = mult*x;
            temp--;
        }
    }
    printf("%f raised to %f = %lf",x,y,mult);
}
(c) Write a general-purpose function to convert any given year into its roman equivalent. The following table shows the roman equivalents of decimal numbers: 1 = i, 5 = v, 10 = x, 50 = l, 100 = c, 500 = d, 1000 = m
Example: Roman equivalent of 1988 is mdcccclxxxviii 
                 Roman equivalent of 1525 is mdxxv & of 1185 is mclxxxv
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-3
/* Write a general-purpose function to convert any given year into its roman equivalent. The following table shows the roman 
equivalents of decimal numbers: 1 = i, 5 = v, 10 = x, 50 = l, 100 = c, 500 = d, 1000 = m
Example: Roman equivalent of 1988 is mdcccclxxxviii 
         Roman equivalent of 1525 is mdxxv and of 1185 is mclxxxv */
#include<stdio.h>
unsigned int Y;
void Roman(unsigned int);
void main()
{
    clrscr();
    printf("Program to convert any given year into its roman equivalent\n");
    printf("Enter any year: ");
    scanf("%d",&Y);
    Roman(Y);
}

void Roman(unsigned int Year)
{
    unsigned int Thousand,Hundred,Ten,Unit,i;
    //-------- Calculation for Thousands' place--------
    Thousand = Year/1000;    // remove last 3 places
    for(i=0;i<Thousand;i++)
        printf("m");

    //-------- Calculation for Hundreds' place--------
    Hundred = Year/100;    // remove last 2 places
    Hundred = Hundred%10;    // get unit place i.e. 3rd place of the year
    switch(Hundred)
    {
        case 0:    break;
        case 1:    printf("c");
                break;
        case 2:    printf("cc");
                break;
        case 3:    printf("ccc");
                break;
        case 4:    printf("cccc");
                break;
        case 5:    printf("d");
                break;
        case 6:    printf("dc");
                break;
        case 7:    printf("dcc");
                break;
        case 8:    printf("dccc");
                break;
        case 9:    printf("dcccc");
                break;
        default:    break;
    }

    //-------- Calculation for Tens' place--------
    Ten = Year%100;    // get last 2 places, remove 1st 2
    Ten = Ten/10;    // remove unit place to get Tens' place
    switch(Ten)
    {
        case 0:    break;
        case 1:    printf("x");
                break;
        case 2:    printf("xx");
                break;
        case 3:    printf("xxx");
                break;
        case 4:    printf("xxxx");
                break;
        case 5:    printf("l");
                break;
        case 6:    printf("lx");
                break;
        case 7:    printf("lxx");
                break;
        case 8:    printf("lxxx");
                break;
        case 9:    printf("lxxxx");
                break;
        default:    break;
    }
   
    //-------- Calculation for Units' place--------
    Unit = Year%10;    // get unit place
    switch(Unit)
    {
        case 0:    break;
        case 1:    printf("i");
                break;
        case 2:    printf("ii");
                break;
        case 3:    printf("iii");
                break;
        case 4:    printf("iv");
                break;
        case 5:    printf("v");
                break;
        case 6:    printf("vi");
                break;
        case 7:    printf("vii");
                break;
        case 8:    printf("viii");
                break;
        case 9:    printf("ix");
                break;
        default:    break;
    }
}
(d) Any year is entered through the keyboard. Write a function to determine whether the year is a leap year or not.
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-4
/* Any year is entered through the keyboard. Write a function to determine whether the year is a leap year or not */
#include<stdio.h>
unsigned int Year;
void Leap_Year(unsigned int);
void main()
{
    clrscr();
    printf("Program to determine whether the year is a leap year or not\n");
    printf("Enter any year: ");
    scanf("%d",&Year);
    Leap_Year(Year);
}

void Leap_Year(unsigned int Y)
{
    if((Y%400==0)||((Y%4==0)&&(Y%100!=0)))
        printf("Year %d is a Leap Year",Y);
    else
        printf("Year %d is NOT a Leap Year",Y);
}
(e) A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number.
For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7. 
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-5
/* A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number.
For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7 */
#include<stdio.h>
unsigned int Num;
void Prime_Fact(unsigned int);
void main()
{
    clrscr();
    printf("Program to find Prime factors of given number\n");
    printf("Enter a No: ");
    scanf("%d",&Num);
    Prime_Fact(Num);
}

void Prime_Fact(unsigned int N)
{
    unsigned int i=2;
    printf("Prime factors of %d are: ",N);
    while(N>1)
    {
        while(N%i==0)  // If remainder equals to 0
        {
            printf("%d ",i); // print the factor
            N=N/i;              // divide number by factor
        }
        i++;  // else increment the factor
    }
}

(f) Write a function which receives a float and an int from main(), finds the product of these two and returns the product which is printed through main(). 
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-6
/* Write a function which receives a float and an int from main(), finds the product of these two and returns the 
product which is printed through main() */
#include<stdio.h>
#include<conio.h>
float Product_1(float,int);
int Product_2(float,int);
void main()
{
    float a;
    int b;
    clrscr();
    printf("Program to find product of float & int\n");
    printf("Enter float value: ");
    scanf("%f",&a);
    printf("Enter int value: ");
    scanf("%d",&b);
    printf("\nProduct of %0.2f & %d = %0.2f",a,b,Product_1(a,b));
    printf("\nProduct of %0.2f & %d = %d",a,b,Product_2(a,b));
}

float Product_1(float x, int y) // function returning float
{
    float mult;
    mult = x*y;
    return(mult);
}
int Product_2(float x, int y) // function returning int
{
    int mult;
    mult = x*y;
    return(mult);
}

(g) Write a function that receives 5 integers and returns the sum, average and standard deviation of these numbers. Call this function from main( ) and print the results in main( )

// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-7
/* Write a function that receives 5 integers and returns the sum, average and standard deviation of these numbers. 
Call this function from main( ) and print the results in main( )
Standard deviation = sqrt of Variance
Variance = Avegrage of squares of difference of each entity from its mean */ /************************************************************************** Standard deviation = sqrt of Variance Variance = Avegrage of squares of difference of each entity from its mean ***************************************************************************/ #include<stdio.h> #include<conio.h> #include<math.h> Funct(float, float, float, float, float, float *, float *, float *); void main() { float a,b,c,d,e; float Sum, Avg, SD; clrscr(); printf("Program to calculate sum, average and standard deviation of 5 ints\n"); printf("Enter 5 integers: "); scanf("%f%f%f%f%f",&a,&b,&c,&d,&e); Funct(a, b, c, d, e, &Sum, &Avg, &SD); printf("Sum = %f\nAverage = %f\nStd. Deviation = %f",Sum,Avg,SD); } Funct(float a,float b,float c,float d,float e,float *S,float *Avrg,float *StdDev) { float Sum,Average,p,q,r,s,t,Variance; Sum = a+b+c+d+e; Average = Sum/5; p = a-Average; q = b-Average; r = c-Average; s = d-Average; t = e-Average; Variance = (p*p + q*q +r*r + s*s + t*t)/5; *S=Sum; *Avrg=Average; *StdDev=sqrt(Variance); }

(h) Write a function that receives marks received by a student in 3 subjects and returns the average and percentage of these marks. Call this function from main( ) and print the results in main( ).
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-8
/* Write a function that receives marks received by a student in 3 subjects and returns the average and percentage 
of these marks. Call this function from main( ) and print the results in main( ) */
#include<stdio.h>
float Marks(float,float,float,float,float *,float *);
void main()
{
    float a,b,c,max,Avg,Per;
    clrscr();
    printf("Program to calculate average and percentage for a student\n");
    printf("Enter marks obtained in 3 subjects\n");
    scanf("%f%f%f",&a,&b,&c);
    printf("Enter maximum marks in one subject: ");
    scanf("%f",&max);
    Marks(a,b,c,max,&Avg,&Per);
    printf("Avg = %0.2f\nPercentage = %0.2f%",Avg,Per);
}

float Marks(float p, float q, float r, float m, float *A, float *P)
{
    *A = (p+q+r)/3.0;
    *P = (p+q+r)*100/(m*3);
}

(i) A 5-digit positive integer is entered through the keyboard, write a function to calculate sum of digits of the 5-digit number:  (1) Without using recursion  (2) Using recursion 
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-9
/* A 5-digit positive integer is entered through the keyboard, write a function to calculate sum of digits of the 
5-digit number:  (1) Without using recursion  (2) Using recursion  */
#include<stdio.h>
unsigned int Sum=0,Rem;
unsigned int Rec_CalSum(long);
unsigned int Cal_Sum(long);
void main()
{
    long Num;
    unsigned int Rec_Sum, NRec_Sum;
    clrscr();
    printf("Program to calculate sum of digits of a 5-digit number\n");
    printf("Enter a 5-digit positive integer: ");
    scanf("%ld",&Num);
    Rec_Sum=Rec_CalSum(Num); // with recursion
    printf("Sum of digits using Recursion = %d\n",Rec_Sum);
    NRec_Sum=Cal_Sum(Num); // without recursion
    printf("Sum of digits withou Recursion = %d",NRec_Sum);
}

unsigned int Rec_CalSum(long N)    // Recursive function
{
    Rem=N%10;
    Sum=Sum+Rem;
    N=N/10;
    if(N != 0)
        Rec_CalSum(N);    // Recursion
    else
    {
        Rem=Sum;
        Sum=0;
        return(Rem);
    }
}

unsigned int Cal_Sum(long M)    // Normal function
{
    while(M)
    {
        Rem=M%10;
        Sum=Sum+Rem;
        M=M/10;
    }
    return(Sum);
}

(j) A positive integer is entered through the keyboard, write a program to obtain the prime factors of the number. Modify the function suitably to obtain the prime factors recursively.
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-10
/* A positive integer is entered through the keyboard, write a program to obtain the prime factors of the number. 
Modify the function suitably to obtain the prime factors recursively */
#include<stdio.h>
void Prime_Fact(unsigned int);
void Rec_Prime_Fact1(unsigned int);
void Rec_Prime_Fact2(unsigned int,unsigned int);

void main()
{
    unsigned int Num;
    clrscr();
    printf("Program to obtain the prime factors of the number\n");
    printf("\nEnter a Positive No. : ");
    scanf("%d",&Num);
    Prime_Fact(Num);
    printf("\nPrime factors of %d (using recursion method1) are: ",Num);
    Rec_Prime_Fact1(Num);
    printf("\nPrime factors of %d (using recursion method2) are: ",Num);
    Rec_Prime_Fact2(Num,2);
}

void Prime_Fact(unsigned int N)    // Normal Function
{
    unsigned int i=2;
    printf("\nPrime factors of %d are: ",N);
    while(N>1)
    {
        while(N%i==0)
        {
            printf("%d ",i);
            N=N/i;
        }
        i++;
    }
}

/************************* Recursive Approach 1 *************************/
void Rec_Prime_Fact1(unsigned int N)
{
    static unsigned int i = 2 ; // defined static so as to initialize only once
    while(N%i==0)
    {
        printf("%d ",i);
        N=N/i;
    }
    if(N>1)
    {
        i++;
        Rec_Prime_Fact1(N); //recursion
    }
}

/************************* Recursive Approach 2 *************************/
void Rec_Prime_Fact2(unsigned int N, unsigned int i)
{
    if(N%i==0)
    {
        printf("%d ",i);
        N=N/i;
        if(N>1)
            Rec_Prime_Fact2(N,i); //recursion
        else
            exit();
    }
    else
    {
        i++;
        Rec_Prime_Fact2(N,i); //recursion
    }
}

(k) Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence:
1 1 2 3 5 8 13 21 34 55 89...


// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-11
/* Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the 
sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence:
1 1 2 3 5 8 13 21 34 55 89...*/
#include<stdio.h>
unsigned long int a=1,b; // global variable initialized to 0
void Fibonacci(unsigned int);
void main()
{
    unsigned int N;
    clrscr();
    printf("Program to obtain the first 25 numbers of a Fibonacci sequence\n");
    printf("Enter No. of terms: ");
    scanf("%d",&N);
    Fibonacci(N);
}

void Fibonacci(unsigned int M)
{
    M--;
    printf("%ld ",a);
    a=a+b;
    b=a-b;
    if(M>0)
    {
        Fibonacci(M);    // recursion
    }
}

(l) A positive integer is entered through the keyboard, write a function to find the binary equivalent of this number using recursion.
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-12
/* A positive integer is entered through the keyboard, write a function to find the binary equivalent of this number 
using recursion */
#include<stdio.h>
#include<conio.h>
unsigned int N;
void BINARY(unsigned int);
void binary(unsigned int);
void main()
{
    clrscr();
    printf("Program to find the binary equivalent of the number using recursion.\n");
    printf("Enter a positive number: ");
    scanf("%d",&N);
    BINARY(N);
    printf("\n");
    binary(N);
}

/********* Method 1 **************/
void BINARY(unsigned int Num)
{
    unsigned int a;
    static unsigned int b=31;
    while(b>0)
    {
        b--;
        a = (Num>>b);
        if(a&1)
            printf("1");
        else
            printf("0");
        BINARY(Num);
    }
}

/********* Method 2 **************/
void binary(unsigned int n)
{   
    int y;
    if(n>0)
    {   
        y=n;
        n=n/2;
        binary(n);
        printf("%d",y%2);
    }
}

(m) Write a recursive function to obtain the running sum of first 25 natural numbers.
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-13
/* Write a recursive function to obtain the running sum of first 25 natural numbers */
#include<stdio.h>
#define Limit 25
unsigned int REC_SUM(unsigned int);
void main()
{
    unsigned int Sum;
    clrscr();
    printf("Program to obtain the running sum of first 25 natural numbers.\n");
    Sum = REC_SUM(Limit);
    printf("Sum of 1st %d natural numbers is: %d",Limit,Sum);
}

unsigned int REC_SUM(unsigned int S)
{
    if(S==1)
        return 1;
    else
        S = S + REC_SUM(S-1);
    return S;
}

(n) Write a C function to evaluate the series sin(x) = x - (x3 / 3!) + (x5 / 5!) - (x7 / 7!) +...
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-14
/* Write a C function to evaluate the series sin(x) = x - (x3 / 3!) + (x5 / 5!) - (x7 / 7!) +... */
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define PI 3.14159    // value of Pi
void Funct_Sin(float,int);
double power(float , float );
double Fact(float );

void main()
{
    unsigned int N;
    float X;
    clrscr();
    printf("Program to Evaluate Sin(x) series\n");
    printf("Enter No. of terms: ");
    scanf("%d",&N);
    printf("Enter value of x: ");
    scanf("%f",&X);
    Funct_Sin(X,N);
}

void Funct_Sin(float x, int n)
{
    float i,y;
    double sum = 0.0;
    y=x;
    x=x*PI/180;    // conversion from degree to radian
    for(i=0;i<n;i++)
    {
        sum = sum + (power((float)-1,(float)i)*power(x,(float)(2*i+1)))/Fact(2*i+1);
    }
    printf("\nSin(%2.2f) = %2.4lf",y,sum);
}

double power(float a, float b)
{
    double p;
    if(b==0.0)
        return 1;
    else if(b==1.0)
        return a;
    else
        p = a*power(a,b-1);
    return p;
}

double Fact(float N)
{
    double fact=1.0;
    if(N==1)
        return 1;
    else
        fact = N*Fact(N-1);
    return fact;
}

(o) Given three variables x, y, z write a function to circularly shift their values to right. In other words if x = 5, y = 8, z = 10 after circular shift y = 5, z = 8, x =10. Call the function with variables a, b, c to circularly shift values.
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-15
/* Given three variables x, y, z write a function to circularly shift their values to right. In other words if x = 5, 
y = 8, z = 10 after circular shift y = 5, z = 8, x =10. Call the function with variables a, b, c to circularly shift values */
#include<stdio.h>
void ShiftRight(int,int,int);
void main()
{
    int x,y,z;
    clrscr();
    printf("Program to circularly shift the values of 3 variables to right \n");
    printf("Enter 3 variables: ");
    scanf("%d%d%d",&x,&y,&z);
    printf("Before Shifting\n%d %d %d",x,y,z);
    ShiftRight(x,y,z);
}

void ShiftRight(int a,int b,int c)
{
    int temp;
    temp = a;
    a = c;
    c = b;
    b = temp;
    printf("\nAfter Shifting\n%d %d %d",a,b,c);
}

(p) Write a function to find the binary equivalent of a given decimal integer and display it.

// refer l in above examples



(q) If the lengths of the sides of a triangle are denoted by a, b, and c, then area of triangle is given by area = Sqrt(S(S - a)(S - b)(S - c)) where, S = ( a + b + c ) / 2
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-16
/* If the lengths of the sides of a triangle are denoted by a, b, and c, then area of triangle is given by 
area = Sqrt(S(S - a)(S - b)(S - c)) where, S = ( a + b + c ) / 2 */
#include<stdio.h>
#include<math.h>
double Triangle_Area(float,float,float);
void main()
{
    float a,b,c;
    double area;
    clrscr();
    printf("Program to calculate Area of Triangle ABC\n");
    printf("Enter side AB: ");
    scanf("%f",&a);
    printf("Enter side AC: ");
    scanf("%f",&c);
    printf("Enter side BC: ");
    scanf("%f",&b);
    area = Triangle_Area(a,b,c);
    printf("Area of traingle ABC = %0.2lf Sq.Unit",area);
}

double Triangle_Area(float x,float y,float z)
{
    double S,A;
    S = (x+y+z)/2;
    A = sqrt(S*(S-x)*(S-y)*(S-z));
    return A;
}

(r) Write a function to compute the distance between two points and use it to develop another function that will compute the area of the triangle whose vertices are A(x1, y1), B(x2, y2), and C(x3, y3). Use these functions to develop a function which returns a value 1 if the point (x, y) lies inside the triangle ABC, otherwise a value 0.
// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-17
/* Write a function to compute the distance between two points and use it to develop another function that will 
compute the area of the triangle whose vertices are A(x1, y1), B(x2, y2), and C(x3, y3). Use these functions to 
develop a function which returns a value 1 if the point (x, y) lies inside the triangle ABC, otherwise a value 0 */
#include<stdio.h>
#include<math.h>     //for sqrt & pow functions

float Calc_dist(float ,float ,float ,float);
double Triangle_Area(float ,float ,float ,float ,float ,float);
int Check_Point(float ,float ,float ,float ,float ,float ,float ,float);

void main()
{
    float x1,y1,x2,y2,x3,y3,x,y;
    clrscr();
    printf("Program to find if a point lies inside the triangle or not");
    printf("\nEnter co-ordinates of vertices of triangle ABC\n");
    printf("\nEnter 2 co-ordinates(x1,y1) of A: ");
    scanf("%f%f",&x1,&y1);
    printf("Enter 2 co-ordinates(x2,y2) of B: ");
    scanf("%f%f",&x2,&y2);
    printf("Enter 2 co-ordinates(x3,y3) of C: ");
    scanf("%f%f",&x3,&y3);
    printf("Enter co-ordinates(X,Y) of any point P: ");
    scanf("%f%f",&x,&y);
    if(Check_Point(x1,y1,x2,y2,x3,y3,x,y)==1)
        printf("\nPoint P lies inside Triangle ABC");
    else
        printf("\nPoint P lies outside Triangle ABC");
}

int Check_Point(float x1,float y1,float x2,float y2,float x3,float y3,float x,float y)
{
    double ABC,PAB,PBC,PAC;    // areas of each triangles
    double Total;
    int D;
    ABC = Triangle_Area(x1,y1,x2,y2,x3,y3);
    printf("\nArea of Triangle ABC = %0.2lf sq.Unit",ABC);
    PAB = Triangle_Area(x,y,x1,y1,x2,y2);
    PBC = Triangle_Area(x,y,x2,y2,x3,y3);
    PAC = Triangle_Area(x,y,x1,y1,x3,y3);
    Total = PAB+PBC+PAC;
    printf("\nSum of areas of 3 Triangles = %0.2lf sq.Unit",Total);
    D = ABC-Total;
    if(D==0)
        return 1;
    else
        return 0;
}

double Triangle_Area(float p1,float q1,float p2,float q2,float p3,float q3)
{
    float AB,BC,AC,S;
    double AREA;
    AB = Calc_dist(p1,q1,p2,q2);
    BC = Calc_dist(p2,q2,p3,q3);
    AC = Calc_dist(p1,q1,p3,q3);
    S = (AB+BC+AC)/2;
    AREA = sqrt(S*(S-AB)*(S-BC)*(S-AC));
    return AREA;
}

float Calc_dist(float a1,float b1,float a2,float b2)
{
    float dist,a,b;
    a = a1-a2;
    b = b1-b2;
    dist = sqrt(pow(a,2)+pow(b,2));
    return dist;
}

(s) Write a function to compute the greatest common divisor given by Euclid's algorithm, exemplified for J = 1980, K = 1617 as follows:

1980 / 1617 = 1 1980 - 1 * 1617 = 363
1617 / 363 = 4     1617 - 4 * 363 = 165
363 / 165 = 2     363 - 2 * 165 = 33
165 / 33 = 5     165 - 5 * 33 = 0

// Let Us C Chapter-5:(Chapter 5 : Functions and Pointers) : Program-18
/* Write a function to compute the greatest common divisor given by Euclid's algorithm, exemplified for J = 1980, 
K = 1617 as follows:

1980 / 1617 = 1 1980 - 1 * 1617 = 363
1617 / 363 = 4     1617 - 4 * 363 = 165
363 / 165 = 2     363 - 2 * 165 = 33
165 / 33 = 5     165 - 5 * 33 = 0 */ #include<stdio.h> void GCD(unsigned int, unsigned int); void main() { unsigned int L,S,temp; clrscr(); printf("Program to compute GCD using Euclid's algorithm\n"); printf("Enter 1st Number: "); scanf("%d",&L); printf("Enter 2nd Number: "); scanf("%d",&S); if(S>L) { temp = L; L = S; S = temp; } GCD(L,S); } void GCD(unsigned int a, unsigned int b) { unsigned int D,R; { D = a/b; R = a - D*b; a = b; if(R==0) printf("GCD = %d",b); else { b=R; GCD(a,b); } } }

Comments

Post a Comment

Popular posts from this blog

Let Us C (Chapter 8 : Arrays) : Program-9

Chapter 3: The Loop Control Structure (Let Us C)