0% found this document useful (0 votes)
18 views15 pages

Module 2 - SBL First Half ATME

This document covers the principles of programming using C, specifically focusing on operators, type conversion, and control statements. It includes detailed explanations of various operators such as arithmetic, relational, logical, and bitwise operators, along with examples of their usage. Additionally, it discusses decision control structures, looping statements, and type conversion methods in C programming.
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)
18 views15 pages

Module 2 - SBL First Half ATME

This document covers the principles of programming using C, specifically focusing on operators, type conversion, and control statements. It includes detailed explanations of various operators such as arithmetic, relational, logical, and bitwise operators, along with examples of their usage. Additionally, it discusses decision control structures, looping statements, and type conversion methods in C programming.
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/ 15

Principles of Programming using C 22POP13

PRINCIPLES OF PROGRAMMING USING C

Module-2

Number of Hours: 6

Operators in C, Type conversion and typecasting.


Decision control and Looping statements: Introduction to decision control, Conditional
Branching statements, iterative statements, nested loops, break and continue statements,
goto statement.

Textbook: Chapter 9.15-9.16, 10.1-10.6

Department of CSE, ATMECE, Mysuru Page 18


Principles of Programming using C 22POP13

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.

Department of CSE, ATMECE, Mysuru Page 19


Principles of Programming using C 22POP13
• Modulus symbol (%)
→ divides the first operand by second operand and
→ returns the remainder.
Remainder is the result obtained after modulus operation.
• To perform modulus operation, both operands must be integers.
• Program to demonstrate the working of arithmetic operators
#include<stdio.h>
void main ()
{
int a=9,b=4,c;
c=a+b;
printf("a+b=%d \n", c);
c=a-b;
printf("a-b=%d \n", c);
c=a*b;
printf("a*b=%d \n", c);
c=a/b;
printf("a/b=%d \n", c);
c=a%b;
printf("Remainder when a divided by b=%d ", c);
}
Output:
a+b=13 a-b=5 a*b=36 a/b=2
Remainder when a divided by b=1
INCREMENT OPERATOR
• ++ is an increment operator.
• As the name indicates, increment means increase, i.e. this operator is used to increase the value of a
variable by 1.
• For example:If b=5 then b++ or ++b; // b becomes 6
• The increment operator is classified into 2 categories:
1) Post increment Ex: b++
2) Pre increment Ex: ++b
• As the name indicates, post-increment means first use the value of variable and then increase the value
of variable by 1.

Department of CSE, ATMECE, Mysuru Page 20


Principles of Programming using C 22POP13
• As the name indicates, pre-increment means first increase the value of variable by 1 and then use the
updated value of variable.
• For ex:If x is 10, then z= x++; sets z to 10 but z = ++x; sets z to 11 Example: Program to illustrate the
use of increment operators. #include<stdio.h>
void main()
{
int x=10,y = 10, z ;
z= x++;
printf(“ z=%d x= %d\n”, z, x);
z = ++y;
printf(“ z=%d y= %d”, z, y);
}
Output:
z=10 x=11 z=11 y=11
DECREMENT OPERATOR
• -- is a decrement operator.
• As the name indicates, decrement means decrease, i.e. this operator is used to decrease the value of a
variable by 1.
• For example:If b=5then b-- or --b; // b becomes 4
• Similar to increment operator, the decrement operator is classified into 2 categories:
1) Post decrement Ex: b--
2) Pre decrement Ex: --b
• For ex:
If x is 10, then z= x--; sets z to 10,but z = --x; sets z to 9.
Example: Program to illustrate the use of decrement operators.
void main()
{
int x=10,y = 10, z ;
z= x--;
printf(“ z=%d x= %d\n”, z, x);
z = --y;
printf(“ z=%d y= %d”, z, y);
}
Output:

Department of CSE, ATMECE, Mysuru Page 21


Principles of Programming using C 22POP13
z=10 x=9
z=9 y=9
ASSIGNMENT OPERATOR
• The most common assignment operator is =.
• This operator assigns the value in right side to the left side.
• The syntax is shown below:
variable=expression;
• For ex:
c=5; //5 is assigned to c
b=c; //value of c is assigned to b 5=c; // Error! 5 is a constant.
• The operators such as +=,*= are called shorthand assignment operators.
• For ex,
a=a+10: can be written as a+=10;
• In the same way, we have:

Operator Example Same as


-= a- =b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
%= a%=b a=a%b

Department of CSE, ATMECE, Mysuru Page 22


Principles of Programming using C 22POP13

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

Department of CSE, ATMECE, Mysuru Page 23


Principles of Programming using C 22POP13

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;

Department of CSE, ATMECE, Mysuru Page 24


Principles of Programming using C 22POP13

printf(“ largest number =%d ”, max);


}
Output:
enter 2 distinct numbers 3 4
largest number = 4
BITWISE OPERATORS
• These operators are used to perform logical operation (and, or, not) on individual bits of a binary
number.
• There are 6 bitwise operators:
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
TYPE CONVERSION
• Type conversion is used to convert data of one type to data of another type.
• Type conversion is of 2 types as shown in below figure:
IMPLICIT CONVERSION
• If a compiler converts one type of data into another type of data automatically, it is known as
implicit conversions.
• There is no data loss in implicit conversion.
• The conversion always takes place from lower rank to higher rank. For ex, int to float as shown
in the above data type hierarchy.
• For ex:int a = 22, b=11;
float c = a; //c becomes 21.000000
float d=b/c=11/22.000000=11.000000/22.000000=0.500000
• If one operand type is same as other operand type, no conversion takes place and type of result
remains same as the operands i.e. int+int=int ,float+float=float

Department of CSE, ATMECE, Mysuru Page 25


Principles of Programming using C 22POP13

• Conversion rules are as follows:


→ If either operand is long double, convert the other to long double.
→ Otherwise, if either operand is double, convert the other to double.
→ Otherwise, if either operand is float, convert the other to float.
→ Otherwise, convert char and short to int.
→ Then, if either operand is long, convert the other to long.
• Example: Program to illustrate implicit conversion.
#include<stdio.h>
void main()
{
int a = 22, b=11; float d ;
d=b/c;
printf("d Value is : %f ", d );
EXPLICIT CONVERSION
• When the data of one type is converted explicitly to another type with the help of some pre-
defined
functions, it is called as explicit conversion.
• There may be data loss in this process because the conversion is forceful.
• The syntax is shown below:
data_type1 v1;
data_type2 v2= (data_type2) v1; where v1 can be expression or variable
• For ex:
float b=11.000000; int c = 22;
float d=b/(float)c=11.000000/22.000000=0.500000
• Example: Program to illustrate explicit conversion.
#include<stdio.h>
void main()
{
float b=11.000000; int c = 22;
float d; d=b/(float)c;

Department of CSE, ATMECE, Mysuru Page 26


Principles of Programming using C 22POP13

printf("d Value is : %f ", d );


}
Output:
d Value is : 0.500000
THE PRECEDENCE OF OPERATORS
The order in which different operators are used to evaluate an expression is called precedence of
operators.

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

printf("Value of ((a + b) * c) / d is : %d \n" , 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; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d " , e );
}
Output:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
Value of a + (b * c) / d is : 50
examples
PROGRAM TO PRINT A SENTENCE.
#include <stdio.h>
void main()
{
printf("C Programming"); //displays the content inside quotation return;
}
Output:
C Programming
PROGRAM TO READ AND DISPLAY AN INTEGER.
#include <stdio.h>
void main()
{
int num;
printf("Enter a integer: ");
scanf("%d",&num); // Storing a integer entered by user in variable num
printf("You entered: %d", num);
return;

Department of CSE, ATMECE, Mysuru Page 28


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

Department of CSE, ATMECE, Mysuru Page 29


Principles of Programming using C 22POP13

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",&dividend);
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);

Department of CSE, ATMECE, Mysuru Page 30


Principles of Programming using C 22POP13

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

Department of CSE, ATMECE, Mysuru Page 31


Principles of Programming using C 22POP13

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

Department of CSE, ATMECE, Mysuru Page 32

You might also like