C Programming Lab Manual – 36

Write a C program to calculate a Quadratic equation.

Function Main
    Declare Integer A
    Declare Integer B
    Declare Integer C
    Declare Integer Delta
    Declare Real X1
    Declare Real X2
    Output "Enter coefficient of X^2:"
    Input A
    Output "Enter coefficient of X:"
    Input B
    Output "Enter known term:"
    Input C
    If A = 0
        Output "Equarion:"
        Assign X1 = -C / B
        Output "The solution is X = " & X1
    Else
        Assign Delta = B^2 - 4*A*C
        If Delta < 0
            Output "There are no real solutions"
        Else
            If Delta = 0
                Assign X1 = -B / (2*A)
                Output "Two real and coincident solutions"
                Output "X1 = " & X1
                Output "X2 = " & X1
            Else
                Assign X1 = -B - SQRT(Delta) / (2*A)
                Assign X2 = -B + SQRT(Delta) / (2*A)
                Output "Two real and distinct solutions"
                Output "X1 = " & X1
                Output "X2 = " & X2
            End
        End
    End
End

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

int main() {
    int A;
    int B;
    int C;
    int Delta;
    double X1;
    double X2;
    printf("Enter coefficient of X^2:\n");
    scanf("%d", &A);
    printf("Enter coefficient of X:\n");
    scanf("%d", &B);
    printf("Enter known term:\n");
    scanf("%d", &C);
    if (A== 0) {
        printf("Equation:\n");
        X1 = -C / (double)B;
        printf("The solution is X = %lf\n", X1);
    } else {
        Delta = (int)(pow(B, 2) - 4 * A * C);
        if (Delta < 0) {
            printf("There are no real solutions\n");
        } else {
            if (Delta == 0) {
                X1 = -B / (2 * (double)A);
                printf("Two real and coincident solutions\n");
                printf("X1 = %lf\n", X1);
                printf("X2 = %lf\n", X1);
            } else {
                X1 = (-B - sqrt(Delta)) / (2 * (double)A);
                X2 = (-B + sqrt(Delta)) / (2 * (double)A);
                printf("Two real and distinct solutions\n");
                printf("X1 = %lf\n", X1);
                printf("X2 = %lf\n", X2);
            }
        }
    }
    return 0;
}

Enter coefficient of X^2:
5
Enter coefficient of X:
10
Enter known term:
5
Two real and coincident solutions
X1 = -1.000000
X2 = -1.000000

C Programming Lab Manual – 35

Write a C program to convert a given number of days into years, weeks, and days.

Function Main
    Declare Integer y
    Declare Integer w
    Declare Integer d
    Declare Integer a
    Output "Enter total number of days:"
    Input d
    Assign y = d / 365
    Assign a = d MOD 365
    Assign w = a / 7
    Assign d = a MOD 7
    Output "Years:" & ToString(y) & ToChar(10) & ToChar(13) & "Weeks: " & ToString(w) & ToChar(10) & ToChar(13) & "Days: " & ToString(d)
End

#include <stdio.h>

int main() {
    int y;
    int w;
    int d;
    int a;
    printf("Enter total number of days:\n");
    scanf("%d", &d);
    y = (int) ((double) d / 365);
    a = d % 365;
    w = (int) ((double) a / 7);
    d = a % 7;
    printf("Years: %d\nWeeks: %d\nDays: %d\n", y, w, d);
    return 0;
}

Enter total number of days:
400
Years: 1                                                                        
Weeks: 5                                                                        
Days: 0   

C Programming Lab Manual – 34

Write a C program to find all divisors of a number.

Function Main
    Declare Integer n
    Declare Integer k
    Declare Integer r
    Output "Insert a number: "
    Input n
    Assign k = 1
    Output "All divisor of the number: "
    While k <= n
        Assign r = n mod k
        If r = 0
            Output k
        End
        Assign k = k + 1
    End
End

#include <stdio.h>

int main() {
    int n;
    int k;
    int r;
    printf("Insert a number: \n");
    scanf("%d", &n);
    k = 1;
    printf("All divisor of the number: \n");
    while (k <= n) {
        r = n % k;
        if (r == 0) {
            printf("%d\n", k);
        }
        k = k + 1;
    }
    return 0;
}

Insert a number:
15
All divisor of the number:
1
3
5
15

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