C Exercise 2

Mannan Ul Haq
0

Objective:

After performing this exercise, you will be able to solve programming problems using:
  1. 'if' statements
  2. 'if-else' statement
  3. Nested 'if' structure
  4. 'while' loop
  5. 'do-while' loop
  6. 'for' loop

Activity 1:

Write a program to input electricity unit charges and calculate total electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill

Solution:

#include <stdio.h>

int main()
{
    int unit;
    float amt, total_amt, sur_charge;

    printf("Enter Total Units Consumed: \n");
    scanf("%d", &unit);

    if (unit <= 50)
    {
        amt = unit * 0.50;
    }
    else if (unit <= 150)
    {
        amt = 25 + ((unit - 50) * 0.75);
    }
    else if (unit <= 250)
    {
        amt = 100 + ((unit - 150) * 1.20);
    }
    else
    {
        amt = 220 + ((unit - 250) * 1.50);
    }

    sur_charge = amt * 0.20;
    total_amt = amt + sur_charge;

    printf("Electricity Bill = %.2f", total_amt);

    return 0;
}



Activity 2:

Write a program that calculates and displays a person’s body mass index (BMI). The BMI is often used to determine whether a person with a sedentary lifestyle is overweight or underweight for his or her height. A person’s BMI is calculated with the following formula:

BMI = weight x 703 / height?

Where weight is measured in pounds and height is measured in inches. The program should display a message indicating whether the person has optimal weight, is underweight, or is overweight. A person’s weight is considered to be optimal if his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered to be underweight. If the BMI value is greater than 25, the person is considered to be overweight.

Solution:

#include <stdio.h>

int main()
{
    double bmi;
    double weight;
    double height;
    double eq1;
    double eq2;

    printf("Enter Weight in pounds: \n");
    scanf("%lf", &weight);
    printf("Enter Height in inches: \n");
    scanf("%lf", &height);

    eq1 = weight * 703;
    eq2 = height * height;

    bmi = eq1 / eq2;
    printf("Your Body Mass Index = %.2lf\n", bmi);

    if (bmi > 25.0)
    {
        printf("The Person is Overweight!\n");
    }
    else
    {
        if (bmi <= 25.0 && bmi >= 18.5)
        {
            printf("The Person's Weight is Optimal!\n");
        }
        else if (bmi < 18.5)
        {
            printf("The Person is Underweight!\n");
        }
    }

    return 0;
}



Activity 3:

Take input of age of 3 people by the user and determine the oldest and youngest among them.

Solution:

#include<iostream>
using namespace std;

#include <stdio.h>

int main()
{
    int a, b, c;
    printf("Type the age of First Person: \n");
    scanf("%d", &a);
    printf("Type the age of Second Person: \n");
    scanf("%d", &b);
    printf("Type the age of Third Person: \n");
    scanf("%d", &c);

    if (a > b)
    {
        if (c < b)
        {
            printf("The First Person is Oldest\n");
            printf("The Third Person is Youngest\n");
        }
        else
        {
            printf("The First Person is Oldest\n");
            printf("The Second Person is Youngest\n");
        }
    }

    else if (b > a)
    {
        if (c > a)
        {
            printf("The Second Person is Oldest\n");
            printf("The Third Person is Youngest\n");
        }
        else
        {
            printf("The Second Person is Oldest\n");
            printf("The First Person is Youngest\n");
        }
    }

    else if ((c > a) && (c > b))
    {
        if (a > b)
        {
            printf("The Third Person is Oldest\n");
            printf("The First Person is Youngest\n");
        }
        else
        {
            printf("The Third Person is Oldest\n");
            printf("The First Person is Youngest\n");
        }
    }

    return 0;
}



Activity 4:

Write a program to find the eligibility of admission for a professional course based on the following criteria: Marks in Maths >=65 and Marks in Phy >=55 and Marks in Chem>=50 and Total in all three subject >=190 or Total in Maths and Physics >=140


Solution:

#include <stdio.h>

int main()
{
    int p, c, m, t, mp;

    printf("Input the marks obtained in Physics: ");
    scanf("%d", &p);
    printf("Input the marks obtained in Chemistry: ");
    scanf("%d", &c);
    printf("Input the marks obtained in Mathematics: ");
    scanf("%d", &m);
    printf("Total marks of Maths, Physics, and Chemistry:\n%d\n", m + p + c);
    printf("Total marks of Maths and Physics:\n%d\n", m + p);

    if (m >= 65)
    {
        if (p >= 55)
        {
            if (c >= 50)
            {
                if ((m + p + c) >= 190 || (m + p) >= 140)
                {
                    printf("The Candidate is eligible for admission.\n");
                }
                else
                    printf("The Candidate is not eligible.\n");
            }
            else
                printf("The Candidate is not eligible.\n");
        }
        else
            printf("The Candidate is not eligible.\n");
    }
    else
        printf("The Candidate is not eligible.\n");

    return 0;
}


