C Programming Lab Manual – 40

Write a C program to Delete duplicate elements in an array.

Function Main
    Declare Integer ssize, i, j, k
    Output "Enter the total number of elements:"
    Input ssize
    Declare Integer Array arr[ssize]
    Output "Enter the elements of list:"
    For i = 0 to ssize - 1
        Output "Element n" & ToString(i + 1)
        Input arr[i]
    End
    Call OrderArrayInt(arr)
    Output "Array list after deleted duplicate elements:"
    For i = 0 to ssize - 1
        Assign j = i + 1
        While j < ssize
            If arr[j] = arr[i]
                For k = j to (ssize - 1) - 1
                    Assign arr[k] = arr[k + 1]
                End
                Assign ssize = ssize - 1
            Else
                Assign j = j + 1
            End
        End
    End
    For i = 0 to ssize - 1
        Output arr[i]
    End
End

Function OrderArrayInt (Integer Array a)
    Declare Integer n, j, i, tmp
    Assign n = size(a)
    For i = 0 to n - 1
        Assign j = 0
        For j = i + 1 to n - 1
            If a[j] < a[i]
                Assign tmp = a[j]
                Assign a[j] = a[i]
                Assign a[i] = tmp
            End
        End
    End
End

#include <stdio.h> 

void orderArrayInt(int[], int);

int main() {
    //!showArray(arr)
    int ssize, i, j, k;
    printf("Enter the total number of elements:\n");
    scanf("%d", &ssize);
    int arr[ssize];
    printf("Enter the elements of list:\n");
    for (i = 0; i <= ssize - 1; i++) {
        printf("Element n%d:", i + 1);
        scanf("%d", &arr[i]);
    }
    orderArrayInt(arr, ssize);
    printf("Array list after deleted duplicate elements:\n");
    for (i = 0; i <= ssize - 1; i++) {
        j = i + 1;
        while (j < ssize) {
            if (arr[j] == arr[i]) {
                for (k = j; k <= ssize - 1 - 1; k++) {
                    arr[k] = arr[k + 1];
                }
                ssize = ssize - 1;
            } else {
                j = j + 1;
            }
        }
    }
    for (i = 0; i <= ssize - 1; i++) {
        printf("%d\n", arr[i]);
    }
    return 0;
}

void orderArrayInt(int a[], int n) {
    int j, i, tmp;
    for (i = 0; i <= n - 1; i++) {
        j = 0;
        for (j = i + 1; j <= n - 1; j++) {
            if (a[j] < a[i]) {
                tmp = a[j];
                a[j] = a[i];
                a[i] = tmp;
            }
        }
    }
} 

Enter the total number of elements:
5
Enter the elements of list:
Element n1:1
Element n2:1
Element n3:2
Element n4:2
Element n5:3
Array list after deleted duplicate elements:
1
2
3

C Programming Lab Manual – 39

Write a C program for Reversing Array Elements using function.

Function Main
    Declare Integer ssize
    Declare Integer i
    Output "Enter the total number of elements:"
    Input ssize
    Declare Integer Array arr[ssize]
    Output "Enter the elements of list:"
    For i = 0 to ssize - 1
        Output " Element n.:" & ToString(i + 1)
        Input arr[i]
    End
    Call InverseArrInt(arr)
    Output "Result after reversal:"
    For i = 0 to ssize - 1
        Output arr[i]
    End
End

Function InverseArrInt (Integer Array v)
    Declare Integer i
    Declare Integer j
    Declare Integer temp
    Assign j = size(v) - 1
    Assign i = 0
    While i < j
        Assign temp = v[i]
        Assign v[i] = v[j]
        Assign v[j] = temp
        Assign i = i + 1
        Assign j = j - 1
    End
End

#include <stdio.h>

void inverseArrInt(int v[], int size);

int main() {
    //!showArray(arr)
    int ssize;
    int i;
    printf("Enter the total number of elements:\n");
    scanf("%d", &ssize);
    int arr[ssize];
    printf("Enter the elements of list:\n");
    for (i = 0; i <= ssize - 1; i++) {
        printf("Element n%d:", i + 1);
        scanf("%d", &arr[i]);
    }
    inverseArrInt(arr, ssize);
    printf("Result after reversal:\n");
    for (i = 0; i <= ssize - 1; i++) {
        printf("%d\n", arr[i]);
    }
    return 0;
}

void inverseArrInt(int v[], int size) {
    int i;
    int j;
    int temp;
    j = size - 1;
    i = 0;
    while (i < j) {
        temp = v[i];
        v[i] = v[j];
        v[j] = temp;
        i = i + 1;
        j = j - 1;
    }
}

Enter the total number of elements:
5
Enter the elements of list:
Element n1:1
Element n2:2
Element n3:3
Element n4:4
Element n5:5
Result after reversal:
5
4
3
2
1

C Programming Lab Manual – 38

Write a C program to Calculate the Square root of a number.

Function Main
    Declare Integer Num
    Declare Real X
    Output "Enter a number: "
    Input Num
    If Num > 1
        Assign X = Num
    Else
        Assign X = 1
    End
    While X * X > Num
        Assign X = (1 / 2) * ( X + ( Num / X ) )
    End
    Output "The square root of the number " & Num & " is: " & X
End

#include <stdio.h>

int main() {
    int Num;
    double X;
    printf("Enter a number: \n");
    scanf("%d", &Num);
    if (Num > 1) {
        X = Num;
    } else {
        X = 1;
    }
    while (X * X > Num) {
        X = (double) 1 / 2 * (X + Num / X);
    }
    printf("The square root of the number %d is: %f\n", Num, X);
    return 0;
}

Enter a number: 
10
The square root of the number 10 is: 3.162278

C Programming Lab Manual – 37

Write a C program to Display the first N prime numbers.

Function Main
    Declare Integer N, Num, Cont, Idx, Div
    Output "How many prime numbers do you want to find?:"
    Assign Cont = 0
    Assign Num = 2
    Input N
    While Cont < N
        Assign Idx = 1
        Assign Div = 0
        While Idx <= Num
            If Num MOD Idx = 0
                Assign Div = Div + 1
            End
            Assign Idx = Idx + 1
        End
        If Div = 2
            Output " " &Num
            Assign Cont = Cont + 1
        End
        Assign Num = Num + 1
    End
End

#include <stdio.h>

int main() {
    int N, Num, Cont, Idx, Div;
    printf("How many prime numbers do you want to find?:\n");
    Cont = 0;
    Num = 2;
    scanf("%d", &N);
    while (Cont < N) {
        Idx = 1;
        Div = 0;
        while (Idx <= Num) {
            if (Num % Idx == 0) {
                Div = Div + 1;
            }
            Idx = Idx + 1;
        }
        if (Div == 2) {
            printf(" %d", Num);
            Cont = Cont + 1;
        }
        Num = Num + 1;
    }
    return 0;
}

How many prime numbers do you want to find?:
10
 2 3 5 7 11 13 17 19 23 29