Simple Expression Based C Programs

Table of Contents
< All Topics
Print

Simple Expression Based C Programs

  • C expressions are a combination of operators, constants, and variables.
  • The simple expressions based c programs are used to compute values, assign values, and test conditions.
  • An expression can perform operations like addition, subtraction, comparison, or logical operations, returning a value.
  • For example, the expression 2 + 3 computes the sum of 2 and 3.
  • Expressions can be simple or complex, involving multiple operators and operands.
  • The evaluation of expressions follows the precedence of operators, which is defined by the C standard.

Program-1: Write a simple program in C to calculate the percentage of student marks for five subjects.

Aim

Write a simple program in C to calculate the percentage of student marks for five subjects.

Procedure/ Algorithm

  • Step 1: Include the standard I/O library to enable input and output operations.
  • Step 2: Define the main function where the program execution begins.
    Step 3: Declare integer variables to store marks of five subjects like m1, m2, m3, m4, m5;
  • Step 4: Prompt the user to enter marks for five subjects out of 100.
    Step 5: Read the marks for five subjects from the user.
  • Step 6: Calculate the average of the marks entered.
    Step 7: Display the average marks.
  • Step 8: Terminate the program.

Code: Calculating average of (given) five subject marks

//WAP in C to calculate the percentage of student marks for five subjects.
#include<stdio.h>
int main()
{
int m1,m2,m3,m4,m5;
printf("\n five sub marks of  a student n 100\n ");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);

float avg= (m1+m2+m3+m4+m5)/5;
printf("\n average marks = %f",avg);

return 0;
}// end main

Output

 five sub marks of  a student n 100
 78
 98
 89
 90
 76

 average marks = 86.000000
--------------------------------
Process exited after 18.25 seconds with return value 27
Press any key to continue . . .

Run Directly on in-built C-Compiler


Program-2: Write a program in C to convert temperature in Celsius to Fahrenheit.

Aim

Write a program in C to convert temperature in Celsius to Fahrenheit

  • Step 1: Include the standard I/O library to enable input and output operations.
  • Step 2: Define the main function where the program execution begins.
    Step 3: Declare integer variables to store temperature in Celsius as cl and Fahrenheit as fl.
  • Step 4: Prompt the user to enter temperature in Celsius.
    Step 5: Read the temp in Celsius to cl.
  • Step 6: Calculate the fl using formula { (°F) = (C x 9/5) + 32 }
    Step 7: Display the Temperature in Fahrenheit.
  • Step 8: Terminate the program.

Code

//WAP in C to convert temperature in Celsius 
//to Fahrenheit. {  (°F) = (C x 9/5) + 32  }

#include<stdio.h>
int main()
{ 
float cl, fr;
printf("\n enter temp in celsius    :   ");
scanf("%f", &cl);

fr= (cl*9/5) +32;
printf("\n temp in Fahrenheit is =%f", fr);

return 0;
}// end main

Output

Output:

 enter temp in celsius    :   37

 temp in Fahrenheit is =98.599998
--------------------------------
Process exited after 4.104 seconds with return value 0
Press any key to continue . . .

Run Directly on in-built C-Compiler


Program-3: Write a simple C program to calculate the area of the triangle.

Aim

Write a simple C program to calculate the area of the triangle.

Procedure/ Algorithm

  • Step 1: Include the standard I/O library to enable input and output operations.
  • Step 2: Define the main function where the program execution begins.
    Step 3: Declare integer variables to store height as hg, breadth as bh and area as ar;
  • Step 4: Prompt the user to enter height and breadth of a triangle.
    Step 5: Read the height and breadth of a triangle to variables hg and bh respectively
  • Step 6: Calculate the area using formula { ar=(hg*bh)/2; }
    Step 7: Display the area.
  • Step 8: Terminate the program.

Code

//WAP in C to calculate the area of the triangle
#include<stdio.h>
int main()
{
float ar, hg,bh;

printf("\nEnter height and breadth \n");
scanf("%f", &hg); //at begineer level it is always good to read one variable at a time.
scanf("%f", &bh);

ar=(hg*bh)/2;

printf("\n area of triangle = %f", ar);

return 0;
}// end main

Output

Output:

Enter height and breadth
30
120

 area of triangle = 1800.000000
--------------------------------
Process exited after 11.23 seconds with return value 32
Press any key to continue . . .

Run Directly on in-built C-Compiler

Conclusion

Thus, the above simple expression based c program serves the purpose by accepting the marks of five subjects and then give the desired output in terms of average of marks of given five subjects and similarly converting the temperature from Celsius to Fahrenheit and calculating area of a tringle for given sides.

So finally, we can say that this C program provides a simple and effective way to calculate the average of five marks. By understanding the concept of average and following the step-by-step code implementation, you can easily adapt this program to suit your specific needs. Whether you’re a beginner or an experienced programmer, this guide will empower you to harness the power of average calculation in your C programming endeavors.


Leave a comment