Program 26) Histogram
Problem: In an organization, the employees are grouped according to their basic pay for the purpose of certain perks. The pay-range and the number of employees in each group are as follows:
Group Pay-Range Number of Employees
1 750 - 1500 12
2 1501 - 3000 23
3 3001 - 4500 35
4 4501 - 6000 20
5 above 6000 11
Draw a histogram to highlight the group sizes.
Problem Analysis: Given the size of groups, it is required to draw bars representing the sizes of various groups. For each bar, its group number and size are to be written.
Given program reads the number of employees belonging to each group and draws a histogram. The program uses four for loops and two if....else statements.
PROGRAM
#define N 5
main ( )
{
int value[N] ;
int i, j, n, x ;
for (n = 0; n < N; ++n)
{
for (i = 1; i <= 3; i++)
{
if (i ==2)
printf ("Enter employees in Group - %d : ", n+1) ;
scanf ("%d", &x) ;
value [n] = x ;
printf ("%d\n", value [n]) ;
}
printf ("\n") ;
printf ("| \n") ;
for (n = 0; n < N; ++n)
{
for (i = 1; i <= 3; i++) ;
{
if (i == 2)
printf ("Group-%1d |", n+1) ;
else
printf (" | ") ;
for (j = 1; j <= value [n]; ++j)
printf ("*") ;
if (i == 2)
printf ("(%d)/n", value[n]) ;
else
printf ("\n") ;
}
printf ("|\n") ;
}
}
Output: Enter employees in Group - 1 : 12
12
Enter employees in Group - 2 : 23
23
Enter employees in Group - 3 : 35
35
Enter employees in Group - 4 : 20
20
Enter employees in Group - 5 : 11
11
|
| ************
Group - 1 | ************ (12)
| ************
|
| ***********************
Group - 2 | ***********************(23)
| ***********************
|
| ***********************************
Group - 3 | *********************************** (35)
| ***********************************
|
| ********************
Group - 4 | ******************** (20)
| ********************
|
| ***********
Group - 5 | *********** (11)
| ***********
|
This site provides information on every topic in Hindi language. This is the first Hindi site which provide knowledge in Hindi language.
Use of continue statement
Program 26) This program illustrates the use of continue statement.
The program evaluates the square root of a series of number and prints the results. The process stops when the number 9999 in typed in.
In case, the series contains any negative numbers,the process of evaluation of square root should be bypassed for such numbers because the square root of a negative number is not defined. The continue statement is used to achieve this. The program also prints a message saying that the number is negative and keeps an account of negative numbers.
The final output includes the number of positive values evaluated and the number of negative items encountered.
PROGRAM
#include <math.h>
main ( )
{
int count, negative ;
double number, sqroot ;
printf ("Enter 9999 to STOP\n") ;
count = 0 ;
negative = 0 ;
while (count <= 100)
{
printf ("Enter a number : ") ;
scanf ("%1f", &number) ;
if (number == 9999)
break ; /* EXIT FROM THE LOOP */
if (number < 0)
{
printf ("Number is negative\n\n") ;
negative++
continue ; /* SKIP REST OF THE LOOP */
}
sqroot = sqrt (number) ;
printf ("Number = %1f\n Square root =
%1f\n\n", number, sqroot) ;
count++ ;
}
printf (" Number of items done = %d\n", count) ;
printf ("\n\nNegative items = %\n", negative) ;
printf ("END OF DATA\n") ;
}
Output : Enter 9999 to stop
Enter a number : 25.0
Number = 25.000000
square root = 5.000000
Enter a number : 40.5
Number = 40.500000
Square root = 6.363961
Enter a number : -9
Number is negative
Enter a number : 16
Number = 16.000000
Square root = 4.000000
Enter a number : -14.75
Number is negative
Enter a number : 80
Number = 80.000000
Square root = 8.944272
Enter a number : 9999
Number of items done = 4
Negative items = 2
END OF DATA
The program evaluates the square root of a series of number and prints the results. The process stops when the number 9999 in typed in.
In case, the series contains any negative numbers,the process of evaluation of square root should be bypassed for such numbers because the square root of a negative number is not defined. The continue statement is used to achieve this. The program also prints a message saying that the number is negative and keeps an account of negative numbers.
The final output includes the number of positive values evaluated and the number of negative items encountered.
PROGRAM
#include <math.h>
main ( )
{
int count, negative ;
double number, sqroot ;
printf ("Enter 9999 to STOP\n") ;
count = 0 ;
negative = 0 ;
while (count <= 100)
{
printf ("Enter a number : ") ;
scanf ("%1f", &number) ;
if (number == 9999)
break ; /* EXIT FROM THE LOOP */
if (number < 0)
{
printf ("Number is negative\n\n") ;
negative++
continue ; /* SKIP REST OF THE LOOP */
}
sqroot = sqrt (number) ;
printf ("Number = %1f\n Square root =
%1f\n\n", number, sqroot) ;
count++ ;
}
printf (" Number of items done = %d\n", count) ;
printf ("\n\nNegative items = %\n", negative) ;
printf ("END OF DATA\n") ;
}
Output : Enter 9999 to stop
Enter a number : 25.0
Number = 25.000000
square root = 5.000000
Enter a number : 40.5
Number = 40.500000
Square root = 6.363961
Enter a number : -9
Number is negative
Enter a number : 16
Number = 16.000000
Square root = 4.000000
Enter a number : -14.75
Number is negative
Enter a number : 80
Number = 80.000000
Square root = 8.944272
Enter a number : 9999
Number of items done = 4
Negative items = 2
END OF DATA
Use of break in a program
Program 24) This program illustrates the use of the break statement n a C program
The program reads a list of positive value and calculates their average. The for loop is written to read values 1000 values. However, if we want the program to calculate the average of any set of values less than 1000, then we must enter a negative number after the last value in the list, to mark the end of input.
PROGRAM
main ( )
{
int m ;
float x, sum, average ;
printf ("This program computes the average of a set
of numbers\n") ;
printf ("Enter values one after another\n") ;
printf ("Enter a NEGATIVE number at the end.\n\n") ;
sum = 0 ;
for (m = 1; m <= 1000; ++m)
{
scanf ("%f', &x)
if (x < 0)
break ;
sum += x ;
}
average = sum/ (float) (m - 1) ;
printf ('\n") ;
printf ("Number of values = %d\n", m - 1) ;
printf ("Sum = %f\n", sum) ;
printf ("Average = %f\n", average) ;
}
Output: This program computes the average of a
set of numbers
Enter values one after another
Enter a NEGATIVE number at the end.
21 23 24 22 26 -1
Number of values = 6
Sum = 138.000000
Average = 23.000000
Each value, when it is read, it is tested to see whether it is a positive number or not. If it is positive, the value is added to the sum; otherwise, the loop terminates. On exit, the average of the values read is calculated and the results are printed out.
The program reads a list of positive value and calculates their average. The for loop is written to read values 1000 values. However, if we want the program to calculate the average of any set of values less than 1000, then we must enter a negative number after the last value in the list, to mark the end of input.
PROGRAM
main ( )
{
int m ;
float x, sum, average ;
printf ("This program computes the average of a set
of numbers\n") ;
printf ("Enter values one after another\n") ;
printf ("Enter a NEGATIVE number at the end.\n\n") ;
sum = 0 ;
for (m = 1; m <= 1000; ++m)
{
scanf ("%f', &x)
if (x < 0)
break ;
sum += x ;
}
average = sum/ (float) (m - 1) ;
printf ('\n") ;
printf ("Number of values = %d\n", m - 1) ;
printf ("Sum = %f\n", sum) ;
printf ("Average = %f\n", average) ;
}
Output: This program computes the average of a
set of numbers
Enter values one after another
Enter a NEGATIVE number at the end.
21 23 24 22 26 -1
Number of values = 6
Sum = 138.000000
Average = 23.000000
Each value, when it is read, it is tested to see whether it is a positive number or not. If it is positive, the value is added to the sum; otherwise, the loop terminates. On exit, the average of the values read is calculated and the results are printed out.
Illustration of nested for loops
Program 23) A class of n students take an annual examination in m subjects. Given program read the marks obtained by each students in various subjects and to compute and print the total marks obtained by each of them.
The program uses two for loops, one for controlling the number of students and the other for controlling the number of subjects. Since both the number of students and the number of subjects are requested by the program, the program may be used for a class of any size and any number of subjects.
The outer loop includes three parts:
(1) reading of roll numbers of students, one
after another;
(2) inner loop, where the marks are read and
totalled for each student; and
(3) printing of total marks and declaration
of grades.
PROGRAM
#define FIRST 360
#define SECOND 240
main ( )
{
int n, m, i, j, roll_numbers, marks, total ;
printf ("Enter number of students and subjects\n") ;
scanf ("%d %d", &n, &m) ;
for (i = 1; i <= n; ++i)
{
printf ("Enter roll_number : ") ;
scanf (%d', &roll_number) ;
total = 0 ;
printf ("\nEnter marks of %d subjects for ROLL
NO %d\n", m, roll_number) ;
for (j =1; j <= m; j++)
{
scanf ("%d", &marks) ;
total = total + marks ;
}
printf ("TOTAL MARKS = %d", total) ;
if (total >= FIRST)
printf (" (FIRST DIVISON)\n\n") ;
else if (total <= SECOND)
printf (" (second Divison)\n\n") ;
else
printf (" (***FAIL*** )\n\n") ;
}
}
Output: Enter number of students and subjects
3 6
Enter roll number : 8701
Enter marks of 6 subjects for ROLL NO 8701
81 75 83 45 61 59
TOTAL MARKS = 404 ( FIRST DIVISON )
Enter roll_number : 8702
Enter marks of 6 subjects for ROLL NO 8702
51 49 55 47 65 41
TOTAL MARKS = 308 ( Second Divison )
Enter roll number : 8704
Enter marks of 6 subjects for ROLL NO 8704
40 19 31 47 39 25
TOTAL MARKS = 201 ( ***FAIL*** )
The program uses two for loops, one for controlling the number of students and the other for controlling the number of subjects. Since both the number of students and the number of subjects are requested by the program, the program may be used for a class of any size and any number of subjects.
The outer loop includes three parts:
(1) reading of roll numbers of students, one
after another;
(2) inner loop, where the marks are read and
totalled for each student; and
(3) printing of total marks and declaration
of grades.
PROGRAM
#define FIRST 360
#define SECOND 240
main ( )
{
int n, m, i, j, roll_numbers, marks, total ;
printf ("Enter number of students and subjects\n") ;
scanf ("%d %d", &n, &m) ;
for (i = 1; i <= n; ++i)
{
printf ("Enter roll_number : ") ;
scanf (%d', &roll_number) ;
total = 0 ;
printf ("\nEnter marks of %d subjects for ROLL
NO %d\n", m, roll_number) ;
for (j =1; j <= m; j++)
{
scanf ("%d", &marks) ;
total = total + marks ;
}
printf ("TOTAL MARKS = %d", total) ;
if (total >= FIRST)
printf (" (FIRST DIVISON)\n\n") ;
else if (total <= SECOND)
printf (" (second Divison)\n\n") ;
else
printf (" (***FAIL*** )\n\n") ;
}
}
Output: Enter number of students and subjects
3 6
Enter roll number : 8701
Enter marks of 6 subjects for ROLL NO 8701
81 75 83 45 61 59
TOTAL MARKS = 404 ( FIRST DIVISON )
Enter roll_number : 8702
Enter marks of 6 subjects for ROLL NO 8702
51 49 55 47 65 41
TOTAL MARKS = 308 ( Second Divison )
Enter roll number : 8704
Enter marks of 6 subjects for ROLL NO 8704
40 19 31 47 39 25
TOTAL MARKS = 201 ( ***FAIL*** )
Printing of a table using do...while loop
Program 22) A program to print the multiplication table from 1 x 1 to 12 x 10
1 2 3 4 ............... 10
2 4 6 8 ............... 20
3 6 9 12 ............... 30
4 ................ 40
-
-
-
12 ................ 120
This program contains two do.... while loops in nested form. The outer loop is controlled by the variable row and executed 12 times. The inner loop is controlled by the variable column and is executed 10 times, each time the outer loop is executed. That is, the inner loop is executed a total of 120 times, each time printing a value in the table.
PROGRAM
#define COLMAX 10
#define ROWMAX 12
main ( )
{
int row, column, y ;
row = 1 ;
printf (" MULTIPLICATION TABLE \n") ;
printf ("-----------------------------------------------------\n") ;
do /* OUTER LOOP BEGINS */
{
column = 1 ;
do /* INNER LOOP BEGINS */
{
y = row * column ;
printf ("%4d", y) ;
column = column + 1 ;
}
while (column <= COLMAX) ;
/* INNER LOOP ENDS */
printf ("\n") ;
row = row + 1 ;
}
while (row <= ROWMAX) ;
/* OUTER LOOP ENDS */
printf ("------------------------------------------\n") ;
}
Output: MULTIPLICATION TABLE
-----------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
11 22 33 44 55 66 77 88 99 110
12 24 36 48 60 72 84 96 108 120
-------------------------------------------------------------------
Notice that the printf of the inner loop does not contain any new line character (\n). This allows the printing of all rows values in one line. The empty printf in the outer loop initiates a new line to print the next row.
1 2 3 4 ............... 10
2 4 6 8 ............... 20
3 6 9 12 ............... 30
4 ................ 40
-
-
-
12 ................ 120
This program contains two do.... while loops in nested form. The outer loop is controlled by the variable row and executed 12 times. The inner loop is controlled by the variable column and is executed 10 times, each time the outer loop is executed. That is, the inner loop is executed a total of 120 times, each time printing a value in the table.
PROGRAM
#define COLMAX 10
#define ROWMAX 12
main ( )
{
int row, column, y ;
row = 1 ;
printf (" MULTIPLICATION TABLE \n") ;
printf ("-----------------------------------------------------\n") ;
do /* OUTER LOOP BEGINS */
{
column = 1 ;
do /* INNER LOOP BEGINS */
{
y = row * column ;
printf ("%4d", y) ;
column = column + 1 ;
}
while (column <= COLMAX) ;
/* INNER LOOP ENDS */
printf ("\n") ;
row = row + 1 ;
}
while (row <= ROWMAX) ;
/* OUTER LOOP ENDS */
printf ("------------------------------------------\n") ;
}
Output: MULTIPLICATION TABLE
-----------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
11 22 33 44 55 66 77 88 99 110
12 24 36 48 60 72 84 96 108 120
-------------------------------------------------------------------
Notice that the printf of the inner loop does not contain any new line character (\n). This allows the printing of all rows values in one line. The empty printf in the outer loop initiates a new line to print the next row.
Calculation of range of values
Program 21) A manufacture company has classified its executives into four levels for the benefit of certain perk. The level and corresponding perks are shown below:
___________________________________________________
Level Perks
Conveyance Entertainment allowance allowance
1 1000 500
2 750 200
3 500 100
4 250 -
__________________________________________________
An executive's gross salary includes basic pay, house rent allowance at 25% of basic pay and other perks. Income tax is withheld from the salary on a percentage basis as follows:
___________________________________________________
Gross salary Tax rate
Gross <= 2000 No tax deduction
2000 < Gross <= 4000 3%
4000 < Gross <= 5000 5%
Gross > 5000 8%
___________________________________________________
Write a program that will read an executive's job number, level number, and basic pay and then compute the net salary after withholding income tax.
Problem analysis:
Gross salary = basic pay + house rent allowance + perks
Net salary = Gross salary - income tax.
The computation of perks depends on the level, while the income tax depends on the gross salary. The major steps are:
1. Read data.
2. Decide level number and calculate perks.
3. Calculate gross salary.
4. Calculate income tax.
5. Compute salary.
6. Print the results.
PROGRAM
#define CA1 1000
#define CA2 750
#define CA3 250
#define CA4 500
#define EA1 500
#define EA2 200
#define EA3 100
#define EA4 0
main ( )
{
int level, jobnumber ;
float gross, basic, house_rent, perks, net, incometax ;
input :
printf ("\nEnter level, job number, and basic pay\n") ;
printf ("Enter 0 (zero) for level to END\n\n") ;
scanf ("%d", &level) ;
if (level == 0) goto stop ;
scanf ("%d %f", &jobnumber, &basic) ;
switch (level)
{
case 1 ;
perks = CA1 + EA1 ;
breaks ;
case 2 ;
perks = CA2 + EA2 ;
breaks ;
case 3 ;
perks = CA3 + EA3 ;
breaks ;
case 4 ;
perks = CA4 + EA4 ;
breaks ;
default :
printf ("Error in level code\n") ;
goto stop ;
}
house_rent = 0.25 * basic ;
gross = basic + house_rent + perks ;
if (gross <= 2000)
incometax = 0 ;
else if (gross <= 4000)
incometax = 0.03 * gross ;
else if (gross <= 5000)
incometax = 0.05 * gross ;
else
incometax = 0.08 * gross ;
net = gross - incometax ;
printf ("%d %d %.2f\n", level, jobnumber, net) ;
goto input ;
stop : printf ("\nEND OF THE PROGRAM") ;
getch ( ) ;
}
Output : Enter level, job number, and basic pay
Enter o (zero) for level to END
1 1111 4000
1 1111 5980.00
Enter level, job number, and basic pay Enter o (zero) for level to END
2 2222 3000
2 2222 4465.00
Enter level, job number, and basic pay
Enter o (zero) for level to END
3 3333 2000
3 3333 3007.00
Enter level, job number, and basic pay Enter o (zero) for level to END
4 4444 1000
4 4444 1500.00
Enter level, job number, and basic pay Enter o (zero) for level to END
0
END OF THE PROGRAM
___________________________________________________
Level Perks
Conveyance Entertainment allowance allowance
1 1000 500
2 750 200
3 500 100
4 250 -
__________________________________________________
An executive's gross salary includes basic pay, house rent allowance at 25% of basic pay and other perks. Income tax is withheld from the salary on a percentage basis as follows:
___________________________________________________
Gross salary Tax rate
Gross <= 2000 No tax deduction
2000 < Gross <= 4000 3%
4000 < Gross <= 5000 5%
Gross > 5000 8%
___________________________________________________
Write a program that will read an executive's job number, level number, and basic pay and then compute the net salary after withholding income tax.
Problem analysis:
Gross salary = basic pay + house rent allowance + perks
Net salary = Gross salary - income tax.
The computation of perks depends on the level, while the income tax depends on the gross salary. The major steps are:
1. Read data.
2. Decide level number and calculate perks.
3. Calculate gross salary.
4. Calculate income tax.
5. Compute salary.
6. Print the results.
PROGRAM
#define CA1 1000
#define CA2 750
#define CA3 250
#define CA4 500
#define EA1 500
#define EA2 200
#define EA3 100
#define EA4 0
main ( )
{
int level, jobnumber ;
float gross, basic, house_rent, perks, net, incometax ;
input :
printf ("\nEnter level, job number, and basic pay\n") ;
printf ("Enter 0 (zero) for level to END\n\n") ;
scanf ("%d", &level) ;
if (level == 0) goto stop ;
scanf ("%d %f", &jobnumber, &basic) ;
switch (level)
{
case 1 ;
perks = CA1 + EA1 ;
breaks ;
case 2 ;
perks = CA2 + EA2 ;
breaks ;
case 3 ;
perks = CA3 + EA3 ;
breaks ;
case 4 ;
perks = CA4 + EA4 ;
breaks ;
default :
printf ("Error in level code\n") ;
goto stop ;
}
house_rent = 0.25 * basic ;
gross = basic + house_rent + perks ;
if (gross <= 2000)
incometax = 0 ;
else if (gross <= 4000)
incometax = 0.03 * gross ;
else if (gross <= 5000)
incometax = 0.05 * gross ;
else
incometax = 0.08 * gross ;
net = gross - incometax ;
printf ("%d %d %.2f\n", level, jobnumber, net) ;
goto input ;
stop : printf ("\nEND OF THE PROGRAM") ;
getch ( ) ;
}
Output : Enter level, job number, and basic pay
Enter o (zero) for level to END
1 1111 4000
1 1111 5980.00
Enter level, job number, and basic pay Enter o (zero) for level to END
2 2222 3000
2 2222 4465.00
Enter level, job number, and basic pay
Enter o (zero) for level to END
3 3333 2000
3 3333 3007.00
Enter level, job number, and basic pay Enter o (zero) for level to END
4 4444 1000
4 4444 1500.00
Enter level, job number, and basic pay Enter o (zero) for level to END
0
END OF THE PROGRAM
Subscribe to:
Comments (Atom)