C Programming Lab Manual – 33

Write a C program to convert Binary to Decimal Numbers.

Function Main
    Declare Integer num
    Output "Enter the Binary number: "
    Input num
    Output "The decimal number is:"
    Output BinaryToDecimal(num)
End

Function BinaryToDecimal (Integer num)
    Declare Integer rem
    Declare Integer sum
    Declare Integer power
    Assign sum = 0
    Assign power = 0
    While num > 0
        Assign rem = num MOD 10
        Assign num = num / 10
        Assign sum = sum + rem * POW(2, power)
        Assign power = power + 1
    End
Return Integer sum

Function POW (Integer Base, Integer Exponent)
    Declare Integer Result
    If Exponent = 0
        Assign Result = 1
    Else
        Assign Result = Base * POW(Base, Exponent -1)
    End
Return Integer Result

#include <stdio.h>

int BinaryToDecimal(int num);
int POW(int Base, int Exponent);

int main() {
    int num;
    printf("Enter the Binary number: \n");
    scanf("%d", &num);
    printf("The Decimal number is:\n");
    printf("%d\n", BinaryToDecimal(num));
    return 0;
}

int BinaryToDecimal(int num) {
    int rem;
    int sum;
    int power;
    sum = 0;
    power = 0;
    while (num > 0) {
        rem = num % 10;
        num = (int) ((double) num / 10);
        sum = sum + rem * POW(2, power);
        power = power + 1;
    }
    return sum;
}

int POW(int Base, int Exponent) {
    int Result;
    if (Exponent == 0) {
        Result = 1;
    } else {
        Result = Base * POW(Base, Exponent - 1);
    }
    return Result;
}

Enter the Binary number: 
101
The Decimal number is:
5

C Programming Lab Manual – 32

Write a C program to display a pattern like a diamond.

Function Main
    Declare Integer i, j, r
    Output "Input number of rows : "
    Input r
    For i = 0 to r
        For j = 1 to r-i
            Output " "
        End
        For j = 1 to 2*i-1
            Output "*"
        End
        Output ""
    End
    For i = r-1 to 1 decreasing
        For j = 1 to r-i
            Output " "
        End
        For j = 1 to 2*i-1
            Output "*"
        End
        Output ""
    End
End

#include <stdio.h>

int main()
{
   int i,j,r;
   printf("Input number of rows: ");
   scanf("%d",&r);
   for(i=0;i<=r;i++)
   {
     for(j=1;j<=r-i;j++)
     printf(" ");
     for(j=1;j<=2*i-1;j++)
       printf("*");
     printf("\n");
   }
 
   for(i=r-1;i>=1;i--)
   {
     for(j=1;j<=r-i;j++)
     printf(" ");
     for(j=1;j<=2*i-1;j++)
       printf("*");
     printf("\n");
   }
 return 0;
}

Input number of rows: 5                                                                 
                                                                                                              
    *                                                                                                         
   ***                                                                                                        
  *****                                                                                                       
 *******                                                                                                      
*********                                                                                                     
 *******                                                                                                      
  *****                                                                                                       
   ***                                                                                                        
    *

C Programming Lab Manual – 31

Write a C program to make such a pattern as a pyramid with an asterisk.

Function Main
    Declare Integer i, j, space, rows, k
    Output "Input number of rows : "
    Input rows
    Assign space = rows+4-1
    For i = 1 to rows
        For k = space to 1 decreasing
            Output " "
        End
        For j = 1 to i
            Output "* "
        End
        Output ""
        Assign space = space -1
    End
End

#include <stdio.h>
int main()
{
   int i,j,space,rows,k;
   printf("Input number of rows : ");
   scanf("%d",&rows);
   space=rows+4-1;
   for(i=1;i<=rows;i++)
   {
         for(k=space;k>=1;k--)
            {
              printf(" ");
            }
                      
	   for(j=1;j<=i;j++)
	   printf("* ");
	printf("\n");
    space--;
   }
 return 0;
}

Input number of rows  : 4                                                                                     
       *                                                                                                      
      * *                                                                                                     
     * * *                                                                                                    
    * * * * 

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

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]