An if
An if
An if
allows you to make multiple conditional checks and execute different code blocks
based on the outcome of those checks. It's called a "ladder" because it resembles a
series of steps or branches that the program follows based on the conditions being
evaluated.
Start: The flowchart begins with a start point, usually depicted as an oval shape.
If condition is true: If the condition in the if statement is true, the flowchart will
follow the arrow labeled "yes" to the next step, which represents the code block
that will be executed if the condition is true. This code block will be enclosed
within a rectangle shape in the flowchart.
Else-if condition is true: If the condition in the if statement is false, the flowchart
will follow the arrow labeled "no" to the next decision point. This is where the
else-if condition is evaluated. If this condition is true, the flowchart will follow the
arrow labeled "yes" to the corresponding code block that will be executed. This
code block will also be enclosed within a rectangle shape in the flowchart.
Repeat else-if condition evaluation: If the else-if condition is false, the flowchart
will follow the arrow labeled "no" to the next decision point. This process will
repeat for as many else-if conditions as needed.
Else: If none of the conditions in the if and else-if statements are true, the
flowchart will follow the arrow labeled "no" to the final step, which represents the
code block that will be executed in the else case. This code block will also be
enclosed within a rectangle shape in the flowchart.
End: The flowchart ends with an end point, usually depicted as an oval shape.
2) A recursive function is a type of computer programming function that calls itself as part
of its own execution. In other words, a recursive function is a function that solves a
problem by breaking it down into smaller, similar sub problems and solving them in a
repetitive manner until a base case is reached, at which point the function returns a
result. The results from each level of recursion are combined or processed to produce
the final result.
4) struct Student {
int rollNo; // Roll number of the student
char name[100]; // Name of the student (assuming a maximum length of 100
characters)
};
Initialization:
// Example of manual initialization
int matrix[3][4]; // Declaration
matrix[0][0] = 1; // Initialization
matrix[0][1] = 2;
//
6)
Call by Value: In call by value, the value of the argument is passed to the function.
This means that any changes made to the parameter within the function do not
affect the original argument outside the function. The function works with a local
copy of the argument.
7)
An array of structures in programming refers to a collection of structures of the same data
type, stored in contiguous memory locations. Each structure in the array can have multiple
members or fields, which can be of different data types.
8) #include <stdio.h>
int main() {
// Declare and initialize an array of structures
struct Student students[3] = {
{101, "Alice", 20},
{102, "Bob", 22},
{103, "Charlie", 21}
};
}
#include <stdio.h>
int main() {
// Print even numbers from 1 to 10 using a for loop with continue statement
for (int i = 1; i <= 10; i++) {
if (i % 2 == 1) {
continue; // Skip odd numbers
}
printf("%d\n", i); // Print even numbers
}
return 0;
}
break statement: The break statement is used to immediately terminate the execution of
a loop and exit the loop entirely. It is often used in loops, such as for and while, to
prematurely end the loop based on a certain condition.
#include <stdio.h>
int main() {
// Print numbers from 1 to 5 using a while loop with break statement
int i = 1;
while (i <= 5) {
if (i == 3) {
break; // Terminate the loop when i becomes 3
}
printf("%d\n", i);
i++;
}
return 0;
}
10) Pointer arithmetic is a feature in programming languages that allows you to perform
arithmetic operations on pointers, which are variables that store the memory addresses
of other variables. Here's a brief explanation of pointer arithmetic with an example in
C:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to the first element of the array
return 0;
}
In this example, we have an integer array arr with 5 elements, and a pointer ptr that
points to the first element of the array. We use pointer arithmetic to access the elements
of the array by incrementing the pointer ptr in each iteration of the loop. The expression
*(ptr + i) dereferences the pointer ptr + i to access the value stored at the memory
location pointed to by the incremented pointer. The result is the same as accessing arr[i]
directly.
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
12) #include <stdio.h>
int main() {
int n = 5; // number of rows in the pattern
printf("Pattern:\n");
return 0;
}
13) while loop: It is an entry-controlled loop, where the condition is checked before
executing the loop body. If the condition is false initially, the loop will not be executed
at all.
do-while loop: It is an exit-controlled loop, where the loop body is executed at least
once, even if the condition is false, as the condition is checked after executing the loop
body.
14) A pointer in C is a variable that stores the memory address of another variable. It
allows direct manipulation of memory and is often used for dynamic memory
allocation, passing parameters to functions by reference, and for efficient memory
management.
16) If statement: It is used for conditional execution of a block of code based on a given
condition.
For loop: It is used for repeating a block of code a fixed number of times.
While loop: It is used for repeating a block of code as long as a given condition is true.
Do-while loop: It is used for repeating a block of code at least once, and then as long as
a given condition is true.
17) A function declaration in C specifies the function's return type, name, and
parameter list
#include <stdio.h>
// Function declaration
int add(int num1, int num2);
int main() {
int a = 5, b = 7;
int sum = add(a, b); // Function call
printf("Sum: %d\n", sum);
return 0;
}
// Function definition
int add(int num1, int num2) {
return num1 + num2;
}