0% found this document useful (0 votes)
17 views12 pages

Pic QB (Solved and Readable)

The document outlines key features of the C programming language, including its simplicity, efficiency, and support for structured programming. It also covers fundamental concepts such as constants vs variables, algorithms, and various input/output methods. Additionally, it provides syntax examples for conditional statements, operators, and formatted I/O, along with a sample program demonstrating basic arithmetic operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views12 pages

Pic QB (Solved and Readable)

The document outlines key features of the C programming language, including its simplicity, efficiency, and support for structured programming. It also covers fundamental concepts such as constants vs variables, algorithms, and various input/output methods. Additionally, it provides syntax examples for conditional statements, operators, and formatted I/O, along with a sample program demonstrating basic arithmetic operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Features of C Language:

1. Simple and Efficient – Easy to learn and use.

2. Fast Execution – Low-level access makes it fast.

3. Portable – Can run on different platforms.

4. Rich Library– Has built-in functions for various tasks.

5. Memory Management– Supports dynamic memory allocation.

6. Structured Programming – Uses functions and loops for better code


organization.

Q2 Constant vs Variable

Fixed value that cannot be changed Value can be modified during


during execution. execution.
Defined using `const` or `#define` Declared normally using a data type.
Memory location is read-only. Memory location is writable
Example: `const int a = 10; Example: `int a = 10;
Used for values that remain Used for storing data that may
unchanged (e.g., PI). change.
Improves code safety and Allows flexibility in program
readability. execution

3. Syntax of Nested `if-else`:

If (condition1) {

If (condition2) {

// Code block if both conditions are true

} else {

// Code block if condition1 is true but condition2 is false


}

} else {

// Code block if condition1 is false

4. Unformatted Input/Output Statements in C:

Unformatted input/output functions are used for character and string-based


input and output. Some common functions are:

- `getchar() – Reads a single character from standard input.

- `putchar() – Writes a single character to standard output.

- `gets()` – Reads a string from standard input.

- `puts()` – Writes a string to standard output.

- `getch()` / `getche()` – Reads a character (without or with echo) from the


keyboard.

- `putch()` – Writes a character to standard output.

5. A) What is an Algorithm?

An algorithm is a step-by-step procedure or set of rules to solve a specific


problem. It consists of a sequence of instructions that lead to the desired
output when executed.

Factors Affecting the Quality of an Algorithm:

1. Time Complexity– The amount of time an algorithm takes to execute.

2. Space Complexity – The amount of memory required for execution.

3. Correctness – The algorithm should provide accurate and expected results.

4. Simplicity – It should be easy to understand and implement.

5. Scalability – It should handle large inputs efficiently.

6. Generality– Should work for different types of inputs


6. Definitions:

1) Assembler – Converts assembly language code into machine code


(binary) for execution by the CPU.

2) Compiler – Translates high-level language code (C, C++, Java) into


machine code or intermediate code in one go before execution.
Example: GCC for C.

3) Interpreter – Translates and executes code line by line instead of


compiling it all at once. Example: Python interpreter.

4) Linker – Combines multiple object files and libraries into a single


executable file, resolving function and variable references.

7. Four Features of C Language:

1. Fast Execution – C is a compiled language, making it faster than


interpreted languages.

2. Portability – C programs can run on different hardware with minimal


changes.

3. Rich Set of Operators – Supports a wide range of operators like arithmetic,


logical, and bitwise.

4. Modularity – Supports functions for better code organization and reuse.

8. Syntax of `else if` Ladder:


