c programming question bank answers
c programming question bank answers
2. Simple Calculator in C
Code:
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
switch (operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/':
if (num2 != 0) result = num1 / num2;
else {
printf("Error: Division by zero.\n");
return 1;
}
break;
default:
printf("Invalid operator.\n");
return 1;
}
4. Structure of a C Program
A C program typically consists of:
1. Preprocessor Directives: Instructions to the compiler (#include).
2. Global Declarations: Variables or functions declared globally.
3. main() Function: Entry point of the program.
4. Function Definitions: Define reusable code.
Example:
int main() {
int result = add(5, 3); // main() Function
printf("Sum: %d\n", result);
return 0;
}
5. Tokens in C
Definition: Smallest elements of a C program.
Types:
1. Keywords (e.g., int, return)
2. Identifiers (e.g., variable names)
3. Constants (e.g., 10, 3.14)
4. Operators (e.g., +, -)
5. Special Characters (e.g., {, ;)
6. Strings (e.g., "Hello")
6. Conditional Statements
Syntax of if and if-else:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Programs:
1. Check Even or Odd:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
2. Find Largest of Two Numbers:
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
printf("%d is larger.\n", a);
else
printf("%d is larger.\n", b);
return 0;
}
7. Type Conversion in C
Definition: Conversion of data types.
1. Implicit Conversion (Type Promotion): Automatically done by the
compiler.
o Example: int a = 10; float b = a;
2. Explicit Conversion (Type Casting): Forced by the programmer.
o Example: float b = 10; int a = (int)b;
8. Data Types in C
Basic: int, float, double, char.
Derived: array, pointer, structure, union.
Enumeration: enum.
Void: void.
9. Flowchart Symbols
1. Oval: Start/End.
2. Rectangle: Process.
3. Diamond: Decision.
4. Arrow: Flow.
Flowchart to Check Positive, Negative, or Zero:
1. Start → Input n.
2. Decision:
o n > 0 → Output Positive.
o n < 0 → Output Negative.
o Else → Output Zero.
13. Algorithm
Definition: Step-by-step procedure to solve a problem. Purpose: Provides a
clear path for implementation.
Example: Problem: Find the sum of two numbers.
1. Start.
2. Input two numbers.
3. Add the numbers.
4. Output the result.
5. Stop.
14. Role of Algorithms in C Programming
Role:
Provides a step-by-step approach to problem-solving.
Serves as a blueprint for writing code.
Ensures the program logic is clear and efficient.
Examples of Algorithms:
1. Searching Algorithms: Linear search, Binary search.
2. Sorting Algorithms: Bubble sort, Quick sort.
Properties of Algorithms:
1. Input: Takes input values.
2. Output: Produces at least one output.
3. Finiteness: Terminates after a finite number of steps.
4. Definiteness: Every step is clearly defined.
5. Effectiveness: Steps are basic enough to be executed.
Advantages:
Simplifies debugging.
Improves maintainability and scalability.
Optimizes performance.
Algorithm to Convert Fahrenheit to Celsius:
1. Start.
2. Input temperature in Fahrenheit (F).
3. Calculate Celsius (C = (F - 32) * 5 / 9).
4. Output C.
5. Stop.
16. Operators in C
Categories of Operators:
1. Arithmetic Operators: +, -, *, /, %
2. Relational Operators: <, >, <=, >=, ==, !=
3. Logical Operators: &&, ||, !
4. Bitwise Operators: &, |, ^, ~, <<, >>
5. Increment/Decrement Operators: ++, --
6. Assignment Operators: =, +=, -=, etc.
7. Ternary Operator: ? :
Examples:
1. Logical Operators:
int a = 1, b = 0;
if (a && b)
printf("True\n");
else
printf("False\n"); // Output: False
2. Increment/Decrement:
int x = 5;
x++;
printf("%d\n", x); // Output: 6
3. Bitwise Operators:
int a = 5, b = 3;
printf("%d\n", a & b); // Output: 1
// Arithmetic Operators
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
// Assignment Operators
result = a; // Assign
result += b; // Add and Assign
printf("Result after +=: %d\n", result);
return 0;
}
18. Formatted Input/Output Statements
Formatted Input: scanf() reads formatted data.
c
Copy code
int num;
scanf("%d", &num); // Reads an integer
Formatted Output: printf() prints formatted data.
c
Copy code
printf("Value: %d\n", num); // Prints the integer
return 0;
}
22. Evaluate Expressions
(i) (a + b - (c + d) * 3 % e + f / 9)
Assume a = 9, b = 18, c = 5, d = 1, e = 2, f = 18.
1. c + d = 6.
2. 6 * 3 = 18.
3. 18 % e = 0.
4. f / 9 = 2.
5. Result: a + b - 0 + 2 = 29.
(ii) x - y | (x - y * z) + p && q - r % 4
Assume x = 20, y = 5, z = 4, p = 10, q = 15, r = 1.
1. x - y = 15.
2. y * z = 20.
3. x - 20 = 0.
4. r % 4 = 1.
5. q - 1 = 14.
6. p + 0 = 10.
7. 15 | 10 && 14 = 15.
-credits
chatgpt & spideeey