PROGRAMMING FOR PROBLEM SOLVING(C)
Assignment Questions For Practice (TOPICS-
OPERATORS,DATATYPES,INTRO TO PROGRAMMING,IF ELSE
STATEMENT)
1. List different types of operators in C with examples. 1
2. What is a data type? List the size and range of basic data types.
5
3. Illustrate the compilation process in C. 9
4. Compare algorithm and pseudocode. 13
16
5. Construct a program for an if-else statement. 17
Solutions:-
1. Operators in C:
An operator is a special symbol that tells the compiler to perform specific mathematical,
logical, or relational operations. C language offers various types of operators used with
variables and constants to create expressions21. These operators are grouped into major
categories: 22
● Arithmetic Operators: These perform mathematical calculations. 23
, ○ Example: For int a = 9, b = 3, c = 0; 24
■ Multiply (*): c = a * b; (Result: 27) 25
■ Divide (/): c = a / b; (Result: 3) 26
■ Addition (+): c = a + b; (Result: 12) 27
■ Subtraction (-): c = a - b; (Result: 6) 28
■ Modulus (%): c = a % b; (Result: 0) 29
● Relational Operators (Comparison Operators): These compare two values and return
true (1) or false (0) based on the relationship30.
○ Example: x < y returns true if x is less than y, otherwise false. 31
■ Less than (<): 3 < 5 (Output: True [1]) 32
■ Greater than (>): 7 > 9 (Output: False [0]) 33
■ Less than or equal to (<=): 100 <= 100 (Output: True [1]) 34
■ Greater than or equal to (>=): 50 >= 100 (Output: False [0]) 35
● Equality Operators: C supports two types for strict equality or inequality. 36
○ Equal to (==): Returns 1 (True) or 0 (False). 37
○ Not equal to (!=): Returns 1 (True) or 0 (False). 38
● Logical Operators:
○ Logical AND (&&): Returns true if both operands are true. 39
■ Truth Table: 40
| A | B | A&&B |
|---|---|---|
| 0 | 1 | 0 |
, | 1 | 0 | 0 |
| 0 | 0 | 0 |
|1|1|1|
○ Logical OR (||): Returns false only if both operands are false. 41
■ Truth Table: 42
| A | B | A||B |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 0 | 0 | 0 |
|1|1|1|
○ Logical NOT (!): Takes a single expression and negates its value. 43
■ Truth Table: 44
| A | !A |
|---|---|
| 0 | 1 |
|1|0|
● Unary Operator (Unary Minus): Different from binary operators because it operates on
only one operand. 45
○ Example: int a, b = 10; a = -(b); results in a = -10; 46
● Conditional Operator: Similar to an if-else statement, used within an expression. 47
● Bitwise Operators: Perform operations at the bit level. 48
○ Bitwise AND
○ Bitwise OR
○ Bitwise NOT
● Assignment Operator (=): Responsible for assigning values to variables. 49
○ Example: int x = 10; This assigns the value 10 to variable x. 50
, 2. Data Types:
A data type tells you what kind of data value is stored in a variable51. The data type of a
variable is specified when it's declared and remains until the program finishes executing52.
● Categories of Data Types: 53
○ Primary: int, char, float 54
○ Secondary: Structure, Array, Pointer 55
● Size and Range of Basic Data Types: 56
| DATATYPE | SIZE | RANGE |
|---|---|---|
| unsigned int | 2 bytes | 0 to 65,535 |
| int | 2 bytes | -32768 to 32767 |
| Long int | 4 bytes | -2147483648 to 2147483647 |
| Short int | 2 bytes | -32768 to 32767 |
| Float | 4 bytes | 1.2E−38 to 3.4E+38 |
| char | 1 byte | -128 to 127 |
| unsigned char | 1 byte | 0 to 255 |
3. Compilation Process in C:
The compilation process involves several steps to transform your source code into an
executable program: 57
1. Source File (.c): This is where you write your program instructions in a high-level
language (like C). 58These are essentially commands given to the compiler to achieve a
specific task. 59
2. Header Files (.h): These contain pre-defined standard library functions. 60
3. Compiler: This is a special program that translates your source code (written in C) into
machine language (0s and 1s), which is called object code. 61
Assignment Questions For Practice (TOPICS-
OPERATORS,DATATYPES,INTRO TO PROGRAMMING,IF ELSE
STATEMENT)
1. List different types of operators in C with examples. 1
2. What is a data type? List the size and range of basic data types.
5
3. Illustrate the compilation process in C. 9
4. Compare algorithm and pseudocode. 13
16
5. Construct a program for an if-else statement. 17
Solutions:-
1. Operators in C:
An operator is a special symbol that tells the compiler to perform specific mathematical,
logical, or relational operations. C language offers various types of operators used with
variables and constants to create expressions21. These operators are grouped into major
categories: 22
● Arithmetic Operators: These perform mathematical calculations. 23
, ○ Example: For int a = 9, b = 3, c = 0; 24
■ Multiply (*): c = a * b; (Result: 27) 25
■ Divide (/): c = a / b; (Result: 3) 26
■ Addition (+): c = a + b; (Result: 12) 27
■ Subtraction (-): c = a - b; (Result: 6) 28
■ Modulus (%): c = a % b; (Result: 0) 29
● Relational Operators (Comparison Operators): These compare two values and return
true (1) or false (0) based on the relationship30.
○ Example: x < y returns true if x is less than y, otherwise false. 31
■ Less than (<): 3 < 5 (Output: True [1]) 32
■ Greater than (>): 7 > 9 (Output: False [0]) 33
■ Less than or equal to (<=): 100 <= 100 (Output: True [1]) 34
■ Greater than or equal to (>=): 50 >= 100 (Output: False [0]) 35
● Equality Operators: C supports two types for strict equality or inequality. 36
○ Equal to (==): Returns 1 (True) or 0 (False). 37
○ Not equal to (!=): Returns 1 (True) or 0 (False). 38
● Logical Operators:
○ Logical AND (&&): Returns true if both operands are true. 39
■ Truth Table: 40
| A | B | A&&B |
|---|---|---|
| 0 | 1 | 0 |
, | 1 | 0 | 0 |
| 0 | 0 | 0 |
|1|1|1|
○ Logical OR (||): Returns false only if both operands are false. 41
■ Truth Table: 42
| A | B | A||B |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 0 | 0 | 0 |
|1|1|1|
○ Logical NOT (!): Takes a single expression and negates its value. 43
■ Truth Table: 44
| A | !A |
|---|---|
| 0 | 1 |
|1|0|
● Unary Operator (Unary Minus): Different from binary operators because it operates on
only one operand. 45
○ Example: int a, b = 10; a = -(b); results in a = -10; 46
● Conditional Operator: Similar to an if-else statement, used within an expression. 47
● Bitwise Operators: Perform operations at the bit level. 48
○ Bitwise AND
○ Bitwise OR
○ Bitwise NOT
● Assignment Operator (=): Responsible for assigning values to variables. 49
○ Example: int x = 10; This assigns the value 10 to variable x. 50
, 2. Data Types:
A data type tells you what kind of data value is stored in a variable51. The data type of a
variable is specified when it's declared and remains until the program finishes executing52.
● Categories of Data Types: 53
○ Primary: int, char, float 54
○ Secondary: Structure, Array, Pointer 55
● Size and Range of Basic Data Types: 56
| DATATYPE | SIZE | RANGE |
|---|---|---|
| unsigned int | 2 bytes | 0 to 65,535 |
| int | 2 bytes | -32768 to 32767 |
| Long int | 4 bytes | -2147483648 to 2147483647 |
| Short int | 2 bytes | -32768 to 32767 |
| Float | 4 bytes | 1.2E−38 to 3.4E+38 |
| char | 1 byte | -128 to 127 |
| unsigned char | 1 byte | 0 to 255 |
3. Compilation Process in C:
The compilation process involves several steps to transform your source code into an
executable program: 57
1. Source File (.c): This is where you write your program instructions in a high-level
language (like C). 58These are essentially commands given to the compiler to achieve a
specific task. 59
2. Header Files (.h): These contain pre-defined standard library functions. 60
3. Compiler: This is a special program that translates your source code (written in C) into
machine language (0s and 1s), which is called object code. 61