Program list

  1. Addition of two numbers -
  2. Write a program for the temperature conversion FAHRENHEIT  TO CELSIUS.  
  3. Write a program for the implementation of the cosine function
  4. Write a program to calculate the average of N  numbers.
  5. Write a program for the Representation of integer constants on a 16-bit computer.
  6. Write a program to illustrates the use  of scanf function
  7. Write a program to calculate a salesman's salary.
  8. Write a program for the solution of the Quadratic equation.
  9. Write a program to illustrate the use of variables 
  10. Write a program to print a sequence of squares of numbers. 



Write a program for the addition of two numbers.
 

Addition of two numbers -
 

Void main()

   {

 int   number;

 float amount;

number = 100;

amount = 30.75 + 75.35;

 printf(“%d\n”,number);

printf(“%5.2f”,amount);

getch();

   }


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 the 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 inaccuracy is due to the way the floating-point numbers are internally represented inside the computer.

 


 Write a program for the temperature conversion fahrenhit to celsius.  

 

Temperature conversion -
 

                       F - 32

 
             C = ------------
 
                       1.8

 

#define   F_LOW      0     

#define   F_MAX    250     

#define   STEP      25     

main()                                                

        {                                                                 

typedef  float  REAL ;     

REAL   fahrenheit, celsius ; 

fahrenheit = F_LOW ;   

printf("Fahrenheit   Celsius\n\n") ;

while( fahrenheit <= F_MAX )      

      {   

celsius = ( fahrenheit - 32.0 ) / 1.8 ; 

printf("  %5.1f     %7.2f\n", fahrenheit, celsius);

fahrenheit = fahrenheit + STEP ;        

 

     }                                                   

     }           

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. The user-defined data type name REAL is used to declare the variables Fahrenheit and celsius. 


 

 Write a program for the implementation of the cosine function
 

Program using cosine function -

 

#include <math.h>

#define   PI      3.1416

#define   MAX     180

main ( )

    {

  int angle;  float x,y;    

  angle = 0;  

printf(“        Angle     Cos(angle)\n\n”);

  

while(angle  <= MAX)

    {   x = (PI/MAX)*angle; 

         y = cos(x);

         printf(“%15d %13.4f\n”, angle, y);

         angle = angle + 10; 

}

 

}



 Write a program to calculate the average of N  numbers.

 

Program to calculate the average of N numbers –

 


 #include<stdio.h>

#define N  10               /* SYMBOLIC CONSTANT */

void main()

{

int     count ;    /* DECLARATION OF */

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 = %d  Sum = %f", N, sum);

printf("  Average = %f", average);

}

 

 

 


 Write a program for the Representation of integer constants on a 16-bit computer.
 

Representation of integer constants on a 16-bit computer -

 

The program illustrates the use of integer constants on a 16-bit machine. The output in figure 2.3 shows that the integer values larger than 32767 are not properly stored on a 16-bit machine.  However, when they have qualified as long integer (by appending L), the values are correctly stored.

 

 

main()

   {

    printf("Integer values\n\n");  

    printf("%d %d %d\n", 32767,32767+1,32767+10);

    printf("\n");

    printf("Long integer values\n\n");

    printf("%ld %ld %ld\n", 32767L,32767L+1L,32767L+10L); 

  }

 

 

Output 
                                                    

 Integer values            32767 -32768 -32759                                              

 Long integer values    32767 32768 32777           

 

 

The program has shown typical declarations, assignments, and values stored in various types of variables.

 

The variables x and p have been declared as floating-point variables.  Note that the way the value of 1.234567890000 that we assigned to x is displayed under different output formats.  The value of x is displayed as 1.234567880630 under %.12lf format, while the actual value assigned is 1.234567890000.  This is because the variable x has been declared as a float that can store values only up to six decimal places.

 

The variable m that has been declared as int is not able to store the value 54321 correctly.  Instead, it contains some garbage.  Since this program was run on a 16-bit machine, the maximum value that an int variable can store is only 32767.  However, the variable k (declared as unsigned) has stored the value 54321 correctly.  Similarly, the long int variable n has stored the value 1234567890 correctly.

 

The value 9.87654321 assigned to y declared as double has been stored correctly but the value is printed as 9.876543 under %lf format.  Note that unless specified otherwise, the printf function will always display a float or double value to six decimal places.  We will discuss later the output formats for displaying numbers.

 

                                    

