KEMBAR78
C Programming Question Bank Answers | PDF | Integer (Computer Science) | Computer Programming
0% found this document useful (0 votes)
131 views14 pages

C Programming Question Bank Answers

The document provides a comprehensive overview of C programming concepts, including algorithms, flowcharts, basic data types, control structures, and operators. It includes examples of code snippets for various functionalities such as checking even/odd numbers, simple calculators, and conditional statements. Additionally, it covers the structure of a C program, type conversion, and the use of flowchart symbols.

Uploaded by

mahaveeranandu03
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)
131 views14 pages

C Programming Question Bank Answers

The document provides a comprehensive overview of C programming concepts, including algorithms, flowcharts, basic data types, control structures, and operators. It includes examples of code snippets for various functionalities such as checking even/odd numbers, simple calculators, and conditional statements. Additionally, it covers the structure of a C program, type conversion, and the use of flowchart symbols.

Uploaded by

mahaveeranandu03
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/ 14

1.

Algorithm and Flowchart to Check if a Number is Even or Odd Without


Using Modulus Operator
Algorithm:
1. Start.
2. Input the number n.
3. Check the last bit of n using the bitwise AND operation (n & 1):
o If the result is 0, the number is even.
o Otherwise, it is odd.
4. Output "Even" or "Odd".
5. Stop.
Flowchart:
 Input → Decision (Check n & 1 == 0) → Output Even or Output Odd

2. Simple Calculator in C
Code:
#include <stdio.h>

int main() {
char operator;
double num1, num2, result;

printf("Enter an operator (+, -, *, /): ");


scanf("%c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);

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

printf("Result: %.2lf\n", result);


return 0;
}

3. Basic Data Types in C


 int: Stores integers (Size: 2 or 4 bytes, Range: -32,768 to 32,767 or -
2,147,483,648 to 2,147,483,647).
 float: Stores decimal numbers (Size: 4 bytes, Range: ~6 decimal digits).
 double: Stores larger decimal numbers (Size: 8 bytes, Range: ~15
decimal digits).
 char: Stores single characters (Size: 1 byte, Range: -128 to 127 or 0 to
255).

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:

#include <stdio.h> // Preprocessor Directive

int add(int a, int b); // Function Declaration

int main() {
int result = add(5, 3); // main() Function
printf("Sum: %d\n", result);
return 0;
}

int add(int a, int b) { // Function Definition


return a + b;
}

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.

10. Break and Continue


 break: Terminates a loop.
 continue: Skips current iteration.
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3)
break; // Ends loop when i is 3
printf("%d ", i);
}
printf("\n");
for (int i = 1; i <= 5; i++) {
if (i == 3)
continue; // Skips 3
printf("%d ", i);
}
return 0;
}
11. If-Else vs Switch
 if-else: Checks multiple conditions.
 switch: Compares a variable against constant values.
Day Program Using Switch:
#include <stdio.h>
int main() {
int day;
printf("Enter a number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid input.\n");
}
return 0;
}

12. Switch Statement


Syntax:
switch (expression) {
case constant1:
// Code block
break;
case constant2:
// Code block
break;
default:
// Default code block
}
Example: (See question 11).

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.

15. Ternary Operator Evaluation


Given int a = 10, b = 5; evaluate (a > b) ? (a - b) : (b - a):
 Condition: (a > b) is true because 10 > 5.
 Result: Executes (a - b) = 10 - 5 = 5.
 Output: 5.

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

17. Program for Arithmetic and Assignment Operators


#include <stdio.h>
int main() {
int a = 10, b = 5, result;

// 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

19. Purpose of goto Statement


Used for jumping to a specific label in the program. Example:
#include <stdio.h>
int main() {
int x = 1;
label:
if (x <= 5) {
printf("%d ", x);
x++;
goto label; // Jumps back to label
}
return 0;
}
20. Largest of Three Numbers
Algorithm:
1. Start.
2. Input three numbers.
3. Compare and find the largest.
4. Output the largest number.
5. Stop.
Flowchart:
1. Input → Decision → Output.
C Program:
#include <stdio.h>
int main() {
int a, b, c, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

if (a > b && a > c)


max = a;
else if (b > c)
max = b;
else
max = c;

printf("Largest number: %d\n", max);


return 0;
}
21. Sum, Difference, Product, Quotient
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

printf("Sum: %d\n", a + b);


printf("Difference: %d\n", a - b);
printf("Product: %d\n", a * b);
printf("Quotient: %d\n", a / b);

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

You might also like