C Programming Lab Manual – 27

Write a C program to add two numbers using call by value and reference.

Function Main
    Declare Integer n1, n2
    Output "This is a call by value add(2, 30):"
    Output add(2, 3)
    Output "Input first number (n1):"
    Input n1
    Output "Input second number(n2):"
    Input n2
    Output "This is a call by reference add(n1, n2):"
    Output add(n1, n2)
End

Function add (Integer first, Integer second)
    Declare Integer result
    Assign result = first + second
Return Integer result

#include <stdio.h>

int add(int first, int second);

int main() {
    int n1, n2;
    printf("This is a call by value add(2, 3):\n");
    printf("%d\n", add(2, 3));
    printf("Input first number(n1):\n");
    scanf("%d", &n1);
    printf("Input second number(n2):\n");
    scanf("%d", &n2);
    printf("This is a call by reference add(n1, n2):\n");
    printf("%d\n", add(n1, n2));
    return 0;
}

int add(int first, int second) {
    int result;
    result = first + second;
    
    return result;
}

This is a call by value add(2, 3):
5
Input first number(n1):
20
Input second number(n2):
30
This is a call by reference add(n1, n2):
50

C Programming Lab Manual – 26

Write a C program to angular conversion degree and radian.

Function Main
    Declare Real turns
    Output "Input number"
    Input turns
    Output ToString(deg2rad(turns)) & " radians"
    Output ToString(rad2deg(turns)) & " grades"
End

Function deg2rad (Real x)
    Declare Real r
    Assign r = x*pi/180
Return Real r

Function rad2deg (Integer x)
    Declare Real r
    Assign r = 180*x/pi
Return Real r

#include <stdio.h>
#include <math.h>

double deg2rad(double x);
double rad2deg(int x);

int main() {
    double turns;
    printf("Input number:\n");
    scanf("%lf", &turns);
    printf("%lf radians\n", deg2rad(turns));
    printf("%lf grades\n", rad2deg((int) turns));
    return 0;
}

double deg2rad(double x) {
    double r;
    r = x * M_PI / 180;
    return r;
}

double rad2deg(int x) {
    double r;
    r = 180 * x / M_PI;
    return r;
}

Input number:
50
0.872665 radians
2864.788976 grades

C Programming Lab Manual – 25

Write a C program to manipulate values of variables using Pointer.

Declare integer variable m
Declare integer pointer variable ab

Assign 29 to m
Print the address of m and its value
Print the address of ab and the content it points to

Assign the address of m to ab
Print the address of ab and the content it points to

Assign 34 to m
Print the address of ab and the content it points to

Assign 7 to the content of ab
Print the value of m

#include <stdio.h>
int main()
{
  //! showMemory()
   int* ab;
   int m;
   m=29;
   printf(" Address of m : %p\n",&m);
   printf(" Value of m : %d\n\n",m);
   ab=&m;
   printf(" Address of pointer ab : %p\n",ab);
   printf(" Content of pointer ab : %d\n\n",*ab);
   m=34;
   printf(" Address of pointer ab : %p\n",ab);
   printf(" Content of pointer ab : %d\n\n",*ab);
   *ab=7;
   printf(" Value of m : %d\n\n",m);
   return 0;
}

Address of m : 0x7ffeeed0d9ec
Value of m : 29

Address of pointer ab : 0x7ffeeed0d9ec
Content of pointer ab : 29

Address of pointer ab : 0x7ffeeed0d9ec
Content of pointer ab : 34

Value of m : 7
Note: In C Programming Manual Tracing, 

Address of pointer ab : fff8

C Programming Lab Manual – 24

Write a C program to get a string using Array.

Function Main
    Declare String Array str[20]
    Input str
    Output ""&str
End

#include <stdio.h>
int main() {
    //! showArray(str)
    char str[20];
    gets(str);
    printf("[%s]\n", str);
    return 0;
}

Hi, My name is Anis
[Hi, My name is Anis]