main()

     {

                                                   /*..........DECLARATIONS............................*/      

  float      x, p ;

  double     y, q ; 

  unsigned   k ;  

                                                  /*..........DECLARATIONS AND ASSIGNMENTS............*/                                                        

  int    m = 54321 ;       

  long int   n = 1234567890 ;      

                                                  /*..........ASSIGNMENTS.............................*/       

  x = 1.234567890000 ; 

  y = 9.87654321 ;

  k = 54321 ;

  p = q = 1.0 ;

printf("m = %d\n", m) ;

printf("n = %ld\n", n) ;

printf("x = %.12lf\n", x) ;  

printf("x = %f\n", x) ;

printf("y = %.12lf\n",y) ;

printf("y = %lf\n", y) ;

printf("k = %u  p = %f  q = %.12lf\n", k, p, q) ; 

        }                                                                                                               

Output      

  • m = -11215
  • n = 1234567890
  • x = 1.234567880630
  • x = 1.234568
  • y = 9.876543210000
  • y = 9.876543
  • k = 54321
  • p = 1.000000
  • q = 1.000000000000


 

Write a program to illustrates the use of scanf function.

 

The program illustrates the use of scanf function –

 

The first executable statement in the program is a printf, requesting the user to enter an integer number.  This is known as "prompt message" and appears on the screen like


               Enter an integer number 

 

As soon as the user types in an integer number, the computer proceeds to compare the value with 100.  If the value typed in is less than 100, then a message

 

               Your number is smaller than 100 

 

is printed on the screen.  Otherwise, the message  Your number contains more than two digits is printed.  Outputs of the program run for two different inputs are also shown

 

Program  

   Void 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");   

}

                                                                                                                        

Output   

  • Enter an integer number     54
  • Your number is smaller than 100
  • Enter an integer number     108
  • Your number contains more than two digits

A computer manufacturing company has the following monthly compensation policy to their salespersons:               

 Minimum base salary  : 1500.00

 Bonus for every computer sold  :  200.00 

Commission on the total monthly sales: 2 percent

 

Since the prices of computers are changing, the sales price of each computer is fixed at the beginning of every month.  A program to compute a salesperson's gross salary is given in

 

Write a program to calculate the salesman's salary.     

Program to calculate salesman salary    

#define   BASE_SALARY   1500.00

#define   BONUS_RATE     200.00

#define   COMMISSION       0.02

 

 Void 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) ;   

   }

 

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 the 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

 

 

Write a program for the solution of Quadratic equation.

 

SOLUTION OF A QUADRATIC EQUATION -

An equation of the form
ax^2 + 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 formula:

 

root 1=-b +sqrt(b^2-4ac)/2a

root 2= -b-sqrt(b^2-4ac)/2a

 

A program to evaluate these roots is given in Fig.3.10. The program requests the user to input the values of a, b and c and outputs root1 and root2.

 

#include <math.h>     

Void  main()     

    { 

float  a, b, c, discriminant, root1, root2;    

printf("Input values of a, b, and c\n");  

scanf("%f %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 );    

}
}

 

OUTPUT

 

Input values of a, b, and c 

2 ,4, -16  respectively

 Root1 =  2.00                                                                                                                  

 Root2 = -4.00                                                                                                                   

Input values of a, b, and c       1 2 3 

 

ROOTS ARE IMAGINARY

 

The term (b^2-4ac) is called the discriminant.  If the discriminant is less than zero, its square roots cannot be evaluated.  In such cases, the roots are said to be imaginary numbers and the program outputs an appropriate message.

 

The program  shows the use of integer arithmetic to convert a given number of days into months and days.


 

 


 Write a program to print a sequence of squares of numbers. 
 

The program prints a sequence of squares of numbers.
 
Note the use of the shorthand operator *=
 

The program attempts to print a sequence of squares of numbers starting from 2. The statement
a *= a;
which is identical to
a = a*a;
replaces the current value of a by its square. When the value of a becomes equal or greater than N (=100) the while is terminated. Note that the output contains only three values 2, 4, and 16.
 

 

#define N 100
 
#define A 2
 
 

void main()
 
{
 
            int a;
 
            a = A;
 
           while( a < N )
 
{
 
           printf("%d\n", a);
 
           a *= a;
 
}
 
}
 

 

Output
2
4
 16

 

Write a program to illustrate the use of variables 

Program to illustrates the use of variables in expressions and their evaluation.
 


 
#include<stdio.h>
 
void main()
 
{

float a, b, c, x, y, z;
 
a = 9;
 
b = 12;
 
c = 3;
 
x = a - b / 3 + c * 2 - 1;
 
y = a - b / (3 + c) * (2 - 1);
 
z = a -(b / (3 + c) * 2) - 1;
 
printf("x = %f\n", x);
 
printf("y = %f\n", y);
 
printf("z = %f\n", z);

 
}
 

Output
x = 10.000000
y = 7.000000
z = 4.000000

 

 

 

Post a Comment

Previous Post Next Post