QB ans (1)
QB ans (1)
QB ans (1)
Definition:
Types of Tokens:
Algorithm:
1. Start.
2. Input two numbers, num1 and num2.
3. Multiply num1 and num2, store the result in product.
4. Display product.
5. End.
Flowchart:
Definition:
Types:
1. Arithmetic Operators: +, -, *, /, %
Example: int sum = a + b;
2. Relational Operators: ==, !=, <, >, <=, >=
Example: if (a > b)
3. Logical Operators: &&, ||, !
Example: if (a > 0 && b < 10)
4. Bitwise Operators: &, |, ^, ~, <<, >>
Example: a = b & c;
5. Assignment Operators: =, +=, -=, *=, /=, %=
Example: a += 5;
6. Increment/Decrement Operators: ++, --
Example: a++;
7. Conditional (Ternary) Operator: ? :
Example: a = (b > c) ? b : c;
c
Copy code
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
return 0;
}
5. Write a program to print the day when the user enters the day
number using a switch-case.
#include <stdio.h>
int main() {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1: printf("Sunday\n"); break;
case 2: printf("Monday\n"); break;
case 3: printf("Tuesday\n"); break;
case 4: printf("Wednesday\n"); break;
case 5: printf("Thursday\n"); break;
case 6: printf("Friday\n"); break;
case 7: printf("Saturday\n"); break;
default: printf("Invalid day number!\n");
}
return 0;
}
struct Employee {
int id;
char name[50];
};
int main() {
struct Employee emp;
printf("\nEmployee Details:\n");
printf("ID: %d\n", emp.id);
printf("Name: %s\n", emp.name);
return 0;
}
7. Write a program to print the Fibonacci series up to a given number.
#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
return 0;
}
int main() {
printf("Even numbers from 1 to 10:\n");
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
return 0;
}
1. Input Unit: Devices like keyboard, mouse, etc., for user input.
2. Central Processing Unit (CPU):
o Control Unit (CU): Manages operations and instructions.
o Arithmetic Logic Unit (ALU): Performs mathematical and
logical operations.
o Registers: Temporary storage within the CPU.
3. Memory Unit:
o Primary Memory: RAM, ROM for storing data.
o Secondary Memory: Hard drives, SSDs, etc., for long-term
storage.
4. Output Unit: Devices like monitors and printers for displaying
results.
Definition:
Components:
1. Compiler:
o Converts the entire source code into machine code before
execution.
o Example: GCC (GNU Compiler Collection).
2. Interpreter:
o Translates and executes the source code line by line.
o Example: Python Interpreter.
3. Assembler:
o Converts assembly language code into machine code.
o Example: NASM (Netwide Assembler).
1. Machine Language:
o Low-level language written in binary (0s and 1s).
o Directly understood by computers but difficult for humans to
write.
2. Assembly Language:
o Low-level language using symbolic instructions (e.g., ADD,
MOV).
o Requires an assembler to convert it to machine code.
3. High-Level Language:
o User-friendly and closer to human language (e.g., C, Java).
o Requires a compiler or interpreter to translate it into machine
code.
Definition:
Symbols:
Algorithm:
1. Start.
2. Input a number, num.
3. Check if num % 2 == 0.
o If true, print "Even".
o Otherwise, print "Odd".
4. End.
Flowchart:
+----------------------+
| Start |
+----------------------+
+----------------------+
| Input num |
+----------------------+
+----------------------+
| num % 2 == 0? |
+----------------------+
/ \
/ \
Yes / \ No
/ \
v v
+---------------+ +----------------+
+---------------+ +----------------+
\ /
\ /
v v
+----------------------+
| End |
Algorithm:
1. Start.
2. Initialize num = 2.
3. For i from 1 to 10:
o Compute result = num * i.
o Print result.
4. End.
Algorithm:
1. Start.
2. For i = 1 to 15:
o Print i.
3. End.
1. Header File:
o A file containing function declarations, macros, and
definitions used in a program.
Example: <stdio.h> provides input/output functions like
o
printf and scanf.
2. Preprocessor Directives:
o Special instructions processed before the compilation of the
program.
o Examples: #include (to include header files), #define (for
macros).
19. What are primary data types? List all the primary data types.
Definition:
Primary data types are the fundamental data types provided by C for
storing basic values.
Types:
Definition:
Types:
Definition:
User-defined data types allow programmers to create new data types that
model real-world entities.
Examples:
1. Structure (struct):
Groups variables of different types under one name.
Example:
struct Student {
int id;
char name[50];
};
2.Union (union):
Similar to structures but shares the same memory for all members.
Example:
union Data {
int i;
float f;
};
3.Enumeration (enum):
Used to define constants.
Example:
enum Days { MON, TUE, WED };
Definition:
Types:
a. Reserved Word:
b. Literals or Constants:
c. Operators:
int age;
float salary;
Definition:
Types of Operators:
1. Arithmetic: +, -, *, /, %.
2. Relational: ==, !=, <, >, <=, >=.
3. Logical: &&, ||, !.
4. Bitwise: &, |, ^, ~, <<, >>.
5. Assignment: =, +=, -=, *=, /=.
6. Increment/Decrement: ++, --.
7. Ternary: ? :.
8. Special: sizeof, &, *.
25. What is the increment and decrement operator? List their types.
Definition:
Types:
1. Pre-Increment/Decrement:
o Increments/decrements before using the variable.
Example:
int a = 5;
int b = ++a; // a = 6, b = 6
2.Post-Increment/Decrement:
int a = 5;
int b = a++; // a = 6, b = 5
Pre-Increment (++var):
int x = 5;
int y = ++x; // x = 6, y = 6
Post-Increment (var++):
int x = 5;
int y = x++; // x = 6, y = 5
Pre-Decrement (--var):
int x = 5;
int y = --x; // x = 4, y = 4
Post-Decrement (var--):
int x = 5;
int y = x--; // x = 4, y = 5
27. What are gets and puts? Define with suitable examples.
Definition:
Example:
#include <stdio.h>
int main() {
char name[50];
Definition:
Types of Loops:
1. For Loop:
Executes a block of code a specific number of times.
Example:
2. While Loop:
Executes a block of code as long as a condition is true.
Example:
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
3. Do-While Loop:
Similar to a while loop but executes the code at least once.
Example:
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
Definition:
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
int a = 10;
if (a > 5) {
printf("a is greater than 5");
}
30. What is the for loop? Write its syntax and give one example.
Definition:
Syntax:
Example:
31. What is the difference between the while loop and the do-while
loop? Explain with a suitable example.
Example:
While Loop:
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
Do-While Loop:
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Program:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
switch (expression) {
case value1:
// Code to execute
break;
case value2:
// Code to execute
break;
default:
// Code to execute if no cases match
}
Program:
#include <stdio.h>
int main() {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1: printf("Sunday\n"); break;
case 2: printf("Monday\n"); break;
case 3: printf("Tuesday\n"); break;
case 4: printf("Wednesday\n"); break;
case 5: printf("Thursday\n"); break;
case 6: printf("Friday\n"); break;
case 7: printf("Saturday\n"); break;
default: printf("Invalid day number!\n");
}
return 0;
}
34. What is the conditional operator? Write its syntax and
flowchart.
Definition:
Syntax:
Example:
Flowchart:
#include <stdio.h>
int main() {
int num1, num2, max;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
return 0;
}
36. Write a program to print the multiplication table using both for
and while loops.
#include <stdio.h>
int main() {
int num;
printf("Enter a number for the multiplication table: ");
scanf("%d", &num);
return 0;
}
#include <stdio.h>
int main() {
int num, i = 1;
printf("Enter a number for the multiplication table: ");
scanf("%d", &num);
return 0;
}
Definition:
A prime number is a number greater than 1 that has only two divisors: 1
and itself. Examples include 2, 3, 5, 7, 11.
Program:
#include <stdio.h>
int main() {
int num, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
printf("%d is not a prime number.\n", num);
return 0;
}
if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
38. What is an array? How to declare it? Write its syntax and give
one example.
Definition:
Syntax:
data_type array_name[size];
Example:
39. How to initialize an integer array and how to access its elements?
Explain with an example.
Initialization:
Accessing Elements:
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
printf("First Element: %d\n", numbers[0]);
printf("Last Element: %d\n", numbers[4]);
return 0;
}
40. Write a program to read five elements into an integer array and
print them with and without using a loop.
Program:
#include <stdio.h>
int main() {
int numbers[5];
printf("Enter 5 numbers:\n");
for (int i = 0; i < 5; i++) {
scanf("%d", &numbers[i]);
}
return 0;
}
41. What is an integer array? Write a program to count the number
of positive and negative numbers from an array of 10 numbers.
Definition:
Program:
#include <stdio.h>
int main() {
int numbers[10], positive = 0, negative = 0;
printf("Enter 10 numbers:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &numbers[i]);
if (numbers[i] > 0) {
positive++;
} else if (numbers[i] < 0) {
negative++;
}
}
return 0;
}
42. Develop a program to read n numbers in an array and print
them in reverse order.
Program:
#include <stdio.h>
int main() {
int n;
int numbers[n];
printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}
return 0;
}
Definition:
data_type array_name[rows][columns];
Program:
#include <stdio.h>
int main() {
int matrix[3][3];
return 0;
}
a. strcat(s1, s2):
b. strstr(s1, s2):
#include <string.h>
#include <stdio.h>
int main() {
char s1[] = "Hello, World!";
char *result = strstr(s1, "World");
printf("Substring found: %s\n", result);
return 0;
}
c. strrev(s1):
#include <stdio.h>
#include <string.h>
int main() {
char s1[20] = "Hello";
strrev(s1); // May need custom implementation if unavailable
printf("Reversed string: %s\n", s1);
return 0;
}
d. strupr(s1):
#include <stdio.h>
#include <string.h>
int main() {
char s1[20] = "hello";
strupr(s1); // May need custom implementation if unavailable
printf("Uppercase string: %s\n", s1);
return 0;
}
45. What is gets and puts? Write a program to read and print
characters using gets and puts.
Program:
#include <stdio.h>
int main() {
char str[100];
return 0;
}
46. List and discuss the different types of looping statements with
suitable examples.
1. For Loop:
o Executes a block of code a specific number of times.
o Example:
2. While Loop:
o Executes a block of code as long as a condition is true.
o Example:
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
3. Do-While Loop:
o Executes the block at least once, then checks the condition.
o Example:
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
Program:
#include <stdio.h>
int main() {
printf("Even numbers from 1 to 10:\n");
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
return 0;
}
One-Dimensional Array:
Two-Dimensional Array:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Definition:
A string is a sequence of characters terminated by a null character \0.
Input/Output Example:
#include <stdio.h>
int main() {
char str[50];
printf("Enter a string: ");
gets(str); // Input
printf("You entered: ");
puts(str); // Output
return 0;
}
Program:
#include <stdio.h>
#include <string.h>
int main() {
char str[100], reversed[100];
printf("Enter a string: ");
scanf("%s", str);
strcpy(reversed, str);
strrev(reversed);
if (strcmp(str, reversed) == 0) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}
Definition:
Differences:
User-Defined
Aspect Library Functions
Functions
Written by the Predefined in
Definition
programmer. libraries.
printf(), scanf(),
Examples void display()
strlen().
For custom logic or Commonly used
Purpose
tasks. operations.
Code Defined within the Stored in header
Location program. files.
Definition:
Recursion is a process in which a function calls itself directly or
indirectly.
Program:
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
Definition:
Syntax:
struct StructureName {
data_type member1;
data_type member2;
// More members...
};
Program:
#include <stdio.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
struct Student s;
printf("\nStudent Information:\n");
printf("ID: %d\n", s.id);
printf("Name: %s\n", s.name);
printf("Marks: %.2f\n", s.marks);
return 0;
}
55. What is a pointer? Explain how pointer variables are declared
and initialized.
Definition:
Program:
#include <stdio.h>
int main() {
int (*ptr)(int, int); // Pointer to a function
ptr = sum;
Definition:
Program:
#include <stdio.h>
int main() {
FILE *file;
char ch;
fclose(file);
return 0;
}
59. What are file positioning functions? List and explain their types
with examples
Definition:
Types:
rewind(file);
60. Write a C program that counts the number of lines, words, tabs,
and characters in a file.
Program:
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *file;
char ch;
int lines = 0, words = 0, tabs = 0, chars = 0;
fclose(file);
Example:
1. Variable:
A named storage location that holds a value.
Example: int x = 5;
2. Constant:
A value that does not change during program execution.
Example: const int PI = 3.14;
3. Associativity:
Determines the direction of evaluation for operators with the same
precedence.
Example: a + b - c evaluates left-to-right.
4. Precedence:
Defines the order in which operators are evaluated.
Example: * has higher precedence than +.
1. if Statement
2. if-else Statement
3. else-if Ladder
4. switch-case Statement
5. Conditional (Ternary) Operator
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
Definition:
Definition:
Input/Output Functions:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int vowels = 0, consonants = 0;
Example:
struct Student {
int id;
char name[50];
float marks;
};
70. Explain the difference between arrays and structures.
Program:
#include <stdio.h>
return 0;
}
73. Write the syntax for opening a file with various modes and
closing a file.
Syntax:
FILE *file;
file = fopen("filename", "mode");
fclose(file);
Modes:
Definition:
Example:
#include <stdio.h>
75. Write a C program to read a file name and display its contents
on the screen.
Program:
#include <stdio.h>
int main() {
FILE *file;
char filename[100], ch;