Lab 6 (C Programming) Solution

Feb 22, 2019

 programs written here are compiled and run in gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0 

1)Write a program to add, subtract, multiply and divide two integers using user defined type function with return type.

 

Source Code: 

#include <stdio.h>
/* function prototype */
int add(int, int);
int substract(int, int);
int multiply(int, int);
int divide(int, int);

int main()
{
    int a = 20, b = 80;
    printf("SUM = %d\n", add(a, b));
    printf("DIFFERENCE = %d\n", substract(a, b));
    printf("MULTIPLICATION = %d\n", multiply(a, b));
    printf("DIVISION = %d\n", divide(a, b));
    return 0;
}

/* function definations */
int add(int x, int y){
    return x + y;
}

int substract(int x, int y){
    return y - x;
}

int multiply(int x, int y){
    return x * y;
}

int divide(int x, int y){
    /* here integer division happens and fractional part is lost*/
    return y/x;
}
 

 

Output:

SUM = 100
DIFFERENCE = 60
MULTIPLICATION = 1600
DIVISION = 4

 

 

2)Write a program to find the sum of first 60 natural numbers using recursive function.

 

Source Code: 

#include <stdio.h>
#define N 60

int nat_sum(int n);

int main()
{
    printf("SUM = %d", nat_sum(N));
    return 0;
}

int nat_sum(int n)
{
    if(n == 1){
        return 1;
    }else{
        return n + nat_sum(n - 1);
    }
}

 

 

Output:

SUM = 1830 

 

3)Define a function fact() to calculate the factorial of a number n and then write a program that uses this function fact() to calculate permutation and combination.

 

Source Code: 

#include <stdio.h>

int fact(int n);

int main()
{
    int n, r, perm, comb;
    printf("Enter the n:");
    scanf("%d", &n);
    printf("Enter the r:");
    scanf("%d", &r);
    perm = fact(n)/fact(n - r);
    comb = fact(n)/(fact(n - r) * fact(r));
    printf("Permutation = %d\n", perm);
    printf("Combination = %d\n", comb);

    return 0;
}

int fact(int n)
{
    if(n == 0){
        return 1;
    }else{
        return n * fact(n - 1);
    }
}

 

 

Output:

Enter the n:12
Enter the r:4
Permutation = 11880
Combination = 495

 

  

4)Write a program to check if the integer number entered by the user is prime or not using function.

 

Source Code: 

#include <stdio.h>
#include <stdlib.h>

int checkPrime(int n);

int main(){
    int number;
    printf("Enter the positive number:");
    scanf("%d", &number);
    if(number == 1){
        printf("Neither\n");
        exit(0);
    }
    if(checkPrime(number)){
        printf("It is PRIME.\n");
    }else{
        printf("It is NOT PRIME.\n");
    }
    return 0;
}

int checkPrime(int n){
    int i;
    for(i = 2; i <= n/2; i++){
        if(n % i == 0){
            return 0;
        }
    }
    return 1;

}

 

 

Output:

Enter the positive number:60
It is NOT PRIME.

 

 

 

5)Write a recursive function to generate fibonacci series 

Source Code: 

#include <stdio.h>
int fibo(int n);

int main()
{
    int number, i;
    printf("Enter the nunber of fibonacci number:");
    scanf("%d", &number);
    for(i = 0; i < number; i++){
        printf("%d, ", fibo(i));
    }
    return 0;
}

int fibo(int n){
    if(n == 0){
        return 0;
    }else if(n == 1){
        return 1;
    }else{
        return fibo(n - 1) + fibo(n - 2);
    }
} 

 

Output:

Enter the nunber of fibonacci number:30
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229,  

 

 

6)Write a program to illustrate the use of static variable.

 

Source Code: 

#include <stdio.h>

void show_call_number();

int main()
{
    int i;
    for(i = 0; i < 10; i++){
        show_call_number();
    }
    return 0;
}

void show_call_number(){
    static int counter = 0;
    counter++;
    printf("Function call number = %d\n", counter);
} 

 

 

Output:

Function call number = 1
Function call number = 2
Function call number = 3
Function call number = 4
Function call number = 5
Function call number = 6
Function call number = 7
Function call number = 8
Function call number = 9
Function call number = 10