If (condition1) {

// Code block if condition1 is true

} else if (condition2) {

// Code block if condition2 is true

} else if (condition3) {

// Code block if condition3 is true

} else {

// Code block if none of the conditions are true

```

9. Arithmetic and Logical Operators

Arithmetic Operators:

| Operator | Use | Example |

| `+` | Addition | `a + b` |

| `-` | Subtraction | `a – b` |

| `*` | Multiplication | `a * b` |

| `/` | Division | `a / b` |

Logical Operators:

| Operator | Use | Example |

| `&&` | Logical AND | `(a > 5 && b < 10)` |

| `||` | Logical OR | `(a > 5 || b < 10)` |

| `!` | Logical NOT | `!(a == b)` |

| `==` | Equality Check | `(a == b)` |


10. Unformatted Input/Output Statements in C:

Unformatted input/output functions handle character and string-based I/O.


Common functions include:

Input Functions:

- getchar() – Reads a single character.

- gets() – Reads a string (unsafe, use fgets() instead).

- getch() – Reads a character without displaying it on screen.

- getche() – Reads a character and displays it immediately.

Output Functions:

- putchar() – Outputs a single character.

- puts() – Outputs a string followed by a newline.

- putch() – Outputs a character to the screen.

11. Rules for Variable Declaration in C:

1. Must start with a letter or underscore (_) – Cannot start with a digit.

2. Can contain letters, digits, and underscore (_) – No special characters


allowed.

3. Cannot be a keyword – Example: int, float, if, etc. are reserved.

4. Case-sensitive – var and Var are different variables.

5. Must be declared before use – Example: int num; before using num.

6. No spaces or special symbols – Example: user_name is valid, but user


name is not.

7. Should have a meaningful name – Helps in better code readability.


12. Symbols Used in Flowchart and Their Uses:

Symbol: Oval (◯)

Name: Start/End

Use: Represents the beginning or end of a flowchart.

Symbol: Parallelogram (▰)

Name: Input/Output

Use: Represents input (e.g., scanf()) or output (e.g., printf()).

Symbol: Rectangle (▭)

Name: Process

Use: Represents a process like a calculation or assignment.

Symbol: Diamond (◇)

Name: Decision

Use: Represents a decision-making step (e.g., if-else conditions).

Symbol: Arrow (→)

Name: Flowline

Use: Shows the direction of flow in the program.


13. Difference Between Algorithm and Flowchart

Algorithm:

- A step-by-step procedure to solve a problem.

- Written in plain language or pseudocode.

- Easier to modify and understand.

- Example: Steps to make tea.

Flowchart:

- A pictorial representation of an algorithm.

- Uses symbols like rectangles, diamonds, and arrows.

- Helps visualize the flow of execution.

- Example: A flowchart showing the process of making tea.

14. Formatted Input and Formatted Output Statements

Formatted Input (scanf):

- Used to take input from the user with specific formats.

- Uses format specifiers like %d, %f, %c, %s, etc.

Example:

```c

Int age;

Printf(“Enter your age: “);

Scanf(“%d”, &age); // Takes integer input


```

Formatted Output (printf):

- Used to display output with a specific format.

- Supports format specifiers for different data types.

Example:

```c

Int age = 20;

Printf(“Your age is %d years.\n”, age); // Prints integer output

```

15. Switch Case Statement with Flowchart

Explanation:

- The switch statement is used when multiple conditions depend on the value
of a single variable.

- It replaces multiple if-else conditions, making the code more readable.

- Each case represents a possible value of the variable.

Syntax:

```c

Switch (expression) {

Case value1:

// Code block for value1

Break;
Case value2:

// Code block for value2

Break;

Default:

// Code block if no case matches

```

Example:

```c

Int choice;

Printf(“Enter a number (1-3): “);

Scanf(“%d”, &choice);

Switch (choice) {

Case 1:

Printf(“You selected One.\n”);

Break;

Case 2:

Printf(“You selected Two.\n”);

Break;

Case 3:

Printf(“You selected Three.\n”);

Break;

Default:

Printf(“Invalid selection.\n”);

}
16 List of Format Specifiers and Their Uses

%d or %i – Integer (decimal)

%f – Float

%lf – Double

%c – Character

%s – String

%x – Hexadecimal (lowercase)

%X – Hexadecimal (uppercase)

%o – Octal

%u – Unsigned integer

%p – Pointer address

17. Program

#include <stdio.h>

Int main() {

Int num1, num2, result;

Char operator;

Printf(“Enter two numbers: “);

Scanf(“%d %d”, &num1, &num2);

Printf(“Enter operator (+, -, *, /): “);

Scanf(“ %c”, &operator); // Space before %c to consume any leftover


whitespace
Switch(operator) {

Case ‘+’:

Result = num1 + num2;

Printf(“%d + %d = %d\n”, num1, num2, result);

Break;

Case ‘-‘:

Result = num1 – num2;

Printf(“%d - %d = %d\n”, num1, num2, result);

Break;

Case ‘*’:

Result = num1 * num2;

Printf(“%d * %d = %d\n”, num1, num2, result);

Break;

Case ‘/’:

If (num2 != 0) {

Result = num1 / num2;

Printf(“%d / %d = %d\n”, num1, num2, result);

} else {

Printf(“Error! Division by zero is not allowed.\n”);

Break;

Default:

Printf(“Invalid operator! Please enter a valid operator (+, -, *, /).\n”);

Break;

}
Return 0;

You might also like