C Programming Lab Manual – 30

Write a C program to print the chain of pointers.

Function Main
     float var = 23.564327;
    Declaring pointer variables upto level_4
    Initializing pointer variables
	ptr1 = &var;
	ptr2 = &ptr1;
	ptr3 = &ptr2;
	ptr4 = &ptr3;
Output values
	"Value of var = %f\n", var);
    "Value of var using level-1"
		" pointer = %f\n",
		*ptr1);
	"Value of var using level-2"
		" pointer = %f\n",
		**ptr2);
	"Value of var using level-3"
		" pointer = %f\n",
		***ptr3);
	"Value of var using level-4"
		" pointer = %f\n",
		****ptr4);
End

#include <stdio.h>
int main()
{
    //!showMemory()
	float var = 23.564327;

	// Declaring pointer variables upto level_4
	float *ptr1, **ptr2, ***ptr3, ****ptr4;

	// Initializing pointer variables
	ptr1 = &var;
	ptr2 = &ptr1;
	ptr3 = &ptr2;
	ptr4 = &ptr3;

	// Printing values
	printf("Value of var = %f\n", var);
	printf("Value of var using level-1"
		" pointer = %f\n",
		*ptr1);
	printf("Value of var using level-2"
		" pointer = %f\n",
		**ptr2);
	printf("Value of var using level-3"
		" pointer = %f\n",
		***ptr3);
	printf("Value of var using level-4"
		" pointer = %f\n",
		****ptr4);

	return 0;
}

Value of var = 23.564327                                                        
Value of var using level-1 pointer = 23.564327                                  
Value of var using level-2 pointer = 23.564327                                  
Value of var using level-3 pointer = 23.564327                                  
Value of var using level-4 pointer = 23.564327

C Programming Lab Manual – 29

Write a C program to calculate the GCD of two numbers using the Euclidean algorithm.

Function Main
    Declare Integer A
    Declare Integer B
    Declare Integer res
    
    Output "Insert 2 numbers to calculate GCD"
    Output "First number: "
    Input A
    Output "Second number"
    Input B
    Assign res = GCD(a,b)
    Output res
End

Function GCD (Integer a, Integer b)
    Declare Integer res
    
    If a = 0
        Assign res = b
    Else
        Assign res = GCD(b MOD a, a)
    End
Return Integer res

#include <stdio.h>

int GCD(int a, int b);

int main() {
    int a;
    int b;
    int res;
    
    printf("Insert 2 numbers to calculate GCD:\n");
    printf("First number: \n");
    scanf("%d", &a);
    printf("Second number:\n");
    scanf("%d", &b);
    res = GCD(a, b);
    printf("%d\n", res);
    return 0;
}

int GCD(int a, int b) {
    int res;
    
    if (a == 0) {
        res = b;
    } else {
        res = GCD(b % a, a);
    }
    
    return res;
}

Insert 2 numbers to calculate GCD:
First number: 
5
Second number:
15
5

C Programming Lab Manual – 28

Write a C program to multiply two numbers without the “*” operator.

Function Main
    Declare Integer fact1
    Declare Integer fact2
    Output " First factor"
    Input fact1
    Output "Second factor"
    Input fact2
    Output multiply(fact1, fact2)
End

Function multiply (Integer x, Integer y)
    Declare Integer result
    If y = 0
        Assign result = 0
    End
    If y > 0
        Assign result = x + multiply(x, y-1)
    End
    If y < 0
        Assign result = -multiply(x, -y)
    End
Return Integer result

#include <stdio.h>

int multiply(int x, int y);

int main() {
    int fact1;
    int fact2;
    
    printf("First factor:\n");
    scanf("%d", &fact1);
    printf("Second factor:\n");
    scanf("%d", &fact2);
    printf("%d\n", multiply(fact1, fact2));
    
    return 0;
}

int multiply(int x, int y) {
    int result;
    
    if (y == 0) {
        result = 0;
    }
    if (y > 0) {
        result = x + multiply(x, y - 1);
    }
    if (y < 0) {
        result = (int) (-multiply(x, (int) (-y)));
    }
    
    return result;
}

First factor:
5
Second factor:
3
15