Lab 3 (C Programming) Solution

Feb 16, 2019

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

3) Given the three numbers a(=8), b(=4),c and constant value PI=3.1415, calculate and display the following result using macros (preprocessor directives).

a) c = PI * mult(a,b) /* the macro mult(a,b) perform the multiplication of a & b*/

b) c = PI * sum(a,b) /* the macro sum(a,b) perform the addition of a & b*/

c) c = PI * sub(a,b) /* the macro sub(a,b) perform the subtraction of a & b*/

d) c = PI * div(a,b) /* the macro div(a,b) perform the division of a & b*/Source Code:

 

#include <stdio.h>

#define PI 3.1416
#define mult(a,b) a * b
#define sum(a,b) a + b
#define sub(a,b) a - b
#define div(a,b) a / b

int main()
{
    int  a = 8, b = 4;
    float mul = PI * mult(a, b);
    float summ = PI * sum(a, b);
    float subb = PI * sub(a, b);
    float divv = PI * div(a, b);
    printf("mul = %f\n", mul);
    printf("sum = %f\n", summ);
    printf("sub = %f\n", subb);
    printf("div = %f\n", divv);

    return 0;
} 

 

Output:

mul = 100.531197
sum = 29.132799
sub = 21.132799
div = 6.283200

 

 

4) Write a program to add two numbers (5 and 7) and display its sum.

Source Code(using ASCII):

#include <stdio.h>

int main()
{
    char ch;
    printf("Enter the character :");
    scanf(" %ch", &ch);
    if(ch >= 48 && ch <=57){
        printf("It is Number.");
    }else if((ch >= 65 && ch <= 90) || (ch <= 97 && ch >= 122){
        printf("It is Alphabet.");
    }else{
        printf("It is special.");
    }
    return 0;
}

 

Output:

Enter the character :D
It is Alphabet.

 

Source Code(using builtin functions):

#include <stdio.h>
#include <ctype.h>

int main()
{
    char ch;
    printf("Enter the character:");
    scanf(" %c", &ch);
    if(isalnum(ch)){
        printf("It is alphanumberic.\n");
    }
    if(isblank(ch)){
        printf("Its is blank character.\n");
    }
    if(isalpha(ch)){
        printf("It is alphabet.\n");
    }
    if(iscntrl(ch)){
        printf("It is control character.\n");
    }
    if(isdigit(ch)){
        printf("It is digit.\n");
    }
    if(isupper(ch)){
        printf("It is Uppercase.\n");
    }
    return 0;
} 

Output:

Enter the character:D
It is alphanumberic.
It is alphabet.
It is Uppercase.