Lab 2 (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 

1) Write a program to to prompt the user to input one floating, one integer and one character variable and display these values in forward and reverse order

Source Code:

 

 #include <stdio.h>

int main()
{
    float f_num;
    int i_num;
    char ch;
    printf("Enter the floating number :");
    scanf(" %f", &f_num);
    printf("Enter the Integer number :");
    scanf(" %d", &i_num);
    printf("Enter the Character number :");
    scanf(" %c", &ch);
    printf("Forward Order: %f, %d, %c\n", f_num, i_num, ch);
    printf("Reverse Order: %c, %d, %f\n", ch, i_num, f_num);
    return 0;
}

 

Output:

Enter the floating number :12.4
Enter the Integer number :23
Enter the Character number :a
Forward Order: 12.400000, 23, a
Reverse Order: a, 23, 12.400000

 

 

2) Write a program to compute the Compound Interest. (Use proper functions from math.h libray)

Source Code:

//program to compute the compound interest
#include <stdio.h>
#include <math.h>

int main()
{
    float p, t, r, c_i;
    printf("Enter principle, time and rate:");
    scanf(" %f %f %f", &p, &t, &r);
    c_i = p *(pow(1 + r/100, t)) - p;
    printf("Compound Interest = %f", c_i);
    return 0;
}

 

Output:

Enter principle, time and rate:2300 3 12
Compound Interest = 931.334412

 

 

3) Write a program to print the size of char, int, float, double and long double data types in C. Discuss, if the data size differs from your expectation.Source Code:

#include <stdio.h>

int main()
{
    printf("Size of char : %lu\n", sizeof(char));
    printf("Size of int : %lu\n", sizeof(int));
    printf("Size of float : %lu\n", sizeof(float));
    printf("Size of long : %lu\n", sizeof(long));
    printf("Size of long double : %lu\n", sizeof(long double));
    return 0;
}

 

Output:

Size of char : 1
Size of int : 4
Size of float : 4
Size of long : 8
Size of long double : 16

 

4) Write a program to swap two variables values with and without using third variable.

Source Code(using third variable):

//program to swap two variables using third variable
#include <stdio.h>

int main()
{
    int a, b, temp;
    printf("Enter value of a:");
    scanf("%d", &a);
    printf("Enter value of b:");
    scanf("%d", &b);
    printf("Before swapping a = %d, b = %d\n", a, b);

    //swapping
    temp = a;
    a = b;
    b = temp;

    printf("After swapping a = %d, b = %d", a, b);
    return 0;
}

Output:

Enter value of a:23
Enter value of b:45
Before swapping a = 23, b = 45
After swapping a = 45, b = 23 

 

Source Code(without using third variable):

//swapping without using third variable
#include <stdio.h>

int main()
{
    int a = 40, b = 100;
    printf("Before swapping a = %d, b = %d\n", a, b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("After swapping a = %d, b = %d\n", a, b);
    return 0;
}

Output:

Enter value of a:34
Enter value of b:66
Before swapping a = 34, b = 66
After swapping a = 66, b = 34

 

 

5) Write a program to check if the given integer number is even using ...i)modulus operator ii)bitwise operator.

Source Code(modulus operator):

//program to check even number using modulus operator
#include <stdio.h>

int main()
{
    int number;
    printf("Enter the number to check:");
    scanf("%d", &number);
    if(number % 2 == 0){
        printf("The entered number is even:");
    }else{
        printf("The entered number is odd:");
    }
    return 0;
}

Output:

Enter the number to check:34
The entered number is even 

 

Source Code(bitwise operator):

//program to check even number using biswise operator
#include <stdio.h>

int main()
{
    int number;
    printf("Enter the number to check:");
    scanf("%d", &number);
    if(number & 1 == 1){
        printf("The entered number is odd:");
    }else{
        printf("The entered number is even:");
    }
    return 0;
}

Output:

