Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views5 pages

Cse115 Lab 04

The document provides an overview of operators in C programming, including arithmetic, assignment, and increment/decrement operators. It explains typecasting with examples of implicit and explicit conversions, and includes sample code for various operations. Additionally, it outlines practice and homework exercises related to these concepts.

Uploaded by

Tahsinul Mohsin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Cse115 Lab 04

The document provides an overview of operators in C programming, including arithmetic, assignment, and increment/decrement operators. It explains typecasting with examples of implicit and explicit conversions, and includes sample code for various operations. Additionally, it outlines practice and homework exercises related to these concepts.

Uploaded by

Tahsinul Mohsin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

North South University

Department of Electrical and Computer Engineering


CSE 115L (Programming Language I Lab)
Lab 4: Operators in C

Objective:
● Operators: Familiarize students with the different types of C programming operators.

Operators:
In C programming, operators are symbols that perform operations on variables or values. They are
classified into different categories based on their functionality.
1. Arithmetic Operators: These operators are used for basic mathematical operations.

Operators Operations

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus (remainder after division)

2. Assignment Operators: These operators assign values to variables.

= Simple assignment

+= Addition assignment

-= Subtraction assignment

*= Multiplication assignment

/= Division assignment

%= Modulus assignment
Example:

Arithmetic Operators Assignment Operators

#include <stdio.h> #include <stdio.h>

int main(void) { int main(void) {


int a = 10, b = 3; int x = 5;
int sum, diff, prod, quotient, remainder; int y = 10;

sum = a + b; // Addition x += y; // Equivalent to x = x + y;


diff = a - b; // Subtraction printf("x += y: %d\n", x);
prod = a * b; // Multiplication
quotient = a / b; // Division x -= y; // Equivalent to x = x - y;
remainder = a % b; // Modulus printf("x -= y: %d\n", x);

printf("Sum: %d\n", sum); x *= y; // Equivalent to x = x * y;


printf("Difference: %d\n", diff); printf("x *= y: %d\n", x);
printf("Product: %d\n", prod);
printf("Quotient: %d\n", quotient); x /= y; // Equivalent to x = x / y;
printf("Remainder: %d\n", remainder); printf("x /= y: %d\n", x);

return 0; x %= y; // Equivalent to x = x % y;
} printf("x %%= y: %d\n", x);

return 0;
}

3. Increment/Decrement Operators: These operators increase or decrease the value of a


variable by 1.

++ Increment (can be used as prefix or


postfix)

-- Decrement (can be used as prefix or


postfix)
Typecasting:
Typecasting in programming refers to the process of converting a variable from one data type to
another. In C, this process can either be implicit or explicit.
Implicit: This happens automatically when you assign a value of one data type to a variable of
another type. The compiler automatically converts the value to the target type,
Explicit: This occurs when you manually convert a variable from one type to another using a type
cast operator. You can explicitly tell the compiler how you want a variable to be converted. This
is done by placing the desired data type in parentheses before the variable.

Example:

Implicit Explicit

#include <stdio.h> #include <stdio.h>

int main(void) { int main() {


int a = 5; float a = 5.67;
float b; int b;

// Implicit typecasting (int to float) // Explicit typecasting (float to int)


b = a; b = (int) a;

printf("The value of b is: %.2f\n", b); // printf("The value of b is: %d\n", b); //
Output: 5.00 Output: 5
return 0; return 0;
} }

Division problem:

#include <stdio.h>

int main(void) {
int a = 5, b = 2;
float result, new;

result = a / b; // Integer division


new = (float)a/b; //Typecasting
printf("Result of %d / %d = %.2f\n", a, b, result); // Output: Result of 5 / 2 = 2.00
printf("Result of %d / %d = %.2f\n", a, b, new); // Output: Result of 5 / 2 = 2.50
return 0;
}
Practice:

Write a program to take two integers as input and Take an integer as input and update it using
perform Sum, Difference, Product. assignment operators (+=, -=, *=, /=, %=).
Print the values after each operation.

#include <stdio.h> #include <stdio.h>

int main(void) { int main(void) {


int num1, num2; int num;

// Taking input from the user // Taking input from the user
printf("Enter two integers: "); printf("Enter an integer: ");
scanf("%d %d", &num1, &num2); scanf("%d", &num);

// Performing calculations // Using assignment operators


int sum = num1 + num2; num += 5; // num = num + 5
int difference = num1 - num2; printf("After += 5: %d\n", num);
int product = num1 * num2;
num -= 3; // num = num - 3
// Displaying results printf("After -= 3: %d\n", num);
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference); num *= 2; // num = num * 2
printf("Product: %d\n", product); printf("After *= 2: %d\n", num);

return 0; num /= 4; // num = num / 4


} printf("After /= 4: %d\n", num);

num %= 5; // num = num % 5


printf("After %%= 5: %d\n", num); //
%% is used to print %

return 0;
}
Homework:
1. Write a program to calculate the area of a circle. Take the radius from user.
(Circle area = π * r * r) and π = 3.1416

Sample output:

2. Write a program to calculate the Square and Cube of a Number and *= for both
operations.

Sample output:

3. Write a program to convert Fahrenheit to Celsius. C = (F - 32) * 5/9

Sample output:

You might also like