Saturday, July 16, 2016

A program to find the range of a set of numbers.

Write a program to find the range of a set of numbers. Range is the difference between the smallest and biggest number in the list
Let Us C Page [B] (i)

Woh! This bad ass took a lot of time! First -  the loop would not execute properly. Then the logic would crumble! But finally a little push from one of the helpful member CBoards, I realized my mistake and managed to get this program working. To be honest I started working on this bit code last evening at around 07.00 PM and finally managed to get it working perfectly at about 11.00 AM Morning (obviously with 7 hours of sleep at night :p)

Now, I would really not like to bore you to death with my boring daily routine, would I? So without much delay, here's the shining piece of program in C for finding the range of set of numbers. This question is yet again from the book Let Us C by Yashvant Kanetkar.


/* Write a program to find the range of a set of numbers. Range
is the difference between the smallest and biggest number in
the list

Author : Viks Von Doom
Date : 16/07/2016  Time : 6.51 PM IST */

#include<stdio.h>

int main()

{
    int num_1;
    int max_num=0;
    int min_num=0;
    char choice = 'Y';

    printf("Enter a number : ");
    scanf("%d", &num_1);
    max_num = min_num = num_1;

    while(choice == 'Y')
        {
        if(num_1 >= max_num)
            {
            max_num = num_1;
            }

        else if (num_1 <min_num)
            {
            min_num=num_1;
            }

        printf("Do you want to enter a new number? ( Press  Y for Yes):");
        scanf(" %c",&choice);

        if (choice=='Y')
            {
            printf("Enter another number : ");
            scanf("%d", &num_1);
            }

        else printf(" \n Max Number : %d | Min Number : %d | Difference : %d", max_num,min_num,max_num-min_num);
        }
return 0;
}

The Output :  The program runs and prompts the user to enter a number. The it prompts the user to enter if he wants more numbers to be inputted. The user can input as many numbers as he wants, and then the computer displays the highest and the lowest number and the diffence between the two extremes. 

A program to enter numbers and to display the count of positive, negetive and zeros entered

Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered
Let Us C Page 129 [B](g)



/*a program to enter the numbers till the user wants and
at the end it should display the count of positive, negative and
zeros entered

Author : Viks Von Doom
Date : 16/7/2016 Time 05.46 PM IST */

#include<stdio.h>

int main()
{
    int num_1;
    int count_zero = 0;
    int count_positive = 0;
    int count_negetive = 0;
    char choice ='Y';
    while(choice == 'Y')
    {
        printf("Enter a number : ");
        scanf("%d", &num_1);

        if(num_1>0)
            count_positive = count_positive + 1;
        else if (num_1 == 0)
            count_zero = count_zero + 1;
        else count_negetive = count_negetive + 1;

        printf("Do you want to enter another number (Y / N) : ");
        scanf("%s", &choice);

    }

    printf(" Number of positive integers : %d | Number of zeros : %d | Number of negetive integers : %d", count_positive, count_zero,count_negetive);


return 0;
}
 The Output - This program asks the user to enter a number and then gives the user a choice to enter another number. As long as the user selects 'Y', he can continue entering numbers. When he types anything other than 'Y', the while() loop breaks and a final count of numbers entered appears on the screen.

A program to play a matchstick party game with the computer

Write a program for a matchstick game being played between the computer and a user. Your program should ensure that the computer always wins. Rules for the game are as follows. There are 21 matchsticks.- The computer asks the player to pick 1, 2, 3, or 4 matchsticks. After the person picks, the computer does its picking. Whoever is forced to pick up the last matchstick loses the game
Let Us C Page 129 [B] (f)

Do you love playing games on computer, like me? In that case you might really like this program where you can not simply defeat the computer because of the strategy in the game. I will discuss the strategy in a few minutes, but here is the source code of the program:

/* Matchstick game : the one to pick up at last loses

Author : Viks Von Doom
Date : 16/07/2016 Time 04.15 PM IST */

#include<stdio.h>

int main()
{
    int user_choice;
    int max_pick = 4;
    int tot_match = 21;
    int com_choice;

    while(tot_match > 1)
    {
        printf("\n There are %d matchsticks. You can pick at once- 1,2,3 or 4 matchsticks.    \n Pick now : \n", tot_match);
        scanf("%d", &user_choice);

        com_choice = max_pick +1 - user_choice;
        printf(" \n Computer picks : %d \n", com_choice);
        tot_match = tot_match - user_choice - com_choice;

        if(tot_match == 1)
        printf(" \n There is only 1 match stick left for you yo pick up \n You lose! \n");
    }

return 0;
}




The Strategy: You can read about the strategy in this number game at hobbylark.com This game seems to be one of the popular party games for kids as well!


Program to find the value of one number raised to the power of another

Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another.
Let Us C Page 128 [B] (c)

/* Program to calculate the value of a number raised to another

Author : Viks Von Doom
Date : 16/07/2016 Time  : 01:56 PM*/

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

int main()
{
    int num_1, num_2, result;

    printf("Enter the first number : ");
    scanf("%d", &num_1);
    printf("Enter the second number : ");
    scanf("%d", &num_2);

    result = pow(num_1,num_2);

    printf(" The result of %d raised to %d is : %d", num_1, num_2, result);


return 0;
}
 

The program calculates the value of the first number to the power of second number and this is done with the help of pow() function from the math.h header file.  

 

A program to calculate overtime pay of 10 employees. Overtime is paid at the rate of Rs. 12.00 per hour for every hour worked above 40 hours.

Write a program to calculate overtime pay of 10 employees. Overtime is paid at the rate of Rs. 12.00 per hour for every hour worked above 40 hours. Assume that employees do notwork for fractional part of an hour
Let Us C page Page 128 [B] (a)

/*Write a program to calculate overtime pay of 10 employees. Overtime is paid at the rate of Rs. 12.00 per hour for every hour worked above 40 hours. Assume that employees do not work for fractional part of an hour*/ 

#include<stdio.h>

/*Write a program to calculate overtime pay of 10 employees.
Overtime is paid at the rate of Rs. 12.00 per hour for every
hour worked above 40 hours. Assume that employees do not
work for fractional part of an hour


Author : Viks Von Doom 
Date 16/07/2016 Time 1:30 PM IST*/

int main()
{
    int ovr_rate = 12;
    int std_hr = 40;
    int hr_worked;
    int ovr_hr;
    int count = 1;
    int pay;

    while (count <= 10)
    {
        printf("\n Enter the Hours worked : ");
        scanf("%d", &hr_worked);
        ovr_hr = hr_worked - std_hr;
        if (ovr_hr > 0)
        {
            pay = ovr_hr*ovr_rate;
            printf("The Over Time Pay is: %d",pay);
        }
        else printf("Not eligible for overtime pay");

        count = count + 1;
    }


return 0;
}


The output is perfectly the way I wanted it to be. If the employee has worked for less than or equal to 40 hours, the program displays a message that he is not eligible for the overtime pay. for any hour of work over 40 hours, the program displays the payment eligible.