Module 2 - SBL First Half ATME
Module 2 - SBL First Half ATME
Module-2
Number of Hours: 6
Module 2
OPERATORS IN C, TYPE CONVERSION AND TYPECASTING:
• An operator can be any symbol like + - * / that specifies what operation need to be performed on the
data.
• For ex: + indicates add operation
* indicates multiplication operation
Operand
• An operand can be a constant or a variable.
Expression
• An expression is combination of operands and operator that reduces to a single value.
• For ex: Consider the following expression a+b Here a and b are operands while + is an operator
CLASSIFICATION OF OPERATORS
• Operator Name
• For Example Arithmetic operators +-*/%
• Increment/decrement operators ++ --
• Assignment operators =
Relational operators <>==
Logical operators && || ~
Conditional operator ?:
Bitwise operators &|^
Special operators []
ARITHMETIC OPERATORS
• These operators are used to perform arithmetic operations such as addition, subtraction,
• There are 5 arithmetic operators:
Operator Meaning of Operator
+ Addition
- Subtraction
* Multiplication
/ Division
• Division symbol (/)
→ divides the first operand by second operand and
→ returns the quotient.
Quotient is the result obtained after division operation.
RELATIONAL OPERATORS
• Relational operators are used to find the relationship between two operands.
• The output of relational expression is either true(1) or false(0).
• For example
a>b //If a is greater than b, then a>b returns 1 else a>b returns 0.
• The 2 operands may be constants, variables or expressions.
• There are 6 relational operators:
Operator Meaning of Operator Example
< Greater than 5>3 returns true (1)
< Less than 5<3 returns false (0)
>= Greater than or equal to 5>=3 returns true (1)
<= Less than or equal to 5<=3 return false (0)
== Equal to 5==3 returns false (0)
!= Not equal to 5!=3 returns true(1)
• For ex:
Condition Return values
2>1 1 (or true)
2>3 0 (or false)
3+2<6 1 (or true)
• Example: Program to illustrate the use of all relational operators. #include<stdio.h>
void main()
{
printf(“4>5 : %d \n”, 4>5);
printf(“4>=5 : %d \n”, 4>=5);
printf(“4<5 : %d \n”, 4<5);
printf(“4<=5 : %d \n”, 4<=5);
printf(“4==5 : %d \n”, 4==5);
printf(“4!=5 : %d ”, 4!=5);
}
Output:
4>5 : 0
4>=5 : 0
4<5 : 1
4<=5 :1
4==5 : 0
4!=5 : 1
LOGICAL OPERATORS
• These operators are used to perform logical operations like negation, conjunction and
disjunction.
• The output of logical expression is either true(1) or false(0).
• There are 3 logical operators:
Operator Meaning Example
&& Logical AND If c=5 and d=2 then ((c==5) && (d>5)) returns false.
|| Logical OR If c=5 and d=2 then ((c==5) || (d>5)) returns true.
! Logical NOT If c=5 then !(c==5) returns false.
• All non zero values(i.e. 1, -1, 2, -2) will be treated as true. While zero value(i.e. 0 ) will be
treated as false.
CONDITIONAL OPERATOR
• The conditional operator is also called ternary operator it takes three operands.
• Conditional operators are used for decision making in C.
• The syntax is shown below:
(exp1)? exp2: exp3;
where exp1 is an expression evaluated to true or false; If exp1 is evaluated to true, exp2 is
executed;
If exp1 is evaluated to false, exp3 is executed.
Example: Program to find largest of 2 numbers using conditional operator.
#include<stdio.h>
void main()
{
int a,b, max ;
printf(“ enter 2 distinct numbers \n”); scanf(“%d %d”, &a, &b);
max=(a>b)? a : b;
Example
#include<stdio.h>
void main()
{
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d \n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
Department of CSE, ATMECE, Mysuru Page 27
Principles of Programming using C 22POP13
}
Output:
Enter a integer: 25 You entered: 25
PROGRAM TO ADD TWO INTEGERS
#include <stdio.h>
void main( )
{
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2); // Stores the two integer entered by user in
// variable num1 and num2
sum=num1+num2; // Performs addition and stores it in variable return;
}
Output:
Enter two integers: 12 11
Sum: 23
PROGRAM TO MULTIPLY TWO FLOATING POINT NUMBERS
#include <stdio.h>
void main( )
{
float num1, num2, product;
printf("Enter two numbers: ");
scanf("%f %f",&num1,&num2); //Stores two floating point numbers entered
//by user in variable num1 and num2 respectively
product = num1*num2; // Performs multiplication and stores it
printf("Product: %f", product);
return;
}
OUTPUT:
Enter two numbers: 2.4 1.1
Product: 2.640000
PROGRAM TO FIND QUOTIENT AND REMAINDER OF TWO INTEGERS ENTERED
BY USER
#include <stdio.h>
void main()
{
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d",÷nd);
printf("Enter divisor: ");
scanf("%d",&divisor);
quotient=dividend/divisor; // Computes quotient
remainder=dividend%divisor; // Computes remainder
printf("Quotient = %d\n",quotient);
printf("Remainder = %d",remainder);
return;
}
Output:
Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1
PROGRAM TO FIND THE AREA OF A RECTANGLE.
#include<stdio.h>
#include<conio.h>
main( )
{
int l, b, area;
printf(“enter length and breadth”);
scanf(“%d %d”, &l, &b);
area=l*b;
printf(“area of rectangle=%d”, area);
}
Output:
enter length and breadth 3 4
area of rectangle=12
PROGRAM TO FIND THE AREA OF A CIRCLE.
#include<stdio.h>
#include<conio.h>
main( )
{
float r, area;
printf(“enter radius”);
scanf(“%f”, &r);
area=3.145*r*r;
printf(“area of circle=%f”, area);
}
Output:
enter radius 4
area of circle=12.58
PROGRAM TO FIND SQUARE OF A NUMBER
#include<stdio.h>
main()
{
int n, p;
printf(“enter a number”);
scanf(“%d”,&n);
p=n*n;
printf(“The square of the number is %d”,p);
}
Output:
enter a number 4
The square of the number is 16
PROGRAM TO FIND CUBE OF A NUMBER.
#include<stdio.h>
void main()
{
int n, p;
printf(“enter a number”);
scanf(“%d”,&n); p=n*n*n;
printf(“The cube of the number is %d”,p);
}
Output:
enter a number 4
The cube of the number is 64
PROGRAM TO SWAP TWO NUMBERS
#include <stdio.h>
void main()
{
float a, b, temp;
printf("Enter value of a: ");
scanf("%f",&a);
printf("Enter value of b: ");
scanf("%f",&b);
temp = a; // Value of a is stored in variable temp
a = b; // Value of b is stored in variable a
b = temp; //Value of temp is stored in variable b
printf ("\nAfter swapping, value of a = %.2f\n", a);
printf("After swapping, value of b = %.2f", b);
}