The document covers programming concepts in C, focusing on arithmetic expressions, operators, conditional branching, and bitwise operations. It includes explanations of various operators, examples of their usage, and programming exercises related to these topics. Additionally, it discusses type conversion, operator precedence, and provides sample code for practical understanding.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
31 views27 pages
Multi Atoms Unit 2 Pps 1st Year
The document covers programming concepts in C, focusing on arithmetic expressions, operators, conditional branching, and bitwise operations. It includes explanations of various operators, examples of their usage, and programming exercises related to these topics. Additionally, it discusses type conversion, operator precedence, and provides sample code for practical understanding.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 27
| Programming for Problem Solving |
ne Shot + PYQ Solutions
Unit-2:
Arithmetic Expressions and Precedence : Operators and Expression Using)
Numeric and Relational Operators, Mixed Operands, Type Conversion, Logical
Operators, Bit Operations, Assignment Operator, Operator precedence and
Associatively.
Conditional Branching: Applying if and Switch Statements, Nesting if and Elsd
and Switch.
PYQs
Qz1 Differentiate between implicit and explicit type conversion.
Q.2 Explain various bitwise operators in C Language with help of an example
‘When precedence of two operators in an expression is same, how associativity
helps in identifying which operator will be evaluated first. Ilustrate it with an
example.
Q.3 Write the output of following code:
#include
int main
{
int a=-10, b= 20;
iffa> 0 && b<0)
aby
else iffa< 0 && b <0)
a
else iffa <0 &8& b> 0)
bs
else
be
printf("%din",a +b);
return 0;
}Q-4 Write a program in C to print grades as per following criteria for obtained
percentage of marks M out 100.
Obtained Percent Marks (M) Grade
90>2 ii) 20 << 2 iii) 25 & 30 iv) 25|30
Q6 Illustrate the Concept of Operator Precedence and Associativity with
Example.
Q.7 Write a Program to discuss the use of break in Switch Statement.
Q8 Discuss the Concept of Type Casting and Type Conversion with the
Program.
Q.9 Differentiate Between Operator and Operands.
Q.10 Define Conditional Operator with an example.
Q.II Define Explicit type conversion with suitable example.
Q.12 Find the value of variable max in the following code:
int a=-10, b=20;
int max= (a>b)? a: bs
Q.13 What is the importance of Precedence order and associativity? Explain Bitwise and Temary
operators in detail.
Q.14 Why break statement is required? Write a C program in C language to check character is a vowel
or consonant
Q.15 Write a program in C language to calculate energy bill. Read the starting and ending meter
readings. The charges are as follows: -
No. of Units Consumed rates in Rs.
O, <,
Logical Operators (&&, ||, !)
Bitwise Operators (4, |, ~
Assignment Operators
Conditional (Ternary) Operator (?:)
Arithmetic Operators: These operators perform basic mathematical operations such as
addition, subtraction, multiplication, and division.veer)
OTe)
Subtraction
Muttplication
Dero tea cca}
#include
int main() {
inta = 10, b=3; ouput
printf("Addition: %d\n", a + b); —__
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a/b); // 10/3 = 3 (Integer division)
printf("Modulus: %d\n", a % b); // 10%3 = 1 (Remainder)
return 0;
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
Relational Operators: Used to compare two values. They always return true (1) or false
mera Teeny ea
eto
Nese)
tem)
eeu)
eee nd
Less than or equal to#include
int main() {
inta = 10, b=5;
printf("%d\n", a > b); // 1 (True)
printf("%din", a
int main() {
inta=10,b=5;
printf("%edin", (a > 5) && (b > 2)); 1 (Both True)
printf("%d\n", (a > 5) [| (b < 2)); 1/4 (At least one True)
printf("%edin", (a ==10)); _‘//0 (Reverses the condition.)
return 0;
¢
El Assignment Operators: Used to assign values to variables.Crore Teeny
#include
int main() {
int a = 10;
a+=5; // Same asa=a+5
printf("%din", a); // Output: 15
return 0;
Increment & Decrement Operators
acd corre
eer : Increase first, then
ere ecu cd psa be en
Coed [py tegeoe Re UTD
eae Use first, then decr
#include
int main() {
int x = 5;
printf("%ed\n", ++x); // Pre-increment (First increase, then print)
printf("%ed\n", x--); // Post-decrement (First print, then decrease)
printf("Zod\n", x);
return 0;PYQ: What is the difference between Operator and Operand? Explain with an
example.
C
Operator: A symbol that performs operations on variables/values.
Operand: The values on which the operator acts.
Here, + is an operator.
10 and 5 are operands.
int result
| Bitwise Operators in C
What are Bitwise Operators?
Bitwise operators perform operations at the bit level (Os and 1s),
These operators work only on integers (not on float/double).
@ Example: 5 — 00000101 (Binary Representation)
leer Deere)
Ly Performs bitwise AND (returns 1 if both bits are 1)
oy Pe eee oe a ee ao)
nly Performs bitwise XOR (returns 1 if bits are different)
el Performs bitwise NOT (flips all bits: 1 — 0, 0 - 1)
Pesci Shifts bits to the left (Multiplies the number by 2 for each shift)
Cre ssg 5 Shifts bits to the right (Divides the number by 2 for each shift)Ell Bitwise AND (4): Both bits must be 1 for the result to be 1.
Example:
Output:
#include a&b=1
int main() {
inta=5,b = 3;
printf("a & b = %d\n", a & b);
return 0;
Binary Calculation:
5 — 00000101
3 — 00000011
& — 00000001 (1 in decimal)
Ed Bitwise OR (|): If any bit is 1, the result is 1.
Example:
Output:
#include alb=7
int main() { eet
inta=5,b=3;
printf("a | b = %d\n", a | by;
return 0;
Binary Calculation:
5 — 00000101
3 = 00000011
| = 00000111 (7 in decimal)Bitwise XOR (*): Returns 1 if bits are different.
Example: Output:
#include atb=6
int main() { Ce
inta=5,b=3;
printf("a * b = %din", a“ b);
return 0;
Binary Calculation:
5 — 00000101
3 — 00000011
* —+ 00000110 (6 in decimal)
Ei Bitwise NOT (~): Flips all bits (1 + 0,0 — 1).
Example:
#include
int main() {
inta=5;
printf("~a = %d\n", ~a);
return 0;
}
Binary Calculation:
5 — 00000101
~5— 11111010 (-6 in decimal)Left Shift (<<): Shifts bits to the left.
Example:
#include ax<>): Shifts bits to the right.
Example:
Output:
#include
a>>1
int main() {
inta=5;
printf("a >> 1 = %d\n", a >> 1);
return 0;
Binary Calculation:
5 — 00000101
5>>1— » 00000010 (2 in decimal)Explain different types of bitwise operators used in C with suitable examples. Find
the value of following expressions:
Sree sareeott are CH sea Por ort aret fairer ware feeares atevert Fh area
at Prefers sia a are ara ARAL
i) 10>>2 ii) 20 <<2 iii) 25 & 30 iv) 25|30
i) 10 >> 2 (Right Shift)
Binary of 10: 00001010
Shift right by 2: 00000010 (binary) = 2 (decimal).
Answer: 10 >> 2 =2
ii) 20 << 2 (Left Shift)
Binary of 20: 00010100
Shift left by 2: 01010000 (binary) = 80 (decimal).
Answer: 20 << 2 = 80
iii) 25 & 30 (Bitwise AND)
Binary of 25: 00011001
Binary of 30: 00011110
Bitwise AND: 00011000 (binary) = 24 (decimal).
Answer: 25 & 30 = 24
iv) 25 | 30 (Bitwise OR)
Binary of 25: 00011001
Binary of 30: 00011110
Bitwise OR: 00011111 (binary) = 31 (decimal).
Answer: 25 | 30 = 31