Activity 5:

Print the Fibonacci sequence (0,1,1,2,3,5,8,13,21, 34,……..) till the N member. Take N from user input. Fibonacci sequence is given by formula: sum the last two numbers to get the current.

Solution:

#include <stdio.h>

int main()
{
    int n;

    printf("Enter the number up to which you want to print the Fibonacci Sequence: \n");
    scanf("%d", &n);

    int next = 0;
    int num = 0;
    int num1 = 1;
    printf("The Series is: - %d, %d, ", num, num1);
    next = num + num1;
    while (next <= n)
    {
        printf("%d, ", next);
        num = num1;
        num1 = next;
        next = num + num1;
    }

    return 0;
}



Activity 6: 

Write a program that prompts the user to enter the values. Get the input again and again until user enter -1 so the condition to break the loop is -1. Your task is to determine the largest and smallest value entered by the user.

Solution:

#include <stdio.h>
#include <stdbool.h>

int main()
{
    int n1, n2, large, small;
    printf("Enter Numbers: \n");
    scanf("%d", &n1);

    large = n1;
    small = n1;
    bool status = true;
    while (status == true)
    {
        scanf("%d", &n2);
        if (n2 > large)
            large = n2;
        else if (n2 < small && n2 >= 0)
            small = n2;
        if (n2 == -1)
            status = false;
    }

    printf("The Largest Number is %d\n", large);
    printf("The Smallest Number is %d\n", small);

    return 0;
}



Activity 7: 

Write a Program to Calculate the Factorial of a Positive Integer. The program repeats the process until the user enters 0.

Solution:

#include <stdio.h>

int main()
{
    int n, a, i = 1;
    double fact;

    do {
        fact = 1;

        printf("Enter a Positive integer: \n");
        scanf("%d", &n);

        if (n < 0)
        {
            printf("Error! Factorial of a negative number doesn't exist.\n");
            break;
        }
        else
        {
            for (a = 1; a <= n; ++a)
            {
                fact *= a;
            }
            printf("Factorial of %d is %.0lf\n", n, fact);
        }

        printf("If you want to Calculate another factorial press 1 otherwise press 0. \n");
        scanf("%d", &i);

    } while (i == 1);

    return 0;
}



Activity 8: 

Write a program that gets starting and ending point from the user and displays all odd numbers in the given range using do-while loop.

Solution:

#include <stdio.h>

int main()
{
    int start, end, num;

    printf("Enter Starting Number: \n");
    scanf("%d", &start);
    printf("Enter Ending Number: \n");
    scanf("%d", &end);
    printf("All Numbers between %d and %d are: \n", start, end);
    num = start;

    do {
        if (num % 2 != 0)
        {
            printf("%d\n", num);
        }
        num++;
    } while (num <= end);

    return 0;
}



Activity 9: 

Write a program that inputs table number and length of table and then displays the table using for loop.

Solution: 

#include <stdio.h>

int main()
{
    int num, length;

    printf("Enter Table Number: \n");
    scanf("%d", &num);
    printf("Enter Length of Table: \n");
    scanf("%d", &length);

    for (int i = 1; i <= length; ++i)
    {
        printf("%d * %d = %d\n", num, i, num * i);
    }

    return 0;
}



Activity 10:

 Write a program to print the following output using loop:


- - - - - * - - - - - 

- - - - * - * - - - -

 - - - * - - - * - - -

 - - * - - - - - * - - 

- * - - - - - - - * - 

* - - - - - - - - - *


Solution:

#include <stdio.h>

int main()
{
    for (int i = 1; i <= 6; i++)
    {
        for (int j = 1; j <= (6 - i); j++)
        {
            printf(" ");
        }
        for (int k = 1; k <= i * 2 - 1; k++)
        {
            if (k == 1 || k == i * 2 - 1)
            {
                printf("*");
            }
            else if (i == 6)
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }

        }
        printf("\n");
    }

    return 0;
}



Activity 11:

Write a program that displays the following using do-while loop.

4 4 4 4

3 3 3

2 2

1


Solution:

#include <stdio.h>

int main()
{
    int row = 1, column = 1;
    int i;
    row = 4;

    do {
        column = 1;
        do {
            printf("%d", row);
            ++column;
        } while (column <= row);
        --row;
        printf("\n");
    } while (row > 0);

    return 0;
}



Activity 12: 

Write a program to print the following pyramid pattern using loop:

*

 * * * 

* * * * * 

* * * * * * * 

* * * * * * * * *


Solution:

#include <stdio.h>

int main()
{
    int space, rows;

    printf("Enter number of rows: ");
    scanf("%d", &rows);

    for (int i = 1, j = 0; i <= rows; ++i, j = 0)
    {
        for (space = 1; space <= rows - i; ++space)
        {
            printf("  ");
        }

        while (j != 2 * i - 1)
        {
            printf("* ");
            ++j;
        }
        printf("\n");
    }

    return 0;
}


Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !