StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Program #2 (U1): Sum & Average + Operator Precedence
Download
  1. C Programming
  2. C Programming Practice
Program #1 (U1): Hello, World! : Program #3 (U1): Simple Interest (SI) & Compound Interest (CI)
C Programming Practice

Task: Read three integers, compute their sum and (floating) average. Also evaluate one complex expression twice—once as-is and once with parentheses—to demonstrate operator precedence.

 
/* 

  Program: Sum & Average + Operator Precedence (U1)

  What it does:

    1) Reads three integers a, b, c.

    2) Computes sum = a + b + c and average = sum / 3.0 (note the .0 to avoid integer division).

    3) Evaluates a complex expression in two ways:

       - Raw   : a + b * c - 10 / 2   (multiplication/division happen before addition/subtraction)

       - Paren : ((a + b) * c) - (10 / 2) (parentheses change the evaluation order)

*/

 
#include <stdio.h>

 
int main(void) {

    int a, b, c;

    int sum;

    double avg;

    int expr_raw, expr_paren;

 
    printf("Enter three integers (a b c): ");

    if (scanf("%d %d %d", &a, &b, &c) != 3) {

        printf("Invalid input. Please enter exactly three integers.\n");

        return 0;

    }

 
    sum = a + b + c;

    avg = sum / 3.0;   /* 3.0 ensures floating-point division, not integer division */

 
    printf("Sum = %d\n", sum);

    printf("Average = %.2f\n", avg);

 
    /* Operator precedence demo: * and / before + and -, and left-to-right associativity */

    expr_raw   = a + b * c - 10 / 2;

    expr_paren = ((a + b) * c) - (10 / 2);

 
    printf("Expression (raw)   : a + b * c - 10 / 2 = %d\n", expr_raw);

    printf("Expression (paren) : ((a + b) * c) - (10 / 2) = %d\n", expr_paren);

 
    return 0;

}

 

 

Program #1 (U1): Hello, World! Program #3 (U1): Simple Interest (SI) & Compound Interest (CI)
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website