Program 8) An equation of the form
ax(square) + bx + c = 0
is known as the quadratic equation. The values of x that satisfy the equation are known as the roots of the equation. A quadratic equation has two roots which are given by the following two formulae:
root 1 = -b + sqrt(b(square) - 4ac)/ 2a
root 2 = -b - sqrt(b(square) - 4ac)/ 2a
A program to evaluate these roots. The program requests the user to input the values of a, b and c and outputs root 1 and root 2.
PROGRAM
#incluce <math.h>
main( )
{
float a, b, c, discriminant, root1, root2 ;
printf("Input values of a, b, and c\n") ;
scanf("%f %f" , &a, &b, &c) ;
discriminant = b*b - 4*a*c ;
if (discriminant < 0)
printf("\n\nROOTS ARE IMAGINARY\n") ;
else
{
root1 = (-b + sqrt(discriminant)) / (2.0*a) ;
root2 = (-b - sqrt (discriminant)) / (2.0*a) ;
printf("\n\nRoot1 = %5.2f\n\nRoot2 = %5.2f\n",
root1, root2) ;
}
getch( ) ;
}
Output: Input values of a, b, and c
2 4 - 16
Root1 = 2.00
Root2 = -4.00
Input values of a, b, and c
1 2 3
ROOTS ARE IMAGINARY
The term (b(square) - 4ac) is called the discriminant. If the discriminant is less than zero, its square rots cannot be evaluated. In such cases, the roots are said to be imaginary numbers and the program outputs an appropriate message.
This site provides information on every topic in Hindi language. This is the first Hindi site which provide knowledge in Hindi language.
program of salesman's salary
Program 8) A computer manufacturing company has the following monthly compensation policy to their sales-persons:
Minimum base salary : 1500.00
Bonus for every computer sold : 200.00
Commission on the total monthly sales : 2 per cent
Since the prices of computers are changing, the sales price of each computer is fixed at the beginning of every month. Make a program to calculate a sales-person's gross salary.
PROGRAM
#define BASE_SALAR 1500.00
#define BONUS_RATE 200.00
#define COMMISSION 0.02
main( )
{
int quantity ;
float gross_salary, price ;
float bonus, commission ;
printf("Input number sold and price\n") ;
scanf("%d %f, &quantity, &price) ;
bonus = BONUS_RATE * quantity ;
commission = COMMISSION * quantity * price ;
gross_salary = BASE_SALARY + bonus + commission ;
printf("/n") ;
printf("Bonus = %6.2f\n" , bonus) ;
printf("Commission = %6.2f\n", commission) ;
printf("Gross salary = %6.2f\n", gross_salary) ;
getch( ) ;
}
Output: Input number sold and price
5 20450.00
Bonus = 1000.00
Commission = 2045.00
Gross salary = 4545.00
Given the base salary,bonus,and commission rate, the inputs necessary to calculate the gross salary are, the price of each computer and the number sold during the month.
The gross salary is given by the equation
Gross salary = base salary + (quantity * bonus rate)
+ (quantity * price) * commission rate
Minimum base salary : 1500.00
Bonus for every computer sold : 200.00
Commission on the total monthly sales : 2 per cent
Since the prices of computers are changing, the sales price of each computer is fixed at the beginning of every month. Make a program to calculate a sales-person's gross salary.
PROGRAM
#define BASE_SALAR 1500.00
#define BONUS_RATE 200.00
#define COMMISSION 0.02
main( )
{
int quantity ;
float gross_salary, price ;
float bonus, commission ;
printf("Input number sold and price\n") ;
scanf("%d %f, &quantity, &price) ;
bonus = BONUS_RATE * quantity ;
commission = COMMISSION * quantity * price ;
gross_salary = BASE_SALARY + bonus + commission ;
printf("/n") ;
printf("Bonus = %6.2f\n" , bonus) ;
printf("Commission = %6.2f\n", commission) ;
printf("Gross salary = %6.2f\n", gross_salary) ;
getch( ) ;
}
Output: Input number sold and price
5 20450.00
Bonus = 1000.00
Commission = 2045.00
Gross salary = 4545.00
Given the base salary,bonus,and commission rate, the inputs necessary to calculate the gross salary are, the price of each computer and the number sold during the month.
The gross salary is given by the equation
Gross salary = base salary + (quantity * bonus rate)
+ (quantity * price) * commission rate
Illustration of integer airthmetic
Program 7) This program shows the use of integer airthmetic to convert a given number of days into month and days.
PROGRAM
main( )
{
int months, days ;
printf("Enter days\n") ;
scanf("%d", &days) ;
months = days/30 ;
days = days % 30 ;
printf("Months = %d Days = %d, months, days) ;
getch( ) ;
}
Output: Enter days
265
months = 8 days = 25
Enter days
364
Months = 12 Days = 4
Enter days
45
Months = 1 days = 15
The variables months and days are declared as integers. Therefore, the statement
months = days/30;
truncates the decimal part and assigns the integer part to months. Similarly, the statement
days = days%30 ;
assigns the remainder part of the divison to days. Thus the given numberof days in converted into an equivalent number of months and days and the result is printed as shown in the output.
PROGRAM
main( )
{
int months, days ;
printf("Enter days\n") ;
scanf("%d", &days) ;
months = days/30 ;
days = days % 30 ;
printf("Months = %d Days = %d, months, days) ;
getch( ) ;
}
Output: Enter days
265
months = 8 days = 25
Enter days
364
Months = 12 Days = 4
Enter days
45
Months = 1 days = 15
The variables months and days are declared as integers. Therefore, the statement
months = days/30;
truncates the decimal part and assigns the integer part to months. Similarly, the statement
days = days%30 ;
assigns the remainder part of the divison to days. Thus the given numberof days in converted into an equivalent number of months and days and the result is printed as shown in the output.
Temperature conversion problem
Program 6) This program converts the given temperature in fahrenheit to celsius using the following conversion formula:
C = F- 32 / 1.8
PROGRAM
#define F_LOW 0
#define F_MAX 250
#define STEP 25
main( )
{
typedef float REAL ;a
REAL fahrenheit Celsius\n\n") ;
while( farenheit <= F_MAX )
{
celsius = (fahrenheit - 32.0) / 1.8 ;
printf(" %5.1f %7.2f\n", fahrenheit, celsius);
fahrenheit = fahrenheit + STEP ;
}
getch() ;
}
Output: Fahrenheit Celsius
0.0 -17.78
25.0 -3.89
50.0 10.00
75.0 23.89
100.0 37.78
125.0 51.67
150.0 65.56
175.0 79.44
200.0 93.33
225.0 107.22
250.0 121.11
The program prints a conversion table for reading temperature in celsius, given the fahrenheit values. The minimum and maximum values and step size are defined as symbolic constants. These values can be changed by redefining the #define statements. An userdefined data types name REAL is used to declare the variables fahrenheit and celsius. The formation specifications %5.1f and %7.2 in the second printf statement produces two column output as shown.
C = F- 32 / 1.8
PROGRAM
#define F_LOW 0
#define F_MAX 250
#define STEP 25
main( )
{
typedef float REAL ;a
REAL fahrenheit Celsius\n\n") ;
while( farenheit <= F_MAX )
{
celsius = (fahrenheit - 32.0) / 1.8 ;
printf(" %5.1f %7.2f\n", fahrenheit, celsius);
fahrenheit = fahrenheit + STEP ;
}
getch() ;
}
Output: Fahrenheit Celsius
0.0 -17.78
25.0 -3.89
50.0 10.00
75.0 23.89
100.0 37.78
125.0 51.67
150.0 65.56
175.0 79.44
200.0 93.33
225.0 107.22
250.0 121.11
The program prints a conversion table for reading temperature in celsius, given the fahrenheit values. The minimum and maximum values and step size are defined as symbolic constants. These values can be changed by redefining the #define statements. An userdefined data types name REAL is used to declare the variables fahrenheit and celsius. The formation specifications %5.1f and %7.2 in the second printf statement produces two column output as shown.
Calculation of average numbers
Program 5) A program to calculate the average of a set of N numbers
PROGRAM
#define N 10
main( )
{
int count ;
float sum, average, number ;
sum = 0 ;
count = 0 ;
while( count < N)
{
scanf("%f", &number) ;
sum = sum + number ;
count = count + 1 ;
}
average = sum/N ;
printf("N = % Sum = %f", N, sum) ;
printf(" Average = %f", average) ;
getch() ;
}
Output: 1
2.3
4.67
1.42
7
3.67
4.08
2.2
4.25
8.21
N = 10 Sum = 38799999 Average = 3.880
The variable number is declared as float and therefore it can take both integer and real numbers. Since the symbolic constant N is assigned the value of 10 using the #define statement, the program accepts ten values and calculates their sum using the while loop. The variable count counts the number of values and as soon as it becomes 11, the while loop is exited and then the average is calculated.
Notice that the actual value of sum is 38.8 but the value displayed is 38.799999. In fact, the actual value that is displayed is quite dependent on the computer system. Such as inaccuracy is due to the way the floating point numbers are internally represented inside the computer.
PROGRAM
#define N 10
main( )
{
int count ;
float sum, average, number ;
sum = 0 ;
count = 0 ;
while( count < N)
{
scanf("%f", &number) ;
sum = sum + number ;
count = count + 1 ;
}
average = sum/N ;
printf("N = % Sum = %f", N, sum) ;
printf(" Average = %f", average) ;
getch() ;
}
Output: 1
2.3
4.67
1.42
7
3.67
4.08
2.2
4.25
8.21
N = 10 Sum = 38799999 Average = 3.880
The variable number is declared as float and therefore it can take both integer and real numbers. Since the symbolic constant N is assigned the value of 10 using the #define statement, the program accepts ten values and calculates their sum using the while loop. The variable count counts the number of values and as soon as it becomes 11, the while loop is exited and then the average is calculated.
Notice that the actual value of sum is 38.8 but the value displayed is 38.799999. In fact, the actual value that is displayed is quite dependent on the computer system. Such as inaccuracy is due to the way the floating point numbers are internally represented inside the computer.
interactive investment program
Program 4) This program can be converted into a more flexible interactive program using scanf.
In this case, computer requests the user to input the values of the amount to be invested,interest rate and period of investments by printing a prompt message
Input amount,interest rate,and period
and then waits for input values.
Program
main( )
{
int year, period;
float amount, inrate, value;
printf("Input amount, interest rate, and period\n\n);
scanf("%f %f %d", &amount, &inrate, &period);
printf("\n");
year = 1
while(year <= period)
{
value = amount + inrate + amount ;
printf("%2d Rs %8.2f\n", year, value) ;
amount = value ;
year = year + 1 ;
}
getch();
}
Output: Input amount, interest rate, and period
1000 0.14 5
1 Rs11400.00
2 Rs12996.00
3 Rs14815.44
4 Rs16889.60
5 Rs 19524.15
Input amount, interest rate, and period
20000 0.12 7
1 Rs 22400.00
2 Rs 25008.00
3 Rs 28098.56
4 Rs 31470.39
5 Rs 35246.84
6 Rs 39476.46
7 Rs 444213.63
Three variables amount,inrate,and period, the computer begins to calculate the amount at the end of each year, up to 'period' and produces output as shown in program.
Note that the sacnf functions contained three variables. In such cases, care should be exercised to see that the values entered match the order and type of the variables in the list. Any mismatch might lead to unexpected results. The compiler may not detect such errors.
In this case, computer requests the user to input the values of the amount to be invested,interest rate and period of investments by printing a prompt message
Input amount,interest rate,and period
and then waits for input values.
Program
main( )
{
int year, period;
float amount, inrate, value;
printf("Input amount, interest rate, and period\n\n);
scanf("%f %f %d", &amount, &inrate, &period);
printf("\n");
year = 1
while(year <= period)
{
value = amount + inrate + amount ;
printf("%2d Rs %8.2f\n", year, value) ;
amount = value ;
year = year + 1 ;
}
getch();
}
Output: Input amount, interest rate, and period
1000 0.14 5
1 Rs11400.00
2 Rs12996.00
3 Rs14815.44
4 Rs16889.60
5 Rs 19524.15
Input amount, interest rate, and period
20000 0.12 7
1 Rs 22400.00
2 Rs 25008.00
3 Rs 28098.56
4 Rs 31470.39
5 Rs 35246.84
6 Rs 39476.46
7 Rs 444213.63
Three variables amount,inrate,and period, the computer begins to calculate the amount at the end of each year, up to 'period' and produces output as shown in program.
Note that the sacnf functions contained three variables. In such cases, care should be exercised to see that the values entered match the order and type of the variables in the list. Any mismatch might lead to unexpected results. The compiler may not detect such errors.
Program 3) Use of scanf function
main( )
{
int number;
printf("Enter an integer number\n");
scanf("%d", &number);
if(number < 100)
printf("Your number is smaller than 100\n\n");
else
printf("Your number contains more than two digits\n");
getch();
}
Output: Enter an integer number
54
Your number is smaller than 100
Enter an integer number
108
Your number contains more than two digits
main( )
{
int number;
printf("Enter an integer number\n");
scanf("%d", &number);
if(number < 100)
printf("Your number is smaller than 100\n\n");
else
printf("Your number contains more than two digits\n");
getch();
}
Output: Enter an integer number
54
Your number is smaller than 100
Enter an integer number
108
Your number contains more than two digits
Simple programs
SAMPLE PROGRAM 1: PRINTING A MESSAGE
main ( )
{
printf("I see, I remember");
getch();
}
Output: I see, I remember
A program to print one line text
PROGRAM 2: ADDING TWO NUMBERS
main()
{
int number;
float amount;
number= 100;
amount = 30.75 + 75.35;
printf("%d\n",number);
printf("%5.2f",amount);
getch();
}
Output: 100
106.
Program to add numbers
PROGRAM 3: INTERSEST CALCULATION
Calculate the value of money at the end of each year of investment, assuming an interest rate of 11 percent and prints the year, and the corresponding amount in two columns. The output is shown for a period of 10 years with an initial investments of 5000.00. The program uses the following formula:
Value at the end year= value at start of year(1+ interest rate)
#define PERIOD 10
#define PRINCIPAL 5000.00
main()
{
int year;
float amount, value, inrate;
amount = PRINCIPAL;
inrate = 0.11;
year = 0;
while(year <= PERIOD)
{ printf("%2d %8.2f\n" ,year, amount);
value = amount + inrate * amount;
year = year + 1;
amount = values;
}
getch();
}
main ( )
{
printf("I see, I remember");
getch();
}
Output: I see, I remember
A program to print one line text
PROGRAM 2: ADDING TWO NUMBERS
main()
{
int number;
float amount;
number= 100;
amount = 30.75 + 75.35;
printf("%d\n",number);
printf("%5.2f",amount);
getch();
}
Output: 100
106.
Program to add numbers
PROGRAM 3: INTERSEST CALCULATION
Calculate the value of money at the end of each year of investment, assuming an interest rate of 11 percent and prints the year, and the corresponding amount in two columns. The output is shown for a period of 10 years with an initial investments of 5000.00. The program uses the following formula:
Value at the end year= value at start of year(1+ interest rate)
#define PERIOD 10
#define PRINCIPAL 5000.00
main()
{
int year;
float amount, value, inrate;
amount = PRINCIPAL;
inrate = 0.11;
year = 0;
while(year <= PERIOD)
{ printf("%2d %8.2f\n" ,year, amount);
value = amount + inrate * amount;
year = year + 1;
amount = values;
}
getch();
}
Subscribe to:
Comments (Atom)