1.
Advantages and Disadvantages in Flowchart
Flowchart Advantages:
• Clarity and Simplicity: Flowcharts provide a visual representation of
processes, making them easier to understand, especially for non-technical people.
• Problem Solving: Helps break down complex processes into simpler steps.
• Effective Communication: Useful in explaining algorithms, workflows, or
system design to others.
• Error Detection: Helps in identifying logical errors in the flow of
processes.
Flowchart Disadvantages:
• Complexity with Large Processes: As the process becomes more complex,
flowcharts can become difficult to understand and maintain.
• Time-Consuming to Create: For large systems or programs, creating
detailed flowcharts can be very time-consuming.
• Not Suitable for All Situations: Flowcharts may not be the best
representation for all types of algorithms or processes, especially when dealing
with recursive functions or complex data manipulations.
2. What is a Variable and Rules for Framing a Variable
A variable in C is a named storage location in memory that holds a value which can
be modified during program execution. The value stored in a variable can be of any
data type such as integer, floating-point, or character.
Rules for Framing a Variable:
1. The name must start with a letter (a-z, A-Z) or an underscore (_).
2. The name can contain letters, digits (0-9), and underscores but cannot
begin with a digit.
3. Variable names are case-sensitive.
4. A variable name cannot be a C keyword (e.g., int, return, if).
5. The name should be meaningful and descriptive.
Example:
int age = 25; // 'int' is the data type, 'age' is the variable name, 25 is the
value.
3. C Program to Display the Pattern
#include <stdio.h>
int main() {
int i, j;
for(i = 1; i <= 4; i++) {
for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Output:
*
**
***
****
4. C Program to Insert Elements into an Array and Display the Array Elements
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Array elements are:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
5. C Program to Check Whether a Given String is Palindrome or Not
#include <stdio.h>
#include <string.h>
int main() {
char str[100], reverse[100];
int i, len, flag = 0;
printf("Enter a string: ");
gets(str);
len = strlen(str);
// Reverse the string
for(i = 0; i < len; i++) {
reverse[i] = str[len - 1 - i];
}
reverse[i] = '\0'; // Null terminate the reversed string
// Compare original and reversed strings
for(i = 0; i < len; i++) {
if(str[i] != reverse[i]) {
flag = 1;
break;
}
}
if(flag) {
printf("The string is not a palindrome.\n");
} else {
printf("The string is a palindrome.\n");
}
return 0;
}
6. Basic Structure of a C Program
A basic C program typically consists of the following parts:
1. Preprocessor Directives: These are instructions that are executed
before the program begins. For example, #include <stdio.h>.
2. Main Function: The main function is the starting point of every C
program, where the execution begins.
3. Variable Declarations: Variables used in the program must be declared
before use.
4. Statements/Expressions: These are the operations or logic that the
program executes.
5. Return Statement: It terminates the program and returns a value to the
operating system (usually return 0;).
Example:
#include <stdio.h> // Preprocessor Directive
int main() { // Main Function
int a = 5; // Variable Declaration
printf("Value of a: %d", a); // Statement to display value
return 0; // Return Statement
}
7. C Program to Demonstrate Use of Arithmetic Operators
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
}
8. Different Forms of Decision-Making Statements in C
Types of Decision-Making Statements:
• If Statement: Executes a block of code if a condition is true.
• If-else Statement: Executes one block of code if true, and another
block if false.
• If-else if Ladder: Checks multiple conditions.
• Switch Case Statement: Allows selection of one of many code blocks
based on the value of an expression.
Example (if-else):
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num > 0) {
printf("The number is positive.\n");
} else if(num < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}
9. C Program to Reverse a String
#include <stdio.h>
#include <string.h>
int main() {
char str[100], reverse[100];
int i, j = 0;
printf("Enter a string: ");
gets(str);
for(i = strlen(str) - 1; i >= 0; i--) {
reverse[j++] = str[i];
}
reverse[j] = '\0';
printf("Reversed string: %s\n", reverse);
return 0;
}
10. C Program to Concatenate Two Strings
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
strcat(str1, str2); // Concatenate str2 at the end of str1
printf("Concatenated string: %s\n", str1);
return 0;
}