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]

C Programming Lab Manual – 23

Write a C program to Control Statement Structure.

Function Main
    Declare Integer k
    Assign k = 1
    If k=1
        Output "t"
    End
    If k=0
        Output "F"
    Else
        Output "!f"
    End
    For k = 0 to 3
        Output ""&k
    End
    While k<5
        Output ""&k
        Assign k = k+1
    End
    Loop
        Assign k = k+1
        Output ""&k
    Do k<7
End

#include <stdio.h>
int main() {
  int k;
  if (1) {
    printf("t");
  }
  if (0) {
    printf("F");
  } else {
    printf("!f");
  }
  for (k = 0; k <= 3; k++) {
    printf("%i", k);
  }
  while (k < 5) {
    printf("%i", k);
    ++k;
  }
  do {
    printf("%i", k);
    k += 1;
  } while (k < 7);
}

t!f0123467

C Programming Lab Manual – 22

Write a C program to Create One Dimensional Array.

Function Main
    Declare Integer i, n
    
    Assign n = 12
    Declare Integer Array a[n]
    
    Assign a[0] = 1
    For i = 1 to n-1
        Assign a[i] = a[i-1] * 2
    End
    For i = 0 to n-1
        Output "a["&i&"] = " & a[i]
    End
End

#include <stdio.h>
int main() {
    //! showArray(a, cursors=[i,n], n=8, cw=32)
    int i, n = 12;
    int a[n];
    a[0] = 1;
    for (i = 1; i < n; i++) {
        a[i] = a[i - 1] * 2;
    }
    for (i = 0; i < n; i++) {
        printf("a[%i] = %i\n", i, a[i]);
    }
}

a[0] = 1
a[1] = 2
a[2] = 4
a[3] = 8
a[4] = 16
a[5] = 32
a[6] = 64
a[7] = 128
a[8] = 256
a[9] = 512
a[10] =1024
a[11] =2048

C Programming Lab Manual – 21

Write a C program to Create an Array of input and output.

Function Main
    Declare Integer Array values[5]
    Declare Integer i
    
    Output "Enter 5 integers: "
    For i = 0 to 4
        Input values[i]
    End
    Output "Show the Array values: "
    For i = 0 to 4
        Output values[i]
    End
End

#include <stdio.h>
int main() {
//! showArray(values)

int values[5];

  printf("Enter 5 integers:\n ");

 for(int i = 0; i<=4; ++i) {
     scanf("%d\n", &values[i]);
  }

  printf("Show the Array values:\n ");
  for(int i = 0; i<=4; ++i) {
     printf("%d\n", values[i]);
  }
  return 0;
} 
Enter 5 integers: 
1
2
3
4
5
Show the Array values: 
1
2
3
4
5

C Programming Lab Manual – 20

Write a C program to Check Leap Year.

Function Main
    Declare Integer year
    
    Output "Enter a year:"
    Input year
    If (year%100 == 0 && year%400 == 0) || (year%100 != 0 && year%4   == 0)
        Output year & " is a leap year"
    Else
        Output year & " is a leap year"
    End
End

#include <stdio.h>  
  
int main()  
{  
    int year;  
  
    printf("Enter a year:");  
    scanf("%d", &year);  
  
    if( (year%100 == 0 && year%400 == 0) ||  
        (year%100 != 0 && year%4   == 0)   )  
    {  
        printf("%d is a leap year\n", year);  
    }  
    else  
    {  
        printf("%d is not a leap year\n", year);  
    }  
  
    return 0;  
} 
Enter a year:2020
2020 is a leap year

C Programming Lab Manual – 19

Write a C program to Check a Palindrome Number.

Function Main
    Declare Integer n, reversed, remainder, original
    
    Assign reversed = 0
    Output "Enter a number:"
    Input n
    Assign original = n
    While n!=0
        Assign remainder = n%10
        Assign reversed = reversed * 10 + remainder
        Assign n = n/10
    End
    If original == reversed
        Output original&  " is a palimdrome number"
    Else
        Output original&  " is not a palimdrome number"
    End
End

#include <stdio.h>
int main() {
  int n, reversed = 0, remainder, original;
    printf("Enter a number: ");
    scanf("%d", &n);
    original = n;

    while (n != 0) {
        remainder = n % 10;
        reversed = reversed * 10 + remainder;
        n /= 10;
    }

    if (original == reversed)
        printf("%d is a palindrome.", original);
    else
        printf("%d is not a palindrome.", original);

    return 0;
}
Enter a number: 1001
1001 is a palindrome.

C Programming Lab Manual – 18

Write a C program to Reverse a Number.

Function Main
    Declare Integer n, reverse, remainder
    
    Assign reverse = 0
    Output "Enter a number:"
    Input n
    While n!=0
        Assign remainder = n%10
        Assign reverse = reverse * 10 + remainder
        Assign n = n/10
    End
    Output "The reverse number =" & reverse
End

#include <stdio.h>

int main() {

  int n, reverse = 0, remainder;

  printf("Enter a number: ");
  scanf("%d", &n);

  while (n != 0) {
    remainder = n % 10;
    reverse = reverse * 10 + remainder;
    n /= 10;
  }

  printf("Reversed number = %d", reverse);

  return 0;
}
Enter a number: 2345
Reversed number = 5432

C Programming Lab Manual – 17

Write a C program to Generate Multiplication Table

Function Main
    Declare Integer n
    Declare Integer i
    Output "Enter a number: "
    Input n
    For i = 1 to 10
        Output "" & n & "*"  & i  & "=" &n*i
    End
End

#include <stdio.h>
int main() {
  int n, i;
  printf("Enter a number: ");
  scanf("%d", &n);
  for (i = 1; i <= 10; ++i) {
    printf("%d * %d = %d \n", n, i, n * i);
  }
  return 0;
}

Enter a number: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90