Enter the number to check:55
The entered number is odd: 

 

6)Write a program to find the greatest of two integers entered by the user using conditional operator, Also find the average in the form of floating point format.(optional-try for the greatest of the three numbers using conditional operator)

Source Code(greatest among two):

//program to find the greatest of two integers
#include <stdio.h>

int main()
{
    int a, b, greatest;
    float average;
    printf("Enter the two numbers :");
    scanf(" %d %d", &a, &b);
    greatest = (a > b)?a:b;
    average = (a + b)/2.0;
    printf("Greatest is :%d\n", greatest);
    printf("Average is :%f\n", average);
    return 0;
}

Output:

Enter the two numbers :34 66
Greatest is :66
Average is :50.000000

 

Source Code(greatest among three):

//program to find the greatest of three integers
#include <stdio.h>

int main()
{
    int a, b, c, greatest;
    printf("Enter the three numbers :");
    scanf(" %d %d %d", &a, &b, &c);
    greatest = (a > b)?((a > c)?a:c):((b > c)?b:c);
    printf("Greatest is :%d\n", greatest);
    return 0;
}

Output:

Enter the three numbers :23
89
55
Greatest is :89

 

 

7)Write the following expressions in programs individually, and discuss on the result. Display the value of y and z for each expression with a = 10, b = 5(only source-codes)

Source Code(7i):

//programs for question 7(i)

#include <stdio.h>

int main()
{
    int a = 10, b = 5;
    int z = a++ + ++a;
    int y = a-- + --a;
    printf("a++ + ++a=%d\n",z);
    printf("a-- + --a=%d",y);
    return 0;
}

Output:

a++ + ++a=22
a-- + --a=22 

 

Source Code(7ii):

//programs for question 7(ii)
#include <stdio.h>

int main()
{
    int a = 10, b = 5;
    int z = ++a + ++a;
    int y = a-- + a--;
    printf("++a + ++a=%d\n",z);
    printf("a-- + a--=%d",y);
    return 0;
}

Output:

++a + ++a=24
a-- + a--=23 

 

Source Code(7iii):

//programs for question 7(iii)
#include <stdio.h>

int main()
{
    int a = 10, b = 5;
    int z = ++a + ++a + --a;
    printf("++a + ++a + --a=%d\n",z);
    return 0;
}

Output:

++a + ++a + --a=35  

 

Source Code(7iv):

//programs for question 7(iv)
#include <stdio.h>

int main()
{
    int a = 10, b = 5;
    int z = a & b, y = a && b;
    printf("a&b=%d\n",z);
    printf("a&&b=%d\n",y);
    return 0;
}

Output:

a&b=0 a&&b=1  

 

Source Code(7v):

//programs for question 7(v)
#include <stdio.h>

int main()
{
    int a = 10, b = 5;
    int z = a | b, y = a || b;
    printf("a|b=%d\n",z);
    printf("a||b=%d\n",y);
    return 0;
}

Output:

a|b=15 a||b=1  

 

Source Code(7vi):

//programs for question 7(vi)
#include <stdio.h>

int main()
{
    int a = 10, b = 5;
    int z =  a > 10 || b < 5, y = a > 10 && b < 5;
    printf("z=%d\n",z);
    printf("y=%d\n",y);
    return 0;
}

Output:

z=0 y=0  

 

Source Code(7vii):

//programs for question 7(vii)
#include <stdio.h>

int main()
{
    int a = 10, b = 5;
    int z =  a == 10, y = a != 4;
    printf("z=%d\n",z);
    printf("y=%d\n",y);
    return 0;
}

Output:

z=1 y=1   

 

Source Code(7viii):

//programs for question 7(viii)
#include <stdio.h>

int main()
{
    int a = 10, b = 5;
    int z = a %= 2, y =  b += 5;
    printf("z=%d\n",z);
    printf("y=%d\n",y);
    return 0;
}

Output:

z=0